前言
上篇文章可能对你们没什么用处,只是我的一个记录,当初查找资料时遇到的坑和无知时的盲目。这篇文章我们就好好讲讲蓝牙。⚠️代码下载地址在最下方
正文
关于蓝牙的操作,不得不说两个类BluetoothGatt和BluetoothGattCallback;
这两个类包含了对蓝牙的操作(写入命令)和读取蓝牙返回的值。
BluetoothGatt 里有几个重要的方法:
connect():链接
disconnect():断开链接
discoverServices():搜索蓝牙服务
getServices():获取蓝牙服务(多个)
getService(UUID uuid):根据蓝牙服务UUID,获取对应的蓝牙服务(一个)
readCharacteristic(BluetoothGattCharacteristic characteristic):读取特征值
writeCharacteristic(BluetoothGattCharacteristic characteristic):写入特征值(向蓝牙写入命令)等。
setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enable):注册蓝牙服务通道
BluetoothGattCallback回调的方法:
/**
* 链接蓝牙后,通信返回数据回调
*/
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override //当连接上设备或者失去连接时会回调该函数
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
//回调 特征值通知回调结果
Log.i(TAG,"onConnectionStateChange111 status = "+status+"; newState = "+newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG,"onConnectionStateChange connect");
gatt.discoverServices();//对蓝牙服务进行扫描
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG,"onConnectionStateChange disConnect");
}
}
@Override //当设备是否找到服务时,会回调该函数
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.i(TAG,"onServicesDiscovered222 status = "+status);
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.i(TAG,"onServicesDiscovered discover services ");
//发现蓝牙服务,对蓝牙通道进行注册
displayGattServices(gatt.getServices());
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override //当读取设备时会回调该函数
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
//回调特征值 操作报告读操作结果
Log.i(TAG ,"onCharacteristicRead333 characteristic "+"\n value = "+ Arrays.toString(characteristic.getValue()) +"\n HEXValue = "+ byte2HexStr(characteristic.getValue())+"\n"+" status = "+status+"\n characteristic UUID = "+characteristic.getUuid());
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.i(TAG ,"onCharacteristicRead333 uuid = "+characteristic.getUuid());
}
}
@Override //当向Characteristic写数据时会回调该函数
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
//回调特征值 操作指示写操作结果
super.onCharacteristicWrite(gatt, characteristic, status);
Log.i(TAG ,"onCharacteristicWrite444 characteristic "+"\n value = "+ Arrays.toString(characteristic.getValue()) +"\n HEXValue = "+ byte2HexStr(characteristic.getValue())+"\n"+" status = "+status+"\n characteristic UUID = "+characteristic.getUuid());
if (BluetoothGatt.GATT_SUCCESS == status) {
Log.i(TAG ,"onCharacteristicWrite value = " +byte2HexStr(characteristic.getValue()) + "\n" + "status = " + status);
}
}
@Override //设备发出通知时会调用到该接口
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.i(TAG ,"onCharacteristicChanged555 characteristic "+"\n value = "+ Arrays.toString(characteristic.getValue()) +"\n HEXValue = "+ byte2HexStr(characteristic.getValue()));
//测量数据返回
}
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
Log.i(TAG ,"onDescriptorRead666 descriptor "+"\n value = "+ Arrays.toString(descriptor.getValue()) +"\n HEXValue = "+ byte2HexStr(descriptor.getValue())+"\n"+" status = "+status);
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
UUID uuid = descriptor.getCharacteristic().getUuid();
Log.i(TAG ,"onDescriptorWrite777 descriptor "+"\n value = "+ Arrays.toString(descriptor.getValue()) +"\n HEXValue = "+ byte2HexStr(descriptor.getValue())+"\n"+" status = "+status);
}
@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
super.onReliableWriteCompleted(gatt, status);
Log.i(TAG ,"onReliableWriteCompleted888"+"\n"+" status = "+status);
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
Log.i(TAG ,"onReadRemoteRssi999 "+"\n rssi = "+rssi+"\n"+" status = "+status);
}
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
Log.i(TAG ,"onMtuChanged000 "+"\n mtu = "+mtu+"\n"+" status = "+status);
}
};
最后说一句 BluetoothGatt初始化方法是:
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
以上是我在看发蓝牙时使用到的方法,没有添加注释的方法,是我暂时没有用到,具体干什么的还不是很清楚。
关于蓝牙开发的代码地址:LeBluetooth开发
以下是Byte[] 、十进制、十六进制间进行转换的代码地址:byteArray、十进制、十六进制间转换
下篇文章进行我的代码怎么使用和在蓝牙开发过程中遇到的坑进行记录。
- 关于蓝牙方面知识的讨论请加Q群:177694195
* 有过有什么疑问,可以在下方评论区进行讨论。*