您的位置:首页 > 其它

onCharacteristicChanged 回调问题

2015-10-22 16:13 393 查看
最近接触了蓝牙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)//写数据、
上面都是我个人的观点,有错误的地方,欢迎大家指出

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: