简体   繁体   English

BLE设备自动与Android设备断开连接。 Android BLE

[英]BLE Device disconnect with Android device automatically. Android BLE

I'm using Android Nexus 7 to connect a device via Bluetooth Low Energy link. 我正在使用Android Nexus 7通过Bluetooth Low Energy链接连接设备。 I'm able to connect the device, and stay connected if I don't do any communication with the device. 如果我不与设备进行任何通信,我可以连接设备并保持连接。

However, if I enable the notification of one specific characteristic by clicking a button, then the device would disconnect with the tablet after a few seconds' data transmission. 但是,如果我通过单击按钮启用一个特定特征的通知,则设备将在数秒传输数据后与平板电脑断开连接。

Does anyone know what might be the problem? 有谁知道可能是什么问题? Thank you very much! 非常感谢你!

Here's my code: 这是我的代码:

    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;

}

(Answered in a question edit. Converted to a community wiki answer. See What is the appropriate action when the answer to a question is added to the question itself? ) (在问题编辑中回答。转换为社区维基答案。请参阅问题答案添加到问题本身时,适当的操作是什么?

The OP wrote: OP写道:

I solved this problem today. 我今天解决了这个问题。

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

to descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);

Follow Up: 跟进:

After I did some research and testing, I found that the automatically disconnection problem has something to do with the interference between Bluetooth and WIFI on Nexus 7. If I turned off the WIFI, then the disconnection problem of Bluetooth has gone. 在我做了一些研究和测试之后,我发现自动断线问题与Nexus 7上的蓝牙和WIFI之间的干扰有关。如果我关闭了WIFI,那么蓝牙的断开问题已经消失。 And this problem did not occur on Galaxy 3,4,5. 并且这个问题在Galaxy 3,4,5上没有发生。

Problem: I was having same problem on Tesco Hudl 2, if i transmit some data soon as Bluetooth is connected, it will disconnect. 问题:我在Tesco Hudl 2上遇到了同样的问题,如果我在连接蓝牙后立即传输一些数据,它将断开连接。

Solution: Wait for few seconds after connection, it seems to work okay. 解决方案:连接后等待几秒钟,似乎工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM