2个Android蓝牙无法连接的原因和解决方法

错误为: RFCOMM_CreateConnection - already opened state:2, RFC state:4, MCB state:5

原因:socket没有关闭。即使使用了代码socket.close().但是硬件需要时间反应。

解决方法:在socket.close();添加       SystemClock.sleep(POST_RESET_DELAY);等待关闭socket。POST_RESET_DELAY的值可设置为1000,也就是1秒。一秒应当足够了。


另外有时候蓝牙还是无法连接,说socket为空,代码如下:

if(mSocket==null) {
BluetoothCon.this.start();
return;
}
//Sleep time of 1000ms after closing the socket
SystemClock.sleep(POST_RESET_DELAY);
mSocket.connect(); //有一次执行connect的时候 ,显示没有源码。mSocket为null。虽然前面判断了mSocket==null

原因是是使用SystemClock.sleep(POST_RESET_DELAY);之前socket不为空,但是之后由于线程问题,socket为空。

所以使用socket之前要先判断下socket是否为空,如果为空就重新连接。

下面是修改后的代码:

//Sleep time of 1000ms after closing the socket
SystemClock.sleep(POST_RESET_DELAY);
       
if(mSocket!=null) {
mSocket.connect();
}
else
{
BluetoothCon.this.start();//BluetoothCon中有代码重新为socket赋值
return;
}

另api level 14有函数isConnected()可以判断是否已经连接。

参考资料:

http://stackoverflow.com/questions/7888294/rfcomm-createconnection-already-opened-state2-rfc-state4-mcb-state5?lq=1

Solution

Starting from API Level 14 there is a Method in BluetoothSocket called isConected(), which returns true, if this socket is already connected and false otherwise, here the original excerpt from the API:

Get the connection status of this socket, ie, whether there is an active connection with remote device.

For API levels < 14 you can work around this issue by putting your Bluetooth Handling Thread to sleep after closing the connection - 1000 ms should be enough, here is an example (btDevice is of the type BluetoothDevice and has been initialized prior to the code snippet below):

    try {
        //Open the socket to an SPP device (UUID taken from Android API for createRfcommSocketToServiceRecord)
        BluetoothSocket btSocket = btDevice.createRfcommSocketToServiceRecord("00001101-0000-1000-8000-00805F9B34FB");
        //Connect to the socket
        btSocket.connect();
        //Close the socket
        btSocket.close();
        //Sleep time of 1000ms after closing the socket
        SystemClock.sleep(POST_RESET_DELAY);

    } catch (Throwable e) {
        // Log error message
    }


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力帮助你解决这个问题。 首先,你需要了解的是,Android上的连接通常分为两个部分:一个是客户端,一个是服务端。服务端将作为设备广播自己的存在,并等待客户端连接。客户端将搜索周围的设备并尝试连接到服务端。 下面是一个简单的示例代码来创建一个服务端,在AndroidManifest.xml文件中,需要添加权限和使用的特性: ``` <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> ``` 在Java代码中,你需要得到BluetoothAdapter的实例,这可以通过调用getDefaultAdapter()方法来完成。接下来,你需要获取一个BluetoothServerSocket的实例,并将其绑定到一个UUID,以便客户端可以连接。该UUID需要与客户端使用的UUID相匹配。 ``` BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothServerSocket serverSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord("BluetoothExample", UUID.fromString("UUID here")); ``` 现在,你已经准备好将应用程序作为设备进行广播,并等待客户端连接了。在另一个线程中等待连接,并接受客户端的连接请求。 ``` BluetoothSocket socket = serverSocket.accept(); ``` 现在你已经成功地与客户端建立了一个BluetoothSocket对象,你可以使用该对象来发送和接收数据。 关于客户端的实现,你需要使用BluetoothAdapter开始发现周围的设备,并尝试连接到服务端。以下是一个简单的示例代码,你可以参考它: ``` BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); bluetoothAdapter.startDiscovery(); Set<BluetoothDevice> discoveredDevices = bluetoothAdapter.getBondedDevices(); if(discoveredDevices.size() > 0) { for(BluetoothDevice device : discoveredDevices){ if(device.getName().equals("BluetoothExample")){ BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("UUID here")); socket.connect(); } } } ``` 现在,你已经成功连接到服务端,并可以使用BluetoothSocket对象来发送和接收数据。 希望这个简单的示例可以帮助你编写一个连接Android程序。如果你需要更深入的了解,可以查看官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值