设备与安卓设备自动断开连接。安卓BLE

11

我正在使用安卓Nexus 7通过蓝牙低功耗链接来连接设备。如果我不与设备进行通信,我可以连接设备并保持连接。

然而,如果我通过点击按钮启用一个特定特征的通知,那么在几秒钟的数据传输后,设备将会与平板电脑断开连接。

有人知道问题可能是什么吗?非常感谢!

这是我的代码:

    public boolean setCharacteristicNotification(boolean enabled){

      if (mBluetoothAdapter == null || mBluetoothGatt == null) {
          Log.w(TAG, "BluetoothAdapter not initialized");
               return false;      
      }

      BluetoothGattService Service = mBluetoothGatt.getService(UUID_MY_SERVICE);
      if (Service == null) {
          Log.e(TAG, "service not found!");
          return false;
      }

      BluetoothGattCharacteristic characteristic = Service.getCharacteristic(UUID_MY_CHARACTERISTIC);

      final int charaProp = characteristic.getProperties();

      if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
          mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                    UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 

            mBluetoothGatt.writeDescriptor(descriptor);

          return true;
      }

    return false;

}
2个回答

4

(在问题编辑中回答。转换为社区维基答案。请参见当问题的答案添加到问题本身时,适当的操作是什么?)

原帖作者写道:

我今天解决了这个问题。

只需更改 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);

跟进:

经过一些研究和测试,我发现Nexus 7上的蓝牙和WIFI之间的干扰与自动断开连接的问题有关。如果我关闭WIFI,则蓝牙的断开连接问题就消失了。而且这个问题在Galaxy 3,4,5上没有出现。


@Brain:在应用这个特性之前,它会持续通知一段时间,然后自行断开连接。但是在更改之后,即使只有一个特性也不会再通知一次,并且有时会出现相同的断开连接情况。 - Mehul Kabaria
@Brain:我已经尝试使用Android应用程序连接Mac,但是它无法连接,每次状态都会断开连接,状态代码为133。但设备已经配对。 - sameer

0
问题:我在Tesco Hudl 2上遇到了同样的问题,如果我一连接蓝牙就传输数据,它就会断开连接。

解决方案:连接后等待几秒钟,似乎可以正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接