[toc]
业务描述
(因项目隐私问题仅简要叙述)
产品希望用户可以不必主动打开app,也能让硬件和app建立蓝牙连接进而进行数据传输,最终结合产品需求以及节省用户手机电量的初衷出发,梳理出最理想的解决方案如下:
- 用户回到家,app自动被唤起,并开始扫描连接硬件
- 用户离开家,app自动断开与硬件的连接以节省手机电量。
但是以上业务场景有两个问题:
- 从外面回到家里,app如何知道用户已回到家中,进而自动发现家中的硬件设备?一直在后台扫描周围的设备是不现实的,何况app有可能被系统或用户杀死。
- 发现了硬件设备后,app如何自动被唤起,即app如何保活?
通过一番技术方案的调研,我们锁定了iBeacon
获取当前同音箱距离+Bluetooth State Reversation and Restoration
自动唤起app的方案。
According to Apple's Developer Technical Support:
There are very few services you can use that will relaunch your app after the user force quits it. Region monitoring is one of these.
[…]Yes, Region Monitoring will wake up the app, relaunch it if necessary, and will give you 10 seconds to do what you need to do.
You can combine this with the state restoration scheme you mentioned, and that will be enough time to setup your BLE stuff and let the system wake you up again when the BLE side is connected.
Alternatively you can extend this 10 seconds to up to 180 seconds, using https://developer.apple.com/reference/uikit/uiapplication/1623031-beginbackgroundtaskwithexpiratio but that adds more complexity, and I don’t believe you need that much time.
If your BLE device is advertising properly and fast enough, you should be able to scan, discover, and connect in the 10 seconds.
iBeacon
Region Monitoring vs iBeacon Ranging
Monitoring answers the simple question:"Have I entered/left the iBeacon space yet?"
Ranging answers a different question:"How close am I to the beacon?"
pitfalls
- 蓝牙开关问题
发出iBeacon信号的设备(如智能音箱或模拟发送iBeacon信号的设备)蓝牙必须打开,接收iBeacon信号的设备,iOS 11+在控制中心关闭蓝牙,底层操作系统仍然能扫描到周围的ibeacon设备,但是iOS 11以下控制中心关闭,则无法扫描到周围的ibeacon设备;如果是在“设置”->“蓝牙”页关闭了总蓝牙开关,则对所有的iOS系统版本均无法扫描到beacon设备。
能否扫描到beacon设备 | 控制中心关闭蓝牙 | 设置页关闭蓝牙 |
---|---|---|
iOS 11+ | 能 | 不能 |
iOS 11- | 不能 | 不能 |
-
定位权限
用户必须选取“总是允许获取地理位置”选项, 才能开启“Region Monitoring”:
EDCF8FFE-3954-46FD-8A8F-89C095614587.png
Bluetooth State Preservation and Restoration
Testing the restoration
Kill the app as if it was killed by the iOS system
kill(getpid(), SIGKILL);
Limitation
- Longer intervals for scanning
- Must Explicitly specify one or more services to scan
-
CBCentralManagerOptionShowPowerAlertKey
option is ignored
Pitfalls
Can't discover any peripherals when scanning in background
确信已经按照苹果开发文档蓝牙后台操作的步骤来做:
-
D0BEADCB-F635-45A5-B215-54D6391ACA66.pngCapabilities
->Background Modes
->Uses Bluetooth LE accessories
-
扫描方法中的serviceUUIDs参数不能为空:
[_central scanForPeripheralsWithServices:_serviceUUIDs options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @NO}];
但是有些类型的手机一锁屏或进入home页,关闭蓝牙后再开启蓝牙,虽然调用了扫描方法,但是就是扫描不到任何设备,查了很多资料比如这里、这里、这里都说外设需要遵循苹果规定的广播频率:
技术实现
实现流程图
实现流程图.png具体实现代码见NMVboxBeaconManager
类。
参考
- iOS beacon region monitoring require to enable bluetooth
- iOS: stay connected to an external BLE device as much as possible
- Determining the Proximity to an iBeacon
- Detecting beacons via iBeacon Monitoring & Ranging vs CoreBluetooth scanForPeripheralsWithServices
- Region Monitoring and iBeacon
- Energy Effiency and the User Experience
- Working with iBeacons on iOS - Zero to BLE Bonus
- Core Bluetooth Background Processing for iOS Apps
- Best practice: How to deal with Bluetooth Low Energy in background
- Practical Core Bluetooth in IoT & Wearable projects
- Background Execution
- iBeacon and Background Modes
- 开发使用 iBeacon 的 iOS 7 应用
- With bluetooth-central UIBackgroundModes enabled iPhone is not discovering BLE device in background not
- Using the correct Bluetooth LE Advertising and Connection Parameters for a stable connection
- Conditions Under Which Bluetooth State Restoration Will Relaunch An App
- Zero to BLE on iOS – Part Three
- How to trigger Core Bluetooth state preservation and restoration
网友评论