转自:http://blog.csdn.NET/jason0539/article/details/17782035
最近在做蓝牙开锁的小项目,手机去连接单片机总是出现问题,和手机的连接也不稳定,看了不少蓝牙方面的文档,做了个关于蓝牙连接的小结。
在做Android蓝牙串口连接的时候一般会使用
- BluetoothSocket tmp = null;
- // Get a BluetoothSocket for a connection with the
- // given BluetoothDevice
- try {
- tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
- } catch (IOException e) {
- Log.e(TAG, "create() failed", e);
- }
然后是tmp赋给BluetoothSocket,接着调用connect方法进行蓝牙设备的连接。
可是 BluetoothSocket 的connect方法本身就会报很多异常错误。
以下根据对蓝牙开发的一点研究可通过以下方法解决:
方法1.先进行蓝牙自动配对,配对成功,通过UUID获得BluetoothSocket,然后执行connect()方法。
方法2.通过UUID获得BluetoothSocket,然后先根据mDevice.getBondState()进行判断是否需要配对,最后执行connnect()方法。
- private class ConnectThread extends Thread {
- String macAddress = "";
- public ConnectThread(String mac) {
- macAddress = mac;
- }
- public void run() {
- connecting = true;
- connected = false;
- if(mBluetoothAdapter == null){
- mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- }
- mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(macAddress);
- mBluetoothAdapter.cancelDiscovery();
- try {
- socket = mBluetoothDevice.createRfcommSocketToServiceRecord(uuid);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- //e.printStackTrace();
- Log.e(TAG, "Socket", e);
- }
- //adapter.cancelDiscovery();
- while (!connected && connetTime <= 10) {
- connectDevice();
- }
- // 重置ConnectThread
- //synchronized (BluetoothService.this) {
- //ConnectThread = null;
- //}
- }
- public void cancel() {
- try {
- socket.close();
- socket = null;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- connecting = false;
- }
- }
- }
接下来是调用的连接设备方法connectDevice():
- protected void connectDevice() {
- try {
- // 连接建立之前的先配对
- if (mBluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) {
- Method creMethod = BluetoothDevice.class
- .getMethod("createBond");
- Log.e("TAG", "开始配对");
- creMethod.invoke(mBluetoothDevice);
- } else {
- }
- } catch (Exception e) {
- // TODO: handle exception
- //DisplayMessage("无法配对!");
- e.printStackTrace();
- }
- mBluetoothAdapter.cancelDiscovery();
- try {
- socket.connect();
- //DisplayMessage("连接成功!");
- //connetTime++;
- connected = true;
- } catch (IOException e) {
- // TODO: handle exception
- //DisplayMessage("连接失败!");
- connetTime++;
- connected = false;
- try {
- socket.close();
- socket = null;
- } catch (IOException e2) {
- // TODO: handle exception
- Log.e(TAG, "Cannot close connection when connection failed");
- }
- } finally {
- connecting = false;
- }
- }
方法3.利用反射通过端口获得BluetoothSocket,然后执行connect()方法。
- private class ConnectThread extends Thread {
- String macAddress = "";
- public ConnectThread(String mac) {
- macAddress = mac;
- }
- public void run() {
- connecting = true;
- connected = false;
- if(mBluetoothAdapter == null){
- mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- }
- mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(macAddress);
- mBluetoothAdapter.cancelDiscovery();
- initSocket();
- //adapter.cancelDiscovery();
- while (!connected && connetTime <= 10) {
- try {
- socket.connect();
- connected = true;
- } catch (IOException e1) {
- connetTime++;
- connected = false;
- // 关闭 socket
- try {
- socket.close();
- socket = null;
- } catch (IOException e2) {
- //TODO: handle exception
- Log.e(TAG, "Socket", e2);
- }
- } finally {
- connecting = false;
- }
- //connectDevice();
- }
- // 重置ConnectThread
- //synchronized (BluetoothService.this) {
- //ConnectThread = null;
- //}
- }
- public void cancel() {
- try {
- socket.close();
- socket = null;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- connecting = false;
- }
- }
- }
接下来是初始化并得到BluetoothSocket的方法
- /**
- * 取得BluetoothSocket
- */
- private void initSocket() {
- BluetoothSocket temp = null;
- try {
- Method m = mBluetoothDevice.getClass().getMethod(
- "createRfcommSocket", new Class[] { int.class });
- temp = (BluetoothSocket) m.invoke(mBluetoothDevice, 1);//这里端口为1
- } catch (SecurityException e) {
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- socket = temp;
- }
要点:1.蓝牙配对和连接是两回事,不可混为一谈。
2.蓝牙串口连接可通过端口 (1-30)和UUID两种方法进行操作。
3.通过UUID进行蓝牙连接最好先进行配对操作。
说明:在连接蓝牙过程中总是不稳定,先配对后,情况明显好转。
另外装载作者的另外一个补充,详细过程还没有试验
http://www.aiseminar.cn/bbs/forum.PHP?mod=viewthread&tid=2369&fromuid=3
如前文“第二个Activity中onStop设置Service为第一个Activity的handler为什么不起作用?”所描述的问题,开始以为是handle出了问题:“蓝牙使用UUID进行连接,第一设备之间可以正常连接。而socket关闭后,后续连接都会出现service discovery failed错误”。
后来查阅资料发现,这是Android蓝牙连接经常遇到的问题。有人怀疑是Android系统的bug,而且提出使用反射方法解决此问题。经过AIS蓝牙对讲机的使用测试结果表明:使用Reflect方法进行蓝牙连接,可以重复连接任意多次,不会再出现上述问题。
所谓使用反射方式,就是通过Java的反射机制,取出Bluetooth的监听和创建连接socket的方法,使用反射的方法进行操作,而再使用listenUsingRfcommWithServiceRecord和createRfcommSocketToServiceRecord这样的问题函数。实现时,同样如同这样连个函数,需要将反射操作配对使用。如下:
替换listenUsingRfcommWithServiceRecord的反射操作:
上面函数中的常量1为蓝牙端口号,据说:蓝牙串口连接可通过端口号:1-30。
Referred to:
[1] 第二个Activity中onStop设置Service为第一个Activity的handler为什么不起作用? http://www.aiseminar.cn/bbs/forum.php?mod=viewthread&tid=2314&fromuid=3
[2] java.io.IOException: Service discovery failed. http://stackoverflow.com/questions/17812222/java-io-ioexception-service-discovery-failed
[3] Android listen for connections without UUID. http://stackoverflow.com/questions/13362774/android-listen-for-connections-without-uuid
[4] android开发之蓝牙配对连接的方法. http://blog.csdn.Net/jason0539/article/details/17782035
另外两篇关于蓝牙的文章:
蓝牙开发:http://www.cnblogs.com/wenjiang/p/3200138.html