繁体   English   中英

在Android BLE GATT服务中未调用onCharacteristicChanged

[英]onCharacteristicChanged is not called in Android BLE GATT servcies

我正在尝试从我的应用程序中的BLE设备接收数据。 我能够将BLE设备成功连接到我的应用程序,并且能够找到BLE设备提供的服务。

我能够在特征中写入数据,并且能够成功地启用特定服务的通知及其返回的TRUE 问题是gatt.writeCharacteristic(characteristic)成功执行后,应调用onCharacteristicChanged重写方法。 但是它没有调用该方法。 从这种方法中,我只能从BLE设备接收数据。

我跟随以下网址

Android BLE

注意:通过使用service,我正在调用建立GATT连接。

 private class BleGattCallback extends BluetoothGattCallback {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

            switch (newState) {
                case BluetoothProfile.STATE_CONNECTED:

                     gatt.discoverServices();

                    break;

                case BluetoothProfile.STATE_DISCONNECTED:
                    Log.d(TAG, "BluetoothProfile.STATE_DISCONNECTED");
                    gatt.close();
                    break;

                default:
                    break;
            }
        }


        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            Log.d(TAG, "onServicesDiscovered");

            if (status == BluetoothGatt.GATT_SUCCESS) {
                BluetoothGattService service = gatt.getService(SERVICE_UUID);
                if (service != null) {

                    BluetoothGattCharacteristic characteristic = service.getCharacteristic(READING_UUID);
                    if (characteristic != null) {
                        Log.d(TAG, " Subscribe Characteristic Notification UUID :  "+characteristic.getUuid());

                        gatt.setCharacteristicNotification(characteristic, true);

                        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UUID);
                        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

                        boolean success = gatt.writeDescriptor(descriptor);


                        Log.d(TAG, "writeDescriptor Status : " + success);
                    }
                }
            }
        }


        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);

            Toast.makeText(mContext,"onCharacteristicChanged",Toast.LENGTH_LONG).show();
            // read Value
        }



        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);


            if (status == BluetoothGatt.GATT_SUCCESS) {

                BluetoothGattService service = gatt.getService(SERVICE_UUID);
                if (service != null) {
                    BluetoothGattCharacteristic characteristic = service.getCharacteristic(INDEX_UUID);
                    Log.d(TAG, "onDescriptorWrite  success UUID for "+characteristic.getUuid());
                    if (characteristic != null) {
                        characteristic.setValue(new byte[] {0x03, 0x00});
                        gatt.writeCharacteristic(characteristic);
                    }
                }
            }
        }


    }

否。将值写入特征后,不会调用onCharacteristicChanged。 调用onCharacteristicWrite。

当收到通知或指示时,将调用onCharacteristicChanged。

暂无
暂无

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

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