上一篇:Android蓝牙开发BLE-蓝牙连接
上一篇写了BluetoothGattCallback的一个方法,判断了蓝牙是否连上。他还有好几个方法,读写就是在这里面进行,读写操作都在BluetoothGattCallback 里面。
蓝牙读写是对特征值的操作,关于服务、特征相关的建议去看看GATT协议。这里推荐:BLE 发现过程介绍
上代码
读写过程差不多是这样
1.调用bluetoothDevice.connectGatt(),进入onConnectionStateChange()方法
2.调用gatt.discoverServices(),到onServicesDiscovered()方法
3.在服务的特征上进行读写
BluetoothGattCallback mBluetoothGattCallback=new BluetoothGattCallback() { //连接回调
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (status==BluetoothGatt.GATT_SUCCESS) {
Log.d(TAG, "onConnectionStateChange: ==&