在Android BLE GATT服务中不调用onCharacteristicChanged

时间:2016-11-18 10:26:56

标签: android bluetooth-lowenergy android-bluetooth gatt android-service-binding

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

我能够在特征中编写数据,并能够成功启用特定服务的通知并返回 TRUE 。问题是 gatt.writeCharacteristic(特征)成功之后应该调用onCharacteristicChanged覆盖方法。但它没有称那种方法。 从该方法只有我能够从BLE设备接收数据。

我跟随以下网址

Android BLE

注意: 通过使用服务,我正在调用建立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);
                    }
                }
            }
        }


    }

1 个答案:

答案 0 :(得分:0)

没有。在为特征写入值后,不会调用onCharacteristicChanged。 onCharacteristicWrite被调用。

在收到通知或指示时调用onCharacteristicChanged。