0
点赞
收藏
分享

微信扫一扫

Android 低功耗蓝牙开发(数据交互)


Android 低功耗蓝牙开发(数据交互)

  • ​​前言​​
  • ​​正文​​
  • ​​一、BluetoothGattCallback​​
  • ​​1. onPhyUpdate​​
  • ​​2. onPhyRead​​
  • ​​3. onServicesDiscovered​​
  • ​​4. onCharacteristicRead​​
  • ​​5. onCharacteristicWrite​​
  • ​​6. onCharacteristicChanged​​
  • ​​7. onDescriptorRead​​
  • ​​8. onDescriptorWrite​​
  • ​​9. onReliableWriteCompleted​​
  • ​​10. onReadRemoteRssi​​
  • ​​11. onMtuChanged​​
  • ​​二、使用​​
  • ​​1. 连接设备​​
  • ​​2. 获取MTU Size​​
  • ​​3. 发现服务​​
  • ​​4. 打开通知​​
  • ​​5. 写入数据​​
  • ​​6. 收到数据​​
  • ​​7. Phy值读取和改变​​
  • ​​8. 读取特性、描述符、RSSI​​
  • ​​三、源码​​

前言

  在上一篇低功耗蓝牙开发文章中,我讲述了扫描和连接,本篇文章讲述数据的交互。当了解了数据交互后就可以开始进行低功耗蓝牙硬件和手机App软件相结合的项目,例如蓝牙音箱、蓝牙灯、蓝牙锁等等。

正文

  因为本篇文章会接着上一篇文章进行一个续写,上一篇文章 ​​Android 低功耗蓝牙开发(扫描、连接)​​,没看过的可以先看看,这样可以平稳过度,当然如果对扫描和连接都没有问题的可以直接从本篇文章开始看。

一、BluetoothGattCallback

  在进行编码之前首先要了解一个很重要的东西,那就是BluetoothGattCallback,这个类非常重要,可以说你能不能进行低功耗蓝牙的数据交互全看它了。

之前在进行低功耗蓝牙连接的时候使用的是Gatt连接,不知道你是否还记得。回顾一下:

Android 低功耗蓝牙开发(数据交互)_低功耗蓝牙


可以看到通过连接gatt,使用了抽象类BluetoothGattCallback,重写了里面的一个onConnectionStateChange方法,进行设备连接状态的回调。这个类里面还有一些方法,可以用于针对开发中的使用进行调用。下面来介绍一下:

方法

描述

onPhyUpdate

物理层改变回调

onPhyRead

设备物理层读取回调

onConnectionStateChange

Gatt连接状态回调

onServicesDiscovered

发现服务回调

onCharacteristicRead

特性读取回调

onCharacteristicWrite

特性写入回调

onCharacteristicChanged

特性改变回调

onDescriptorRead

描述读取回调

onDescriptorWrite

描述写入回调

onReliableWriteCompleted

可靠写入完成回调

onReadRemoteRssi

读取远程设备信号值回调

onMtuChanged

MtuSize改变回调

onConnectionUpdated

连接更新回调

这里光有一个表好像是没有啥用,在介绍详细的API方法及里面的属性值之前先做好准备工作。

BluetoothGattCallback是一个抽象类,那么自然需要一个实现类,在之前的文章中我是通过匿名实现里面的onConnectionStateChange方法对低功耗蓝牙设备进行连接和断开的监听的。

不过在实际开发中这样的做法并不可取,因为一个蓝牙项目里面不可能只有一个地方需要使用这个监听,那么此时就需要封装一个类去单独实现BluetoothGattCallback中的方法,然后再根据需要取使用。

下面在com.llw.bledemo下新建一个callback包,包里面新建一个BleCallback类,然后继承BluetoothGattCallback,重写里面的onConnectionStateChange方法,代码如下:

