我是Android新手,现在正在做一个简单的应用程序,需要将一些数据写入外围设备.
实际上三星GT-S7272C设备没有出错.但是当我切换到Sony LT29i时,当我试图写入某个特征时,总会有状态133.我会给出一些简短的代码.
BluetoothGattService syncService = gatt.getService(SYNC_DATA_SERVICE);
BluetoothGattCharacteristic tChar = syncService.getCharacteristic(SYNC_TIME_INPUT_CHAR);
if (tChar == null) throw new AssertionError("characteristic null when sync time!");
int diff = /*a int*/;
tChar.setValue(diff, BluetoothGattCharacteristic.FORMAT_SINT32, 0);
gatt.writeCharacteristic(tChar);
Run Code Online (Sandbox Code Playgroud)
和onCharacteristicWrite函数:
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.d(TAG, String.format("Sync: onCharWrite, status = %d", status));
try {
if (status != BluetoothGatt.GATT_SUCCESS) throw new AssertionError("Error on char write");
super.onCharacteristicWrite(gatt, characteristic, status);
if (characteristic.getUuid().equals(SYNC_TIME_INPUT_CHAR)) {
BluetoothGattService syncService = gatt.getService(SYNC_DATA_SERVICE);
BluetoothGattCharacteristic tChar = syncService.getCharacteristic(SYNC_HEIGHT_INPUT_CHAR);
if (tChar == null) throw …Run Code Online (Sandbox Code Playgroud) 在onCharacteristicwrite成功解决熟悉的问题后,我继续133在readCharacteristic功能中遇到这些状态.
这里有一个简短的代码:我将特性存储到onServicesDiscovered函数中的变量:
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
try {
syncDataService = gatt.getService(GiraffeFriendAttributes.SYNC_DATA_SERVICE);
if (syncDataService == null) throw new AssertionError("sync data service null!");
syncDataInputChar = syncDataService.getCharacteristic(GiraffeFriendAttributes.SYNC_DATA_INPUT_CHAR);
syncDataOutputChar = syncDataService.getCharacteristic(GiraffeFriendAttributes.SYNC_DATA_OUTPUT_CHAR);
if (syncDataInputChar == null || syncDataOutputChar == null) throw new AssertionError("sync data service null!");
...
} catch ...
}
Run Code Online (Sandbox Code Playgroud)
然后在写入SYNC_DATA_INPUT_CHAR设备之后,设备将更改其中一个特性的值,我将需要获取该值.所以我在下面写代码.
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.d(TAG, String.format("Sync: onCharWrite, status = %d", status));
try …Run Code Online (Sandbox Code Playgroud)