在stackoverflow上修改此article和answer后,我编写了以下BluetoothGattCallback:
private BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) {
if (status == GATT_SUCCESS) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
// We successfully connected, proceed with service discovery
Log.i(TAG, "We successfully connected, proceed with service discovery");
stopScan();
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
// We successfully disconnected on our own request
Log.i(TAG, "We successfully disconnected on our own request");
gatt.close();
} else {
}
} else {
// An error happened...
gatt.close();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.i(TAG, "We are onServiceDiscovered");
BluetoothGattCharacteristic characteristic =
gatt.getService(Beacon.BEACON_SERVICE_UUID)
.getCharacteristic(Beacon.BEACON_CHAR_UUID);
BluetoothGattDescriptor descriptor =
characteristic.getDescriptor(convertFromInteger(0x2902));
descriptor.setValue(
BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
gatt.setCharacteristicNotification(characteristic, true);
writeCharacteristic();
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, final BluetoothGattDescriptor descriptor, int status) {
Log.i(TAG, "onWrite");
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.i(TAG, "We are onCharacteristicChanged");
dev_data_hendler.sendMessage(Message.obtain(dev_data_hendler, 0, characteristic.getValue()));
}
};
其中writeCharacteristic为:
private void writeCharacteristic() {
class writeTask implements Runnable {
byte [] bytes;
writeTask(byte[] to_write){bytes = to_write;}
public void run() {
if (gatt == null) {
Log.e(TAG, "lost connection");
}
if (gatt.getService(Beacon.BEACON_SERVICE_UUID) == null) {
Log.e(TAG, "service not found!");
}
if (gatt.getService(Beacon.BEACON_SERVICE_UUID).getCharacteristic(Beacon.BEACON_CHAR_UUID) == null) {
Log.e(TAG, "char not found!");
}
BluetoothGattCharacteristic characteristic = gatt.getService(Beacon.BEACON_SERVICE_UUID).getCharacteristic(Beacon.BEACON_CHAR_UUID);
characteristic.setWriteType(WRITE_TYPE_DEFAULT);
characteristic.setValue(bytes);
if (gatt.writeCharacteristic(characteristic)) {
Log.i(TAG, "Char written" + flag);
flag++;
} else {
Log.e(TAG, "Char writing failure");
}
}
}
Thread t = new Thread(new writeTask(new byte[] {(byte)0x05, (byte)0x01}));
t.start();
try {
t.sleep(150);
} catch (InterruptedException e) {
Log.i(TAG, "interrupted exception");
}
}
而gatt.writeCharacteristic(characteristic)返回false。编写和设置通知可以很好地工作,但要分开进行。如果我先写特征,writeCharacteristic返回true,但是我的设备对此没有反应。它只能分开工作
最佳答案
在Android上,您需要等待写入/读取完成,然后才能进行下一个写入/读取。所以你需要排队写/读...