简体   繁体   English

bluetoothGatt.writeCharacteristic 总是返回 false,ble 特征类型为 write_no_reponse

[英]bluetoothGatt.writeCharacteristic always return false with ble characteristic type is write_no_reponse

I am writing an Android app to talk with an BLE meter.我正在编写一个 Android 应用程序来与 BLE 仪表通话。 I have been able to scan devices, connect to the target, discover services, get characteristics.我已经能够扫描设备、连接到目标、发现服务、获取特征。 However, when I try to write a write_no_reponse characteristics the method always returns false.但是,当我尝试编写 write_no_reponse 特性时,该方法总是返回 false。 I am sure the characteristics is writeable, i use others app do it.我确信这些特征是可写的,我使用其他应用程序来完成。 When I debugged into the android.bluetooth code, the following sequence occurs failure, maybe it return a null service, debug run to this step and then step out the method:当我调试进入android.bluetooth代码时,出现以下序列失败,可能是返回了一个空服务,调试运行到这一步然后退出方法:

BluetoothGattService service = characteristic.getService(); 

which causes the writeCharacteristic return false.这会导致 writeCharacteristic 返回 false。 But before writeCharacteristic, I have a log但是在writeCharacteristic之前,我有一个日志

Log.d(TAG, "onServicesDiscovered: writeType" + mCharacteristic.getService());
bluetoothGatt.writeCharacteristic(mCharacteristic);

and the log is correct and does not return null;并且日志正确且不返回空值;

Any help is greatly appreciated!任何帮助是极大的赞赏!

And I use the same code to write to a WRITE characteristics, it worked.而且我使用相同的代码来写入 WRITE 特性,它起作用了。 And I try use the我尝试使用

mCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);

to set the write type, but it also failed.设置写入类型,但它也失败了。

the write data code:写入数据代码:

public void writeReadDataCommand() {
    if (null != bluetoothGatt) {
        mCharacteristic.setValue("123123");
        bluetoothGatt.setCharacteristicNotification(mCharacteristic, true);
        mCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
        Log.d(TAG, "onServicesDiscovered: writeType" + mCharacteristic.getService());
        boolean isWrite = bluetoothGatt.writeCharacteristic(mCharacteristic);
        if (isWrite) {
            Log.d(TAG, "writeReadDataCommand: write data to BLE");
        }
    }
}

the onServiceDiscovered code: onServiceDiscovered 代码:

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    super.onServicesDiscovered(gatt, status);

    if (status == BluetoothGatt.GATT_SUCCESS) {
        bluetoothGatt = gatt;
        BluetoothGattService service = gatt.getService(UUID.fromString(SERVICE_UUID));
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID));
        Log.d(TAG, "onServicesDiscovered: writeType" + characteristic.getWriteType());

        boolean isSuccess = gatt.setCharacteristicNotification(characteristic, true);
        if (isSuccess) {
            mCharacteristic = characteristic;
            List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
            if (null != descriptors && descriptors.size() > 0) {
                for (BluetoothGattDescriptor descriptor : descriptors) {
                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                    gatt.writeDescriptor(descriptor);
                }
            }
        }
        connectCallback.findService();
    } else {
        Log.d(TAG, "onServicesDiscovered received: " + status);
    }
}

and the onCharacteristicWrite callback does not trigger并且 onCharacteristicWrite 回调不会触发

public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    Log.d(TAG, "onCharacteristicWrite: " + Arrays.toString(characteristic.getValue()));
}

I have this bug for a long time, thanks so much if someone could help.我有这个错误很长时间了,如果有人可以提供帮助,非常感谢。

characteristics特征

as the pic can see, I have 2 characteristic in my service -- 0 and 1. The characteristics have the same uuid.如图所示,我的服务中有 2 个特征——0 和 1。这些特征具有相同的 uuid。 The first I do is我做的第一件事是

BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID));

This code find the 0 characteristic, and I write data to it.Obviously, I failed.这段代码找到了0的特征,我向它写入数据。显然,我失败了。 Because the 0 characteristic can only READ and NOTIFY.因为0特性只能READ和NOTIFY。 So I do the follow step to write date to BLE所以我按照以下步骤将日期写入 BLE

                characteristics = service.getCharacteristics();
                for(BluetoothGattCharacteristic characteristic : characteristics){
                    if(characteristic.getUuid().equals(UUID.fromString(CHARACTERISTIC_UUID))){
                        enableNotification(characteristic);
                    }
                }
            for(BluetoothGattCharacteristic characteristic : characteristics){
                if(characteristic.getUuid().equals(UUID.fromString(CHARACTERISTIC_UUID))){
                    characteristic.setValue("123123");
                    characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);

                    boolean isWrite = bluetoothGatt.writeCharacteristic(characteristic);
                    if (isWrite) {
                        Log.d(TAG, "writeReadDataCommand: write data to ble");
                    }
                }
            }

I try notify the two characteristics and write data to the two characteristics, althoght they have the same UUID, they have different function.我尝试通知这两个特征并将数据写入这两个特征,尽管它们具有相同的 UUID,但它们具有不同的功能。 Obviously, one characteristic will notify failed, one will write failed.很明显,一个特性会通知失败,一个会写失败。 But finally I successful write my data.但最后我成功地写了我的数据。

After onDescriptorWrite() send Data to BLE Device.在 onDescriptorWrite() 将数据发送到 BLE 设备之后。

 private final BluetoothGattCallback getGattCallback1=new BluetoothGattCallback() {
        @Override
        public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyUpdate(gatt, txPhy, rxPhy, status);
        }

        @Override
        public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyRead(gatt, txPhy, rxPhy, status);
        }

        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
        }

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

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

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);
            // here send broadcast to MainActivity.That u are ready to write Data To the device.
        }

        @Override
        public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
            super.onReliableWriteCompleted(gatt, status);
        }

        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            super.onReadRemoteRssi(gatt, rssi, status);
        }

        @Override
        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
            super.onMtuChanged(gatt, mtu, status);
        }
    };

 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void setCharacteristicNotifications(BluetoothGatt bluetoothGatt,BluetoothGattCharacteristic characteristic,
                                               boolean enabled,String bleAddress) {
        if (mBluetoothAdapter == null || bluetoothGatt == null) {
            return;
        }
        bluetoothGatt.setCharacteristicNotification(characteristic, enabled);
        BluetoothGattDescriptor descriptor = null;
        descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG));
        if (descriptor == null) {
            enableNotiticationToFirmwareCompleted(false,bleAddress);
            return;
        }
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        bluetoothGatt.writeDescriptor(descriptor);

     }

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

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