我基本上想从我的 iOS 应用程序中搜索附近的 BLE 设备,即使应用程序在后台也是如此。
iOS 提供了这个方法:
//_cbcManager 是 CBCentralManager 的对象
[_cbcManager scanForPeripheralsWithServices:nil options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]]
此方法将扫描所有服务(因为参数中提供了 nil)。但这在后台不起作用。当应用程序在后台时,iOS 需要特定的服务名称。
我基本上想扫描音频设备,阅读此页面:https ://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx我已经提供了音频设备可能广播的大部分通用服务。所以现在,该方法看起来像这样:
[_cbcManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"180A"], [CBUUID UUIDWithString:@"1815"], [CBUUID UUIDWithString:@"1800"], [CBUUID UUIDWithString:@"1801"], [CBUUID UUIDWithString:@"1807"], [CBUUID UUIDWithString:@"180E"], [CBUUID UUIDWithString:@"1813"], [CBUUID UUIDWithString:@"181C"]] options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]]
是否有每个 BLE 设备都会广播的特定/通用服务名称?