求助:将BLE服务ID转换为Flutter Reactive BLE所需的UUID(List<int>)格式
如何将BLE服务UUID转换为Flutter Reactive BLE所需的格式
针对你提供的服务UUID 000018AE-0000-1000-8000-00805F9B34FB,有三种方式可以转换为Flutter Reactive BLE插件Uuid类要求的格式,直接用于withServices参数:
方法1:直接使用完整UUID字符串(最简便)
利用Uuid.fromString构造函数,直接传入带连字符的完整UUID字符串,插件内部会自动完成格式转换:
flutterReactiveBle.scanForDevices( withServices: [Uuid.fromString("000018AE-0000-1000-8000-00805F9B34FB")], scanMode: ScanMode.lowLatency, ).listen((device) { // 处理扫描结果的代码 }, onError: () { // 处理错误的代码 });
方法2:手动转换为List
如果需要手动控制字节序列,可先将UUID拆分为十六进制字节数组,再通过Uuid.fromList传入:
- 去掉UUID中的所有连字符,得到
000018AE00001000800000805F9B34FB - 每两位字符转换为一个十六进制整数,生成字节列表
- 传入
Uuid.fromList
代码示例:
// 手动转换后的UUID字节列表 final serviceUuidBytes = [ 0x00, 0x00, 0x18, 0xAE, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB, ]; flutterReactiveBle.scanForDevices( withServices: [Uuid.fromList(serviceUuidBytes)], scanMode: ScanMode.lowLatency, ).listen((device) { // 处理扫描结果的代码 }, onError: () { // 处理错误的代码 });
方法3:利用标准蓝牙服务的短UUID格式
你的UUID属于标准蓝牙服务UUID(格式为0000XXXX-0000-1000-8000-00805F9B34FB),其中XXXX是16位服务ID18AE,可以直接用Uuid.shortest传入对应的16位整数,插件会自动补全为完整的128位UUID,这是最简洁的写法:
flutterReactiveBle.scanForDevices( withServices: [Uuid.shortest(0x18AE)], scanMode: ScanMode.lowLatency, ).listen((device) { // 处理扫描结果的代码 }, onError: () { // 处理错误的代码 });
内容的提问来源于stack exchange,提问作者Kyle Andrew
相关产品推荐
相关产品推荐

