在Android 4.4和5中没有调用BLE oncharacteristicchanged

时间:2018-03-13 19:37:11

标签: android bluetooth-lowenergy

我有一个程序可以发送一个单词并获取一些数据。在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);
uuids是:

    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");

这是logcat

    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。请帮帮我......

1 个答案:

答案 0 :(得分:0)

所以我假设您是否已经在使用git repo中的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。 确保您同时在清单中以及在运行时针对6.0和7动态请求ACCESS_COARSE_LOCATION / ACCESS_FINE_LOCATION权限。