public class BleCallback extends BluetoothGattCallback {

private static final String TAG = BleCallback.class.getSimpleName();

/**
* 连接状态改变回调
*
* @param gatt gatt
* @param status gatt连接状态
* @param newState 新状态
*/
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (status == BluetoothGatt.GATT_SUCCESS) {
switch (newState) {
case BluetoothProfile.STATE_CONNECTED://连接成功
Log.d(TAG, "连接成功");
break;
case BluetoothProfile.STATE_DISCONNECTED://断开连接
Log.e(TAG, "断开连接");
break;
default:
break;
}
} else {
Log.e(TAG, "onConnectionStateChange: " + status);
}
}
}

为了区别于上一篇文章,我这里会新建一个DataExchangeActivity来做数据的交互,不会影响到上一篇文章的内容。然后在MainActivity,点击列表item时调用的connectDevice方法中跳转到DataExchangeActivity中,通过传递蓝牙对象过去。

Android 低功耗蓝牙开发(数据交互)_android_02

接下来看看DataExchangeActivity中做了什么?

public class DataExchangeActivity extends AppCompatActivity {
private static final String TAG = DataExchangeActivity.class.getSimpleName();

/**
* Gatt
*/
private BluetoothGatt bluetoothGatt;
/**
* 设备是否连接
*/
private boolean isConnected = false;

/**
* Gatt回调
*/
private BleCallback bleCallback;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_exchange);
//初始化
bleCallback = new BleCallback();
//获取上个页面传递过来的设备
BluetoothDevice device = getIntent().getParcelableExtra("device");
//连接gatt 设置Gatt回调
bluetoothGatt = device.connectGatt(this, false, bleCallback);
}

/**
* 断开设备连接
*/
private void disconnectDevice() {
if (isConnected && bluetoothGatt != null) {
bluetoothGatt.disconnect();
}
}

/**
* Toast提示
*
* @param msg 内容
*/
private void showMsg(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}

很简单这个代码,就是接收上个页面传递过来的蓝牙对象,然后进行gatt连接。直接传入实例化之后的bleCallback即可,请注意关于gatt的处理都是在子线程中进行的,可以验证一下:

Android 低功耗蓝牙开发(数据交互)_ide_03


运行一下,进入交互页面。

Android 低功耗蓝牙开发(数据交互)_物联网_04


下面进行GattCallback中的API介绍。

1. onPhyUpdate

/**
* 物理层改变回调
*
* @param gatt gatt
* @param txPhy 发送速率 1M 2M
* @param rxPhy 接收速率 1M 2M
* @param status 更新操作的状态
*/
@Override
public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
super.onPhyUpdate(gatt, txPhy, rxPhy, status);
}

通过gatt.setPreferredPhy()方法触发,例如:

//设置 2M 
gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M, BluetoothDevice.PHY_LE_2M, BluetoothDevice.PHY_OPTION_NO_PREFERRED);

2. onPhyRead

/**
* 读取物理层回调
*
* @param gatt gatt
* @param txPhy 发送速率 1M 2M
* @param rxPhy 接收速率 1M 2M
* @param status 更新操作的状态
*/
@Override
public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
super.onPhyRead(gatt, txPhy, rxPhy, status);
}

通过gatt.readPhy();进行触发,该方法不需要传入参数。

3. onServicesDiscovered

/**
* 发现服务回调
*
* @param gatt gatt
* @param status gatt状态
*/
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
}

通过gatt.discoverServices(); 触发,没有输入参数。

4. onCharacteristicRead

/**
* 特性读取回调
*
* @param gatt gatt
* @param characteristic 特性
* @param status gatt状态
*/
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
}

通过gatt.readCharacteristic(characteristic);触发,需要构建一个BluetoothGattCharacteristic 对象,在后面的实例中会演示。

5. onCharacteristicWrite

/**
* 特性写入回调
*
* @param gatt gatt
* @param characteristic 特性
* @param status gatt状态
*/
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
}

通过gatt.writeCharacteristic(characteristic);触发,需要BluetoothGattCharacteristic 对象,在后面的实例中会演示。

6. onCharacteristicChanged

/**
* 特性改变回调
*
* @param gatt gatt
* @param characteristic 特性
*/
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
}

