zoukankan      html  css  js  c++  java
  • Android-BlutoothBle,蓝牙中心设备(peripheral)向外围设备(GattServer)连续写入多个Characteristic的注意事项

    新写入一个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);
                }
            }

    Android蓝牙开发的各种坑

  • 相关阅读:
    Mybatis学习(2)原始dao开发和使用mapper接口代理开发
    Mybatis学习(1)
    Leetcode | Merge Intervals
    GDB打印STL容器内容
    LeetCode | Max Points on a Line
    最长不减子序列【转】
    LeetCode | Evaluate Reverse Polish Notation
    LeetCode | Word Ladder II
    LeetCode | Valid Number
    LeetCode | Set Matrix Zeroes
  • 原文地址:https://www.cnblogs.com/sunupo/p/10390533.html
Copyright © 2011-2022 走看看