Android ble 连接不上问题

来源:互联网 发布:贵阳广电网络营业厅 编辑:程序博客网 时间:2024/06/05 00:18

问题:

手机作为ble服务器在发现客户端后主动连接,连接失败。

现象:

在调用bluetoothDevice.connectgatt(BluetoothDevice,autoconnect,BluetoothGattCallback); 后,在BluetoothGattCallback中的onConnectionStateChange(BluetoothGatt gatt , int status , int newState)回调中 newState状态为 BluetoothProfile.STATE_DISCONNECTED

解决方式:

在onConnectionStateChange(BluetoothGatt gatt , int status , int newState)回调中打印第二个参数(status)的值。

值为0:直接调用gatt.connect()重新连接。

值不为0(值为133/257等): ①首先执行gatt.close()清除连接;

      ②然后重新调用bluetoothDevice.connectgatt(BluetoothDevice,autoconnect,BluetoothGattCallback); 建立连接

原因分析:

值为0:不确定因素导致连接失败。不确定因素可能为信号太弱等。

值不为0:由于协议栈原因导致连接建立失败。所以清除掉连接后重新建立连接。

0 0
原创粉丝点击