此回调的触发需要远程设备特性改变,通俗的说就是,你需要给设备发送消息之后,才会触发这个回调。在后面的实例中会演示。

7. onDescriptorRead

/**
* 描述符获取回调
*
* @param gatt gatt
* @param descriptor 描述符
* @param status gatt状态
*/
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
}

通过gatt.readDescriptor(descriptor);触发,需要传入BluetoothGattDescriptor对象,在后面的实例中会演示。

8. onDescriptorWrite

/**
* 描述符写入回调
*
* @param gatt gatt
* @param descriptor 描述符
* @param status gatt状态
*/
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
}

通过gatt.writeDescriptor(descriptor);触发,需要传入BluetoothGattDescriptor 对象,在后面的实例中会演示。

9. onReliableWriteCompleted

/**
* 可靠写入完成回调
*
* @param gatt gatt
* @param status gatt状态
*/
@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
super.onReliableWriteCompleted(gatt, status);
}

通过gatt.executeReliableWrite();触发,不需要参数,在实际中用的不是很多。

10. onReadRemoteRssi

/**
* 读取远程设备的信号强度回调
*
* @param gatt gatt
* @param rssi 信号强度
* @param status gatt状态
*/
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
}

通过gatt.readRemoteRssi();触发,无需参数,实际中使用不多。

11. onMtuChanged

/**
* Mtu改变回调
*
* @param gatt gatt
* @param mtu new MTU size
* @param status gatt状态
*/
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
}

通过gatt.requestMtu(512);触发,需要传入请求的Mtu大小,最大是512,这里单位是字节,512是理论最大值,如果不设置就是默认23字节,而且传输本身用掉3字节,实际上携带数据只有20字节。在后面的实例中会演示。

最后的一个onConnectionUpdated回调无法进行覆写,就不介绍了,下面进入使用API环节。

二、使用

1. 连接设备

  第一步是连接,代码在上面已经写好,连接上设备之后,

2. 获取MTU Size

下一步就是获取MtuSize。

Android 低功耗蓝牙开发(数据交互)_ide_05


然后会触发onMtuChanged回调,

3. 发现服务

在onMtuChanged回调中去发现服务。

Android 低功耗蓝牙开发(数据交互)_android_06

然后就会触发onServicesDiscovered回调,在这个回调中要做的就是打开通知开关。这个在之前没有提到,因为它不在基础的回调API中,但是打开通知开关属于描述符的内容,因此当你设置了之后会触发onDescriptorWriteh回调,还是先来看这个通知怎么打开吧。

4. 打开通知

  为了规范一些,将使用到的方法封装起来,这样便于管理,增加一个BleHelper类和BleConstant类。
新建一个utils包,包下建两个类,下面先看这个BleConstant类

public class BleConstant {

/**
* 服务 UUID
*/
public static final String SERVICE_UUID = "0000ff01-0000-1000-8000-00805f9b34fb";
/**
* 特性写入 UUID
*/
public static final String CHARACTERISTIC_WRITE_UUID = "0000ff02-0000-1000-8000-00805f9b34fb";
/**
* 特性读取 UUID
*/
public static final String CHARACTERISTIC_READ_UUID = "0000ff10-0000-1000-8000-00805f9b34fb";
/**
* 描述 UUID
*/
public static final String DESCRIPTOR_UUID = "00002902-0000-1000-8000-00805f9b34fb";
/**
* 电池服务 UUID
*/
public static final String BATTERY_SERVICE_UUID = "0000180f-0000-1000-8000-00805f9b34fb";
/**
* 电池特征(特性)读取 UUID
*/
public static final String BATTERY_CHARACTERISTIC_READ_UUID = "00002a19-0000-1000-8000-00805f9b34fb";
/**
* OTA服务 UUID
*/
public static final String OTA_SERVICE_UUID = "5833ff01-9b8b-5191-6142-22a4536ef123";
/**
* OTA特征(特性)写入 UUID
*/
public static final String OTA_CHARACTERISTIC_WRITE_UUID = "5833ff02-9b8b-5191-6142-22a4536ef123";
/**
* OTA特征(特性)表示 UUID
*/
public static final String OTA_CHARACTERISTIC_INDICATE_UUID = "5833ff03-9b8b-5191-6142-22a4536ef123";
/**
* OTA数据特征(特性)写入 UUID
*/
public static final String OTA_DATA_CHARACTERISTIC_WRITE_UUID = "5833ff04-9b8b-5191-6142-22a4536ef123";
}

