Android蓝牙开发的一点总结

蓝牙的开发和使用中,主要过程为:

(1)本机蓝牙设备的开启

(2)扫描周边可用蓝牙设别

(3)配对设备

(4)连接设备创建RFCOMM信道进行数据通信

 

1.添加权限

android开发之前需要添加BluetoothBluetoothAdmin两个权限,这样才能对蓝牙进行操作。

2.开启本地蓝牙

在搜索蓝牙设备之前,需要先查看本机是否支持蓝牙,然后打开本地的蓝牙设备,并将蓝牙设备处于可见的状态,便于别个设备可以搜索到本地蓝牙。

Intent mIntent = new  Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 

startActivityForResult(mIntent, 1);   

Intent intent = new  Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);

startActivityForResult(intent, 1);

这几行代码允许本地蓝牙设备能在300s内被搜索到。

3.获取本地蓝牙适配器

BluetoothAdapter代表的是本地的蓝牙适配器设备,通过此类可让用户执行基本的蓝牙任务。例如设备的搜索,蓝牙设备的配对等相关的操作。为了取得本地蓝牙适配器,需要调用静态方法getDefaultAdapter()来获得本地蓝牙适配器,拥有蓝牙适配器后,用户可以通过搜索获得一系列BluetothDevice对象。

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

mBluetoothAdapter.startDiscovery();来开启设备的搜索

蓝牙的搜索是一个异步的过程,不需要考虑线程被阻塞的问题。搜索的过程大约有12s,这时需要紧接着注册一个BroadcastReceiver对象来接收查找到的蓝牙设备信息,这些信息中包含了其mac地址,IP和蓝牙设备名等其他一些相关信息。

private class BluetoothReceiver extends BroadcastReceiver

{

public void onReceive(Context context, Intent intent)

     {

        String action = intent.getAction();

     if(BluetoothDevice.ACTION_FOUND.equals(action))

     {    

           BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

           if(IsLock(bluetoothDevice))

           {

      bluelist.add(bluetoothDevice.getName());//保存蓝牙设备的名字

      listdevice.add(bluetoothDevice);//保存蓝牙设备

           }

           showDevices();

     }

        }

}

注册完成后,需要使用代码结束注册

//结束注册

protected void onDestroy() 

{

//蓝牙协议中需要关闭广播,减少能耗

unregisterReceiver(blueReceiver);

super.onDestroy();

}

搜索到的蓝牙设备信息保存在一个listview

private void showDevices() {

blueadapter = new ArrayAdapter(BluetoothActivity.this, android.R.layout.simple_list_item_1, bluelist);

bluelistView.setAdapter(blueadapter);

}

 

4.通信的建立

Android中的蓝牙和Socket套接字是紧密相连的,蓝牙端的监听接口和TCP的端口类似,都是使用socketserversocket类。在服务器端,serversocket创建一个监听服务监听端口,接受客户端的连接申请,客户端则不断向指定地址和端口发送连接请求,连接成功后,创建RFCOMM信道来进行通信,双方都是用过getInputSreamgetOutputStream来打开I/O流,从而获得输入输出流。

一个蓝牙设备既可以做客户端也可以做服务端。运行机制和JAVA中的serversocketsocket是一样的。和其它移动蓝牙设备进行连接时,手机端可以作为客户端,移动蓝牙端作为服务端,手机端向服务端进行连接请求,通过不同的UUID,和服务端建立起相应的连接。通信属于异步,需要另外开启线程来运行。

客户端:

private class BlueConnectThread extends Thread

{

public BlueConnectThread(BluetoothDevice device)

try 

{

//根据UUID创建蓝牙socket

socket =  device.createRfcommSocketToServiceRecord(MY_UUID);

} catch (IOException e) {}

}

public void run()

{

mBluetoothAdapter.cancelDiscovery();

try 

{

socket.connect();

Blueout = socket.getOutputStream();

Bluein = socket.getInputStream();

blueReader = new BufferedReader(new InputStreamReader(Bluein));

} catch (Exception e){}

}

}

socket建立好后,mBluetoothAdapter.cancelDiscovery();避免在不断的搜索过程中消耗过多资源。通过建立好的socket获得输入输出流,实现数据的交互。

建立连接所使用的UUID是通用唯一识别码:Universally Unique Identifier,是一种软件构建的标准。蓝牙通信中不同的通信操作会对应不同的UUID。比如蓝牙的数据通信的UUID就是

UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

将字符串转换成UUID对象。

 

你可能感兴趣的:(Android蓝牙开发的一点总结)