Android BLE 读写开发回顾
这里主要记录BLE的通讯方面的笔记
1.发现BluetoothGattService回调
BLE 调用gatt.connect()连接成功之后调用:
gatt.discoverServices()
触发回调void onServicesDiscovered(BluetoothGatt gatt, int status)
通过硬件规定的uuid获取对应的BluetoothGattService:
BluetoothGattService service = gatt.getService(mServiceUuid);
在BluetoothGattService中包含一个或多个BluetoothGattCharacteristic,这些特征字将是后续用来读写数据的载体,而
通过硬件给定的uuid筛选出对应的特征字,譬如有些特征字用于收发简单命令,而有些特征字用于批量发送数据:
if (characteristic.getUuid().equals(UUID.fromString(String.format(MEDTRUM_BASE_UUID, MEDTRUM_UUID_CHARACTERISTIC_BULK)))) {
//获取批量发送数据特征字
mBulkCharacteristics = characteristic;
} else if (characteristic.getUuid().equals(UUID.fromString(String.format(MEDTRUM_BASE_UUID, MEDTRUM_UUID_CHARACTERISTIC_CTRLPT)))) {
//获取收发命令特征字
mCommandCharacteristics = c