onCharacteristicChanged 回调问题

           最近接触了蓝牙BLE4.0开发,今天是我接触的第二天,完全是盟的,以前根本就没听过BLE,所以遇到的问题就一大堆了,特此做个笔记,就不具体瞎扯了。

首先涉及到的还是搜索,连接,接收和发送数据。

 1.搜索

     官方api http://www.android-doc.com/reference/android/bluetooth/package-summary.html

   

final BluetoothManager bluetoothManager =
        (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
mBluetoothAdapter.startLeScan(mLeScanCallback); // 搜索蓝牙
mBluetoothAdapter.stopLeScan(mLeScanCallback) //结束搜索

2连接
   mLeScanCallback 这个回调接口会返回device,通过device.connectGatt(this, false, mGattCallback);
mgattcallback又是一个回调,一般用到 onConnectionStateChange(连接状态改变),
onCharacteristicRead(readCharacteristic的时候会触发),onCharacteristicWrite(发数据触发),
onServicesDiscovered(查找service,一般连接成功就调用mBluetoothGatt.discoverServices();去触发),onCharacteristicChanged(需要先打开notify,在查询服务的时候去打开),如果BLE设备返回数据的时候会通过这个方法回调获得数据,然后在特征里面取。

3 数据
   readCharacteristic(BluetoothGattCharacteristic characteristic)//读数据
  writeCharacteristic(BluetoothGattCharacteristic characteristic)  //写数据、

 上面都是我个人的观点,有错误的地方,欢迎大家指出











  


你可能感兴趣的:(android)