Android BluetoothGatt writeCharacteristic 有响应
Android BluetoothGatt writeCharacteristic with response
从文档中我看到如何在没有响应的情况下这样写:
BluetoothGattCharacteristic characteristic = ...
characteristic.setValue(bytes);
mBluetoothGatt.writeCharacteristic(characteristic);
如何执行带有响应的写入请求操作?
(在 iOS 中有选项 select 写入类型 CBCharacteristicWriteWithResponse 和 CBCharacteristicWriteWithoutResponse)
- 你的特征应该是可写的。
检查特征是否可写:
(characteristic.getProperties() & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE))) != 0
- BluetoothGatt.writeCharacteristic() returns 布尔标志,表示写入是否成功(成功则为真)。
- 一旦 BluetoothGatt.writeCharacteristic 成功 BluetoothGattCallback.onCharacteristicWrite() 回调将使用具有您指定值的特征执行。
来自文档:
public void setWriteType (int writeType)
Added in API level 18 Set the write type for this characteristic
Setting the write type of a characteristic determines how the
writeCharacteristic(BluetoothGattCharacteristic) function write this
characteristic.
Parameters writeType The write type to for this characteristic. Can be
one of: WRITE_TYPE_DEFAULT, WRITE_TYPE_NO_RESPONSE or
WRITE_TYPE_SIGNED.
是的,我没有提到它。但是,请注意,应用不同的写入类型是在您写入值的特征必须是可写的条件下工作的,这是蓝牙设备特定的逻辑。首先找出您的特征是什么类型,因为它可能与您需要的类型相同。一旦它满足可写条件,每个 WRITE_TYPE 都应该适合你。
通过分析BluetoothGatt的writeCharacteristic()方法清晰可见:
public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
&& (characteristic.getProperties() &
BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false;
if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid());
if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false;
BluetoothGattService service = characteristic.getService();
if (service == null) return false;
BluetoothDevice device = service.getDevice();
if (device == null) return false;
synchronized(mDeviceBusy) {
if (mDeviceBusy) return false;
mDeviceBusy = true;
}
try {
mService.writeCharacteristic(mClientIf, device.getAddress(),
service.getType(), service.getInstanceId(),
new ParcelUuid(service.getUuid()), characteristic.getInstanceId(),
new ParcelUuid(characteristic.getUuid()),
characteristic.getWriteType(), AUTHENTICATION_NONE,
characteristic.getValue());
} catch (RemoteException e) {
Log.e(TAG,"",e);
mDeviceBusy = false;
return false;
}
return true;
}
我知道这是我的第二个答案,但它对我的第一个 post 进行了详细说明。我希望你能以某种方式帮助你。
从文档中我看到如何在没有响应的情况下这样写:
BluetoothGattCharacteristic characteristic = ...
characteristic.setValue(bytes);
mBluetoothGatt.writeCharacteristic(characteristic);
如何执行带有响应的写入请求操作?
(在 iOS 中有选项 select 写入类型 CBCharacteristicWriteWithResponse 和 CBCharacteristicWriteWithoutResponse)
- 你的特征应该是可写的。
检查特征是否可写:
(characteristic.getProperties() & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE))) != 0
- BluetoothGatt.writeCharacteristic() returns 布尔标志,表示写入是否成功(成功则为真)。
- 一旦 BluetoothGatt.writeCharacteristic 成功 BluetoothGattCallback.onCharacteristicWrite() 回调将使用具有您指定值的特征执行。
来自文档:
public void setWriteType (int writeType)
Added in API level 18 Set the write type for this characteristic
Setting the write type of a characteristic determines how the writeCharacteristic(BluetoothGattCharacteristic) function write this characteristic.
Parameters writeType The write type to for this characteristic. Can be one of: WRITE_TYPE_DEFAULT, WRITE_TYPE_NO_RESPONSE or WRITE_TYPE_SIGNED.
是的,我没有提到它。但是,请注意,应用不同的写入类型是在您写入值的特征必须是可写的条件下工作的,这是蓝牙设备特定的逻辑。首先找出您的特征是什么类型,因为它可能与您需要的类型相同。一旦它满足可写条件,每个 WRITE_TYPE 都应该适合你。
通过分析BluetoothGatt的writeCharacteristic()方法清晰可见:
public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
&& (characteristic.getProperties() &
BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false;
if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid());
if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false;
BluetoothGattService service = characteristic.getService();
if (service == null) return false;
BluetoothDevice device = service.getDevice();
if (device == null) return false;
synchronized(mDeviceBusy) {
if (mDeviceBusy) return false;
mDeviceBusy = true;
}
try {
mService.writeCharacteristic(mClientIf, device.getAddress(),
service.getType(), service.getInstanceId(),
new ParcelUuid(service.getUuid()), characteristic.getInstanceId(),
new ParcelUuid(characteristic.getUuid()),
characteristic.getWriteType(), AUTHENTICATION_NONE,
characteristic.getValue());
} catch (RemoteException e) {
Log.e(TAG,"",e);
mDeviceBusy = false;
return false;
}
return true;
}
我知道这是我的第二个答案,但它对我的第一个 post 进行了详细说明。我希望你能以某种方式帮助你。