首页 > 解决方案 > 无法从 BLE 设备读取数据,永远不会调用 onCharacteristicChanged()

问题描述

我一直在编写一个工具来从我的 Arduino 设备接收字节。我尝试了以下 BluetoothGatt 回调类,它可以很快连接到设备但无法接收任何数据。onCharacteristicChanged() 从未被调用过。

我在android上使用了其他BLE串口调试应用程序来确保特征UUID“0000fff6-0000-1000-8000-00805f9b34fb”可以接收数据。但在我的代码中,它不能。

这是来自 Android Studio 的日志:

E/BluetoothGatt: STATE_CONNECTED
D/BluetoothGatt: discoverServices() - device: 3C:A3:08:8B:44:B9
D/BluetoothGatt: onConnectionUpdated() - Device=3C:A3:08:8B:44:B9 interval=6 latency=0 timeout=500 status=0
D/BluetoothGatt: onSearchComplete() = Device=3C:A3:08:8B:44:B9 Status=0
I/Service UUID: 00001801-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a05-0000-1000-8000-00805f9b34fb enable: true
I/Service UUID: 0000fff0-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: setCharacteristicNotification() - uuid: 0000fff4-0000-1000-8000-00805f9b34fb enable: true
I/Service UUID: 0000fff0-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: setCharacteristicNotification() - uuid: 0000fff6-0000-1000-8000-00805f9b34fb enable: true
D/BluetoothGatt: onConnectionUpdated() - Device=3C:A3:08:8B:44:B9 interval=36 latency=0 timeout=500 status=0
D/BluetoothGatt: onConnectionUpdated() - Device=3C:A3:08:8B:44:B9 interval=30 latency=0 timeout=100 status=0

在此之后,当我的 Arduino 设备不断发送数据时,没有新的日志进入。

    public int connectDevice(){
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        device = bluetoothAdapter.getRemoteDevice(macAddress);
        if (!bluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 1);
        }
         bluetoothGatt = device.connectGatt(this, false, new BluetoothGattCallback() {
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                super.onConnectionStateChange(gatt, status, newState);
                Log.e("BluetoothGatt", "Connecting state:" + newState);
                if (newState == BluetoothGatt.STATE_CONNECTED) {
                    Log.e("BluetoothGatt", "STATE_CONNECTED");
                    bluetoothGatt = gatt;
                    gatt.discoverServices();
                }
                else if(newState == BluetoothGatt.STATE_CONNECTING){
                    Log.e("BluetoothGatt", "Connecting");
                }

            }

            @Override
            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                super.onServicesDiscovered(gatt, status);
                List<BluetoothGattService> services = gatt.getServices();
                for (BluetoothGattService service : services) {
                    List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
                    for (BluetoothGattCharacteristic character : characteristics) {
                        enableNotification(gatt, service.getUuid(), character.getUuid());
                    }
                }
            }

             @Override
             public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                 super.onCharacteristicChanged(gatt, characteristic);
                 byte[] value = characteristic.getValue();
                 Log.i("BLE", "receive value ----------------------------");
                 for (int i = 0; i < value.length; i++) {
                     Log.w("BLE", "character_value = " + value[i]);
                 }
             }
        });

        return 0;
    }

    public boolean enableNotification(BluetoothGatt gatt, UUID serviceUUID, UUID characteristicUUID) {
        boolean success = false;
        BluetoothGattService service = gatt.getService(serviceUUID);
        if (service != null) {
            BluetoothGattCharacteristic characteristic = findNotifyCharacteristic(service, characteristicUUID);
            if (characteristic != null) {
                Log.i("Service UUID", serviceUUID.toString());
                success = gatt.setCharacteristicNotification(characteristic, true);
                if (success) {

                    for (BluetoothGattDescriptor dp : characteristic.getDescriptors()) {
                        if (dp != null) {
                            if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
                                dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                            } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
                                dp.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                            }
// I just commented this line but still not working                 
// dp.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); 
                            gatt.writeDescriptor(dp);
                            gatt.readCharacteristic(characteristic);
                        }
                    }
                }
            }
        }
        return success;
    }

    private BluetoothGattCharacteristic findNotifyCharacteristic(BluetoothGattService service, UUID characteristicUUID) {
        BluetoothGattCharacteristic characteristic = null;
        List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
        for (BluetoothGattCharacteristic c : characteristics) {
            if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0
                    && characteristicUUID.equals(c.getUuid())) {
                characteristic = c;
                break;
            }
        }
        if (characteristic != null)
            return characteristic;
        for (BluetoothGattCharacteristic c : characteristics) {
            if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0
                    && characteristicUUID.equals(c.getUuid())) {
                characteristic = c;
                break;
            }
        }
        return characteristic;
    }

无论我做什么,onCharacteristicChanged()都不会被调用。有谁知道为什么?

标签: javaandroidarduinobluetoothbluetooth-lowenergy

解决方案


推荐阅读