新写入一个characteristic时,应该等上一个写入characteristic操作结束后,在回调函数里面得到返回状态过后,再能继续写入下一个characteristic
新写入一个characteristic时,
public static boolean Write_Characteristic_Callback_Success = true; Thread t1=new Thread(new Runnable() { @Override public void run() { //上一次回调是否成功,不成功继续等待 while(!Write_Characteristic_Callback_Success){} //开始写入之前,把Write_Characteristic_Callback_Success置为false Write_Characteristic_Callback_Success=false; mBluetoothLeService.writeCharacteristic(finalBluetoothGattCharacteristic); } });
t1.start(); try { //等待该线程执行结束 t1.join(); } catch (InterruptedException e) { e.printStackTrace(); }
在回调函数里面得到返回状态
@Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if(status==BluetoothGatt.GATT_SUCCESS){ Write_Characteristic_Callback_Success=true;//自定义的全局静态变量,记录上一个写入操作回调函数得到的状态 broadcastUpdate(ACTION_DATA_WRITE_SUCCESS, characteristic); } }