这里面都是常规的UUID常量值,就是一些服务和特性的标识符,这个UUID常量值由SIG联盟所规定的,当然也可以根据自己的硬件去做设置,值不是固定的,请根据实际的硬件为主。

下面是BleHelper类,代码如下:

public class BleHelper {

/**
* 启用指令通知
*/
public static boolean enableIndicateNotification(BluetoothGatt gatt) {
//获取Gatt 服务
BluetoothGattService service = gatt.getService(UUID.fromString(BleConstant.OTA_SERVICE_UUID));
if (service == null) {
return false;
}
//获取Gatt 特征(特性)
BluetoothGattCharacteristic gattCharacteristic = service.getCharacteristic(UUID.fromString(BleConstant.OTA_CHARACTERISTIC_INDICATE_UUID));
return setCharacteristicNotification(gatt, gattCharacteristic);
}

/**
* 设置特征通知
* return true, if the write operation was initiated successfully
*/
private static boolean setCharacteristicNotification(BluetoothGatt gatt, BluetoothGattCharacteristic gattCharacteristic) {
//如果特性具备Notification功能,返回true就代表设备设置成功
boolean isEnableNotification = gatt.setCharacteristicNotification(gattCharacteristic, true);
if (isEnableNotification) {
//构建BluetoothGattDescriptor对象
BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(UUID.fromString(BleConstant.DESCRIPTOR_UUID));
gattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
//写入描述符
return gatt.writeDescriptor(gattDescriptor);
} else {
return false;
}
}
}

代码没有什么难度,下面就是在发现服务的回调中调用BleHelper中的方法。

Android 低功耗蓝牙开发(数据交互)_低功耗蓝牙_07


当开启通知失败时断开gatt连接。下面会进入onDescriptorWrite回调

Android 低功耗蓝牙开发(数据交互)_物联网_08

当通知开启成功可以就可以进行数据的交互了,不过在此之前先运行一下,看程序是否按照我们所想的运行,看一下日志:

Android 低功耗蓝牙开发(数据交互)_低功耗蓝牙_09


Very Good!

现在基本的前置工作都准备好了,下一步就是数据的读写了,首先来看看写数据到设备。

5. 写入数据

  常规来说写入数据的话肯定是要对设备做点什么,列如一个蓝牙灯,控制这个灯开关,那么这就是一条指令,指令的内容是App与设备端协商好的,这个要以实际的需求为主。假设我对一个蓝牙手环要进行数据的写入,那么肯定会有很多的指令,所以可以封装一个方法集中处理,依然写在BleHelper中。方法如下:

/**
* 发送指令
* @param gatt gatt
* @param command 指令
* @param isResponse 是否响应
* @return
*/
public static boolean sendCommand(BluetoothGatt gatt, String command, boolean isResponse) {
//获取服务
BluetoothGattService service = gatt.getService(UUID.fromString(BleConstant.OTA_SERVICE_UUID));
if (service == null) {
Log.e("TAG", "sendCommand: 服务未找到");
return false;
}
//获取特性
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(BleConstant.OTA_CHARACTERISTIC_WRITE_UUID));
if (characteristic == null) {
Log.e("TAG", "sendCommand: 特性未找到");
return false;
}
//写入类型 WRITE_TYPE_DEFAULT 默认有响应, WRITE_TYPE_NO_RESPONSE 无响应。
characteristic.setWriteType(isResponse ?
BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT : BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
//将字符串command转Byte后进行写入
characteristic.setValue(ByteUtils.hexStringToBytes(command));

boolean result = gatt.writeCharacteristic(characteristic);
Log.d("TAG", result ? "写入初始化成功:" + command : "写入初始化失败:" + command);
return result;
}

  下面解释一下这个方法的内容,首先通过服务UUID获取到Gatt服务,然后通过写数据特性UUID从服务中获取写数据特性,这里的UUID的值请根据自己的实际情况填写,不知道就问硬件工程师。然后根据传入的isResponse去设置是否需要响应,这里要弄清楚有响应和无响应的区别,有响应的速度比无响应慢,但是有响应更安全,因为你可以对每一次发出的数据进行一个确认,是否发送到,有无丢失。不过这样的话效率会比较低,一般来说实际开发中大部分指令型消息都会选择无响应,数据型消息会选择有响应。

