无法从BLE设备读取数据,永远不会调用onCharacteristicChanged()

时间:2019-08-23 17:09:59

标签: java android arduino bluetooth bluetooth-lowenergy

我一直在写一个工具来从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()都不会被召唤。有人知道为什么吗?

0 个答案:

没有答案