我的Android应用程序可以根据方法的顺序来编写或读取特征。如何同步两者?我需要使用onClick侦听器进行编写并读取值(不一定是来自写入特征的响应)。使用下面的代码,它只能读取。如果我注释掉def json
JSON.parse(response.body)
end
,那么我只能写一次
主要活动 具有BLE服务的UI活页夹:
mBluetoothLeService.readCharacteristic(characteristic);
获取服务,写作/阅读:
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (RBLService.ACTION_GATT_DISCONNECTED.equals(action)) {
} else if (RBLService.ACTION_GATT_SERVICES_DISCOVERED
.equals(action)) {
getGattService(mBluetoothLeService.getSupportedGattService());
Log.w(TAG,"calling gattservice");
} if (RBLService.ACTION_DATA_AVAILABLE.equals(action)) {
Log.w(TAG,"sending data to display function");
displayData(intent.getExtras().getString(RBLService.EXTRA_DATA));
}
}
};
BLE服务,编写特征:
private void getGattService(BluetoothGattService gattService) {
BluetoothGattCharacteristic characteristic = gattService
.getCharacteristic(RBLService.UUID_BLE_SHIELD_TX);
map.put(characteristic.getUuid(), characteristic);
mBluetoothLeService.setCharacteristicNotification(characteristic,true);
mBluetoothLeService.readCharacteristic(characteristic);
Log.w(TAG,"I am at read charac method");
Log.w(TAG, "TX ok:");
if (wData == TRUE) {
try {
characteristic.setValue(arr);
Log.w(TAG, "characteristic set to array" + characteristic);
} catch (NullPointerException e) {
Log.w(TAG, "sth is wrong @player");
}
try {
mBluetoothLeService.writeCharacteristic(characteristic);
Log.w(TAG, "write success" + characteristic);
} catch (NullPointerException e) {
Log.w(TAG, "sth is wrong @player 2");
}