这里增加一个工具类,代码如下:

public class ByteUtils {
/**
* Convert hex string to byte[]
*
* @param hexString the hex string
* @return byte[]
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}

public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}

/**
* Convert char to byte
*
* @param c char
* @return byte
*/
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
}

然后就是对消息的内容转Byte,这里的指令长度有一个最大值就是之前通过onMtuChange回调时得到的数值,247 去掉3字节传输实际上就是244字节,那么你一次传输的最大字节就是244,这个值你不能写死,因为你要根据Android版本和蓝牙设备硬件去适配。最终通过setValue将值放入特性,然后通过写入特性传递给设备。然后返回一个boolean值,这个值只是表明写入特性的初始化成功,不代表就真的写入到设备中了,那么写入到设备成功的标识是什么呢?

先不急,我们先调用这个方法,
修改页面的布局文件activity_data_exchange.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/dp_20"
tools:context=".DataExchangeActivity">
<EditText
android:id="@+id/et_command"
android:hint="请输入指令"
android:layout_width="match_parent"
android:layout_height="60dp"/>
<Button
android:id="@+id/btn_send_command"
android:layout_marginTop="10dp"
android:text="发送指令"
android:insetTop="0dp"
android:insetBottom="0dp"
android:layout_width="match_parent"
android:layout_height="50dp"/>

</LinearLayout>

然后回到DataExchangeActivity,
先声明变量

private EditText etCommand;

然后在onCreate中

Android 低功耗蓝牙开发(数据交互)_Ble蓝牙数据交互_10

然后先写入一条指令给设备。例如0102,这对于我这个设备来说是一个切换模式的指令,这条指令不需要响应,那么在哪里确认这个指令写入到了设备呢?通过onCharacteristicWrite。

先修改一下这个回调方法中的内容。在BleCallback中,

Android 低功耗蓝牙开发(数据交互)_Ble蓝牙数据交互_11


下面运行一下:

Android 低功耗蓝牙开发(数据交互)_低功耗蓝牙_12


下面来看看控制栏的日志打印:

Android 低功耗蓝牙开发(数据交互)_ide_13


写入成功。下面来看收到数据的处理

6. 收到数据

  如果你需要收到数据,那么就需要使用有响应的设置,这里设置为true。

Android 低功耗蓝牙开发(数据交互)_android_14


当设备的特性改变时

Android 低功耗蓝牙开发(数据交互)_低功耗蓝牙_15


我这里打印一下,然后运行。

Android 低功耗蓝牙开发(数据交互)_ide_16


01020,是我的蓝牙设备中定义一个值,收到0081 则表示正常,然后看控制台。

Android 低功耗蓝牙开发(数据交互)_物联网_17


这里当我们进行有响应的数据写入时,设备收到后会先触发onCharacteristicChanged然后再触发onCharacteristicWrite。也就是先发送指令,设备收到回复后,再是你的指令写入成功,注意这个执行的顺序,这很重要,在实际开发中请注意这一点,然后再去写相应的业务逻辑。

还有一些其他的API也需要介绍一下怎么使用的,例如onPhyRead和onPhyUpdate。

7. Phy值读取和改变

  首先来看这个值的读取,比如我们在通知开启成功之后去读取这个设备的Phy

