ios 蓝牙扫描 UUID(scanForPeripheralsWithServices)

相信很多小伙伴都对接过系统的蓝牙库(CoreBluetooth.framework),对于蓝牙各个阶段都很清楚。但是就我个人而言对于第一步【扫描】中scanForPeripheralsWithServices,所需要带的参数serviceUUIDs 该参数怎么样传以及它是怎么定义的?一直没有深究,相信很多小伙伴和我一样如下调用姿势

//CBCentralManagerScanOptionAllowDuplicatesKey

//CBCentralManagerScanOptionSolicitedServiceUUIDsKey

//NSDictionary *scanOptions = nil;

//开始扫描 一般会将 serviceUUIDs 设置为nil,这样就会扫描周围所有的设备

NSArray<CBUUID *> *serviceUUIDs = nil;

[BCustomBlueMananger scanForPeripheralsWithServices:serviceUUIDs options:scanOptions];

以上调用方式有没有问题呢?如果对于蓝牙扫描没有过多要求,该方式基本可以满足需求,但是有可能细心的小伙伴会发现,该方式如果推到后台呢?发现扫描不起作用,于是翻开开发文档有这么一句描述:

Your app can scan for Bluetooth devices in the background by specifying the bluetooth-central background mode. To do this, your app must explicitly scan for one or more services by specifying them in the serviceUUIDs parameter. The CBCentralManager scan option has no effect while scanning in the background.

//翻译为中文 大致意思就是  如果需要支持后台扫描,那么就需要提供serviceUUIDs 该参数以及在项目中配置蓝牙后台模式

那么这个参数怎么定义呢?是由谁定义呢? 这个是由外设(如手表、心率带、标签打印机等)开发蓝牙模块的小伙伴提供,那么他(外设开发小伙伴)应该放置在哪个参数呢?一般会有以下配置在01~06中随意配置一个就行

  • 0x00:广播标志
  • 0x01:不完整的16位服务UUID列表
  • 0x02:完整的16位服务UUID列表
  • 0x03:不完整的32位服务UUID列表
  • 0x04:完整的32位服务UUID列表
  • 0x05:不完整的128位服务UUID列表
  • 0x06:完整的128位服务UUID列表
  • 0x07:本地名称
  • 0x08:TX功率级别
  • 0x09:蓝牙名称
  • 0x0A:简单配对的Hash C
  • 0x0B:简单配对的随机数R
  • 0x0C:设备ID
  • 0x0D:服务数据
  • 0x0E:制造商特定数据
  • 0x0F:广播间隔

当【外设】小伙伴配置后,ios端就可以看到设备广播出来的字段kCBAdvDataServiceUUIDs

//当配置了1234,

//CBCentralManagerScanOptionAllowDuplicatesKey

//CBCentralManagerScanOptionSolicitedServiceUUIDsKey

//NSDictionary *scanOptions = nil;

//开始扫描 一般会将 serviceUUIDs 设置为nil,这样就会扫描周围所有的设备

NSArray<CBUUID *> *serviceUUIDs = @[[CBUUID UUIDWithString:@"1234"]];

[BCustomBlueMananger scanForPeripheralsWithServices:serviceUUIDs options:scanOptions];

//

//则在发现设备代理中 advertisementData 会存在一个kCBAdvDataServiceUUIDs字段值为1234

- (void)centralManager:(CBCentralManager *)central

    didDiscoverPeripheral:(CBPeripheral *)peripheral

        advertisementData:(NSDictionary<NSString *, id> *)advertisementData

                     RSSI:(NSNumber *)RSSI {} 

这里意外的发现个问题,并不是说配置了后台模式以及添加了UUID App处于后台就一定会扫描设备,处于后台有锁屏的后台、非锁屏后台,当锁屏后台时,经测试无效

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值