【问题标题】:BLE Device disconnect with Android device automatically. Android BLEBLE 设备自动与 Android 设备断开连接。安卓低功耗蓝牙
【发布时间】:2014-05-05 18:20:58
【问题描述】:

我正在使用 Android 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;

}

【问题讨论】:

    标签: android bluetooth bluetooth-lowenergy android-4.4-kitkat android-4.3-jelly-bean


    【解决方案1】:

    (在问题编辑中回答。转换为社区 wiki 答案。参见What is the appropriate action when the answer to a question is added to the question itself?

    OP 写道:

    我今天解决了这个问题。

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

    descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);

    跟进:

    经过一番研究和测试,我发现自动断线问题与Nexus 7上蓝牙和WIFI的干扰有关。如果我关闭了WIFI,那么蓝牙的断线问题就消失了。而在 Galaxy 3,4,5 上并没有出现这个问题。

    【讨论】:

    • @Brain :在应用此之前,每个特征都会通知继续一段时间,然后它会自行断开连接。更改此甚至单个特征后,不会通知单个时间并在同一时间断开连接。
    • @Brain :我尝试过从 android 应用程序连接 mac。但是它没有连接,并且每次状态都会断开连接。状态码将为 133。但设备将配对。
    【解决方案2】:

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

    解决方法:等几个 连接后几秒钟,它似乎工作正常。

    【讨论】:

      猜你喜欢
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 2021-04-09
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多