我正在编写一个与BLE仪表通话的Android应用程序。我已经能够扫描设备,连接到目标,发现服务并获得特征。但是,当我尝试编写write_no_reponse特性时,该方法始终返回false。我确信特性是可写的,我使用其他应用程序来做到这一点。当我调试到android.bluetooth代码时,以下序列发生故障,可能返回空服务,调试运行到这一步,然后退出该方法:
BluetoothGattService service = characteristic.getService();
导致writeCharacteristic返回false。 但是在writeCharacteristic之前,我有一个日志
Log.d(TAG, "onServicesDiscovered: writeType" + mCharacteristic.getService());
bluetoothGatt.writeCharacteristic(mCharacteristic);
并且日志正确,并且不返回null;
非常感谢您的帮助!
而且我使用相同的代码来写WRITE特征,它起作用了。 我尝试使用
mCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
设置写入类型,但也失败。
写入数据代码:
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");
}
}
}
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);
}
}
并且onCharacteristicWrite回调不会触发
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.d(TAG, "onCharacteristicWrite: " + Arrays.toString(characteristic.getValue()));
}
我很久没有这个bug了,非常感谢有人帮忙。
答案 0 :(得分:0)
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID));
这段代码找到0特征,然后我向它写入数据。很明显,我失败了。 因为0特性只能读取和通知。 因此,我按照以下步骤将日期写入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");
}
}
}
我尝试通知这两个特征并将数据写入这两个特征,尽管它们具有相同的UUID,但它们具有不同的功能。显然,一个特征将通知失败,一个特征将写入失败。但最后我成功写入了数据。
答案 1 :(得分:0)
在 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);
}