在BluetoothGatt中不调用onCharacteristicChanged

时间:2017-04-27 22:01:54

标签: android bluetooth-lowenergy gatt

我在做什么:

  1. 使用gatt.connect()将我的Android应用程序连接到设备。
  2. 连接后,我发现了所有的服务。
  3. 然后,我启用所有控制特性的通知。这是一次成功的写作。
  4. 之后,我试图写一些关于其中一个控制特性的数据,这是成功的。
  5. 预期的行为是我应该收到关于onCharacteristicChanged()的回复,但它没有触发。
  6. 连接:

    mBluetoothGatt = device.connectGatt(mContext, false, mGattCallback);
            mDevice = device;
    

    关贸总协定回拨

    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                Log.i(Constant.BLUETOOTH_GATT_TAG, "Status: " + status);
                super.onConnectionStateChange(gatt, status, newState);
                switch (newState) {
                    case STATE_CONNECTED:
                        Log.i("gattCallback", "STATE_CONNECTED");
                        gatt.discoverServices();
                        break;
                }
            }
    
            @Override
            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                super.onServicesDiscovered(gatt, status);
                services = gatt.getServices();
                for (BluetoothGattService service : services) {
                    Log.i("services Discovered", service.getUuid().toString());
                }
    
                BluetoothGattCharacteristic characteristic = gatt.getService(Constant.UUID_FRANK_SERVICE).getCharacteristic(Constant.UUID_ILLUMINATION_CONTROL_POINT_CHARACTERISTICS);
                Log.d("Notification Enabled", "" + enableNotificationsForCharacteristic(characteristic));
                //getAuthenticated();
            }
    
            @Override
            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                super.onCharacteristicChanged(gatt, characteristic);
                Log.d(CHARACTERISTICS_CHANGED_TAG, "Received from Characteristic UUID");
    
            }
    
            @Override
            public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicWrite(gatt, characteristic, status);
                Log.d("onCharacteristicWrite", "Value is written...");
            }
    
            @Override
            public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
                super.onDescriptorWrite(gatt, descriptor, status);
                Log.d("onDescriptorWrite", "Descriptior is written...");
                Log.d("onDescriptorWrite" , "" + mBluetoothGatt.readDescriptor(descriptor));
            }
    
            @Override
            public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
                super.onDescriptorRead(gatt, descriptor, status);
    
                Log.d("onDescriptorRead" , "" + status);
            }
    
            @Override
            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicRead(gatt, characteristic, status);
            }
        };
    

    启用通知:

    private boolean enableNotificationsForCharacteristic(BluetoothGattCharacteristic characteristic) {
            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            if (mBluetoothGatt != null) {
                mBluetoothGatt.setCharacteristicNotification(characteristic, true);
    
                if (mBluetoothGatt.writeDescriptor(descriptor)) {
                    return true;
                }
            }
            return false;
        }
    

    写作特色:

    private boolean writeToCharacteristic(BluetoothGattService service, BluetoothGattCharacteristic characteristic, byte[] aValue, boolean aWithResponse) {
            boolean ret = false;
    
            if (service != null) {
                if (characteristic != null) {
                    characteristic.setValue(aValue);
                    characteristic.setWriteType(aWithResponse ? BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT : BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
                    ret = mBluetoothGatt.writeCharacteristic(characteristic);
    
                } else {
                    Log.e(TAG, "writeToCharacteristic() - Characteristic is null! " + service + " - " + characteristic);
                }
            } else {
                Log.e(TAG, "writeToCharacteristic() - Service is null! " + service);
            }
            return ret;
        }
    

    我期待的是什么: 设置通知描述符后,我得到一个GATT_SUCCESS但是当我写一个特征时,它没有触发onCharacteristicChanged()。我希望它能够触发,以便我可以阅读通知响应。设备运行正常,因为我已使用Nordic Connect App确认通知

    我尝试了什么:

    1. 尝试验证描述符,看它是否正确写入。它确实。
    2. 交叉检查服务名称和特征。他们都很好。
    3. 尝试在写入描述符之前放置延迟。它写得很好。
    4. 任何人都可以帮助我吗?为什么它没有触发onCharacteristicChanged?

0 个答案:

没有答案