Android 低功耗蓝牙开发(数据交互)_android_18


这个读取的方法要求你的Android版本必须要在8.0和8.0以上,因此如果你的Android设备是低版本的就不用考虑去使用这个API了,因为系统不支持。我是Android10.0所以没问题,调用这个方法API就会触发onPhyRead回调。我们在回调的时候打印一下内容,看看当前的硬件Phy是什么值。

Android 低功耗蓝牙开发(数据交互)_物联网_19


运行一下看控制台打印了什么

Android 低功耗蓝牙开发(数据交互)_android_20


都是1 就代表1M的发送和接收速率,那么你也可以改成2M,可以这么做,当我读取到速率为1M时就请求2M的速率。

Android 低功耗蓝牙开发(数据交互)_Ble蓝牙数据交互_21


然后就会触发onPhyUpdate回调,我们打印一下:

Android 低功耗蓝牙开发(数据交互)_低功耗蓝牙_22


运行之后查看控制台:

Android 低功耗蓝牙开发(数据交互)_低功耗蓝牙_23


Beautiful!现在我们知道这个Phy怎么改的了,那么在什么时候改呢?当你要传大数据的时候。例如你要对蓝牙设备中的软件进行升级,那么升级文件是比较大的,此时在条件允许提高传输速率可以降低等待时间。

8. 读取特性、描述符、RSSI

  一般来说这三个回调用的比较少,如果你不熟悉的话,前期可以使用。它们在不同的时候使用,由于获取特性和描述符需要一个参数,因此你需要在有这个参数的时候去调用它,比如当写入特性回调被触发时,

Android 低功耗蓝牙开发(数据交互)_低功耗蓝牙_24


再比如特性改变时。

Android 低功耗蓝牙开发(数据交互)_物联网_25


然后会触发onCharacteristicRead回调,在这个回调中打印一下特性的uuid。

Android 低功耗蓝牙开发(数据交互)_物联网_26


其实说起来这个方法比较的鸡肋,这可能也是为什么使用的比较少的原因了,因为当我能知道特性是什么的时候,我直接就能拿到特性对象所携带的信息,根本不需要再去通过gatt.readCharacteristic(characteristic);去查看特性。说是这么说,不过该介绍的还是要有的,知道就可以了。另一个描述符的读取也是一样的道理,可以在描述符写入回调时调用,

Android 低功耗蓝牙开发(数据交互)_低功耗蓝牙_27


同时我还调用了gatt.readRemoteRssi,因为获取RSSI不需要参数,只要你的设备保持了连接,那么可以在任何时候获取RSSI,然后我们在对应的地方去打印一下:

Android 低功耗蓝牙开发(数据交互)_物联网_28


Android 低功耗蓝牙开发(数据交互)_ide_29


下面运行一下:

Android 低功耗蓝牙开发(数据交互)_物联网_30


这里可以看到我写入了0102之后设备的地址会发生改变,所以我退出了当前页面,再连接设备之后,发送了010200,这里我们看到了RSSI和描述符的UUID,不过特性的UUID并没有打印出来,这是为什么呢?gatt.readCharacteristic(characteristic);执行后会返回一个boolean结果,打印一下这个结果看看。

Android 低功耗蓝牙开发(数据交互)_物联网_31


运行打印一下:

Android 低功耗蓝牙开发(数据交互)_ide_32


那么来看看为什么会是false。

Android 低功耗蓝牙开发(数据交互)_物联网_33


这里我突然想到一种可能性,是不是读取这个特性的对象有问题,我现在的这个特性的uuid是之前写特性的uuid,所以拿不到读特性的回调。然后试了一下,发现还是false,拿不到特性,这个就和硬件有关系了,蓝牙硬件会根据功能的需求,对特性进行改动,有一些特性不重要的就去掉了,因此针对我这个蓝牙设备来说就拿不到读特性。

三、源码

GitHub: BleDemo

山高水长,后会有期~


举报

相关推荐

0 条评论