繁体   English   中英

BLE oncharacteristicistic已更改未在android 4.4和5中调用

[英]BLE oncharacteristicchanged not called in android 4.4 and 5

我有一个程序可以发送单词并获取一些数据。 在Android 6和7中效果很好。 但在Android 4.4和5中,在writeCharacteristic之后未调用oncharacteristicchanged。 我在启用通知和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");

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

因此,我假设您是否已经在使用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权限。为此,请参阅Google示例代码。

暂无
暂无

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

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