【问题标题】:BLE oncharacteristicchanged not called in android 4.4 and 5在 android 4.4 和 5 中未调用 BLE oncharacteristicchanged
【发布时间】:2018-03-13 19:37:11
【问题描述】:

我有一个程序可以向 ble 发送一个单词并获取一些数据。在 android 6 和 7 中运行良好。但在 android 4.4 和 5 中,oncharacteristicchanged 在 writeCharacteristic 之后没有被调用。我在启用通知和 writeCharacteristic 之间添加了一个延迟,但没有奏效。 启用通知代码:

    mBluetoothGatt.setCharacteristicNotification(TxChar,true);
    BluetoothGattDescriptor descriptor = TxChar.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);

uuid 是:

    CCCD            = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
    RX_SERVICE_UUID = UUID.fromString("00001234-0000-1000-8000-00805f9b34fb");
    RX_CHAR_UUID    = UUID.fromString("00001235-0000-1000-8000-00805f9b34fb");
    TX_CHAR_UUID    = UUID.fromString("00001236-0000-1000-8000-00805f9b34fb");

这是日志猫

    03-14 11:29:10.024 11767-12038/com.example.metasense D/BluetoothGatt: onGetCharacteristic() - Device=E3:84:5F:81:55:87 UUID=00001236-0000-1000-8000-00805f9b34fb
03-14 11:29:10.029 11767-11779/com.example.metasense D/BluetoothGatt: onGetDescriptor() - Device=E3:84:5F:81:55:87 UUID=00002902-0000-1000-8000-00805f9b34fb
03-14 11:29:10.029 11767-11778/com.example.metasense D/BluetoothGatt: onSearchComplete() = Device=E3:84:5F:81:55:87 Status=0
03-14 11:29:10.029 11767-11778/com.example.metasense D/onServicesDiscovered bt: onServicesDiscovered
03-14 11:29:10.029 11767-11778/com.example.metasense D/BluetoothService: mBluetoothGatt = android.bluetooth.BluetoothGatt@42c448d8
03-14 11:29:10.029 11767-11778/com.example.metasense D/enableTXNotification bt: enableTXNotification
03-14 11:29:10.029 11767-11778/com.example.metasense D/BluetoothGatt: setCharacteristicNotification() - uuid: 00001236-0000-1000-8000-00805f9b34fb enable: true
03-14 11:29:10.029 11767-11778/com.example.metasense D/BluetoothGatt: writeDescriptor() - uuid: 00002902-0000-1000-8000-00805f9b34fb
03-14 11:29:10.334 11767-11778/com.example.metasense D/writeRXChar bt: writeRXCharacteristic
03-14 11:29:10.334 11767-12258/com.example.metasense D/BluetoothService: mBluetoothGatt null (1/1) android.bluetooth.BluetoothGatt@42c448d8
03-14 11:29:10.334 11767-12258/com.example.metasense D/BluetoothGatt: writeCharacteristic() - uuid: 00001235-0000-1000-8000-00805f9b34fb
03-14 11:29:10.334 11767-12258/com.example.metasense D/BluetoothService: All1packs sent.
03-14 11:29:10.394 11767-12258/com.example.metasense D/akhare write bt: inja
03-14 11:29:10.404 11767-12038/com.example.metasense D/BluetoothGatt: onDescriptorWrite() - Device=E3:84:5F:81:55:87 UUID=00001236-0000-1000-8000-00805f9b34fb
03-14 11:29:13.454 11767-11794/com.example.metasense V/FA: Inactivity, disconnecting from the service

蓝牙模块是 nRF51822。请帮帮我...

【问题讨论】:

  • 您需要添加更多详细信息。我们不知道为什么您的外围设备没有发送预期的数据。你有没有调试过任何东西。比如检查外设日志、嗅探器跟踪、hci 日志或 logcat?
  • 谢谢。我添加了 logcat

标签: android bluetooth-lowenergy


【解决方案1】:

所以我假设您是否已经在使用 git 存储库中的 Nordic UartService.java 示例代码。在您的 UartService.java 中执行此操作。 由于某些奇怪的原因,Android 在读取可用数据时不稳定,我通过在不同位置多次读取来设法获得可靠的读取。

这将在 UartService.java 中的 gatt 回调方法中

@Override
public void onCharacteristicRead(BluetoothGatt gatt,  
                                         BluetoothGattCharacteristic characteristic,
                                         int status) {

super.onCharacteristicRead(gatt, characteristic, status);


//Check to verify whether gatt is successful and you're reading the right characterstic

if (status == BluetoothGatt.GATT_SUCCESS &&
                    characteristic.getUuid().equals(TX_CHAR_UUID)) {

                //Your byte[] will be extracted here if available
                byte[] data = characteristic.getValue();

                //Update to tell data is available
                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);

                //No data
                if (data == null) {
                    Log.e(TAG, "onCharacteristicRead: data = null ERROR???");

                    broadcastUpdate(ACTION_GATT_DISCONNECTED);

                    return;
                }


            } else {
                Log.w(TAG, "onCharacteristicRead" + status);
            }




            }

在同一个UartService.java中添加如下方法

public void readData(BluetoothGatt gatt, BluetoothGattCharacteristic c) {
        gatt.readCharacteristic(c);
    }

在您的 enableTXNotification 中执行此操作

mBluetoothGatt.setCharacteristicNotification(TxChar,true);
BluetoothGattDescriptor descriptor = TxChar.getDescriptor(CCCD);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);


//Delay based on recommendations from various blog posts 

        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


//Now call readData method
        readData(mBluetoothGatt,TxChar);

尽管 Android 和 nRF51822 模块都存在低效率,但多次读取帮助我捕获数据,即使它第一次失败/错过。试试这个,让我知道它是否有帮助。这适用于我的 Android OS 5.1、6.0 和 7。 确保在清单中请求 ACCESS_COARSE_LOCATION/ACCESS_FINE_LOCATION 权限,并在 6.0 和 7 的运行时动态请求。请参阅 google 示例代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多