在尝试了修改uuid,新建子线程得方法后均失败后,通过反射机制发现将端口号改为2时连接成功,分享给小伙伴 try { // MY_UUID是应用程序的UUID,客户端代码使用相同的UUID //tmp = device.createRfcommSocketToServiceRecord(MY_UUID); //bluetoothSocket的端口号为2时蓝牙连接成功 tmp = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(mmDevice,2); } catch (Exception e) { } mmSocket = tmp; //mmSocket =(BluetoothSocket) mmDevice.getClass("createRfcommSocket", new Class[] {int.class}).invoke(mmDevice,1); } public void run() { new Thread() { public void run() { mBluetoothAdapter.cancelDiscovery(); try { // 通过socket连接设备,阻塞运行直到成功或抛出异常时 mmSocket.connect(); Log.e("eeeeeeeeeee", "Bluetooth connection!"); } catch (Exception connectException) { mHandler.sendMessage(mHandler.obtainMessage(Constant.MSG_ERROR, connectException)); // 如果无法连接则关闭socket并退出 try { Log.e("wwwwwwwwwwwwww", "Couldn't establish Bluetooth connection!"); mmSocket.close(); } catch (IOException closeException) { } return; } } }.start(); }
02-28
2313
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-10
8406
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-27
1133
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-27
1960
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)