[toc]
業(yè)務(wù)描述
(因項目隱私問題僅簡要敘述)
產(chǎn)品希望用戶可以不必主動打開app,也能讓硬件和app建立藍(lán)牙連接進(jìn)而進(jìn)行數(shù)據(jù)傳輸,最終結(jié)合產(chǎn)品需求以及節(jié)省用戶手機(jī)電量的初衷出發(fā),梳理出最理想的解決方案如下:
- 用戶回到家,app自動被喚起,并開始掃描連接硬件
- 用戶離開家,app自動斷開與硬件的連接以節(jié)省手機(jī)電量。
但是以上業(yè)務(wù)場景有兩個問題:
- 從外面回到家里,app如何知道用戶已回到家中,進(jìn)而自動發(fā)現(xiàn)家中的硬件設(shè)備?一直在后臺掃描周圍的設(shè)備是不現(xiàn)實(shí)的,何況app有可能被系統(tǒng)或用戶殺死。
- 發(fā)現(xiàn)了硬件設(shè)備后,app如何自動被喚起,即app如何?;睿?br>
通過一番技術(shù)方案的調(diào)研,我們鎖定了
iBeacon獲取當(dāng)前同音箱距離+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
- 藍(lán)牙開關(guān)問題
發(fā)出iBeacon信號的設(shè)備(如智能音箱或模擬發(fā)送iBeacon信號的設(shè)備)藍(lán)牙必須打開,接收iBeacon信號的設(shè)備,iOS 11+在控制中心關(guān)閉藍(lán)牙,底層操作系統(tǒng)仍然能掃描到周圍的ibeacon設(shè)備,但是iOS 11以下控制中心關(guān)閉,則無法掃描到周圍的ibeacon設(shè)備;如果是在“設(shè)置”->“藍(lán)牙”頁關(guān)閉了總藍(lán)牙開關(guān),則對所有的iOS系統(tǒng)版本均無法掃描到beacon設(shè)備。
| 能否掃描到beacon設(shè)備 | 控制中心關(guān)閉藍(lán)牙 | 設(shè)置頁關(guān)閉藍(lán)牙 |
|---|---|---|
| iOS 11+ | 能 | 不能 |
| iOS 11- | 不能 | 不能 |
-
定位權(quán)限
用戶必須選取“總是允許獲取地理位置”選項, 才能開啟“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
-
CBCentralManagerOptionShowPowerAlertKeyoption is ignored
Pitfalls
Can't discover any peripherals when scanning in background
確信已經(jīng)按照蘋果開發(fā)文檔藍(lán)牙后臺操作的步驟來做:
-
Capabilities->Background Modes->Uses Bluetooth LE accessories
D0BEADCB-F635-45A5-B215-54D6391ACA66.png 掃描方法中的serviceUUIDs參數(shù)不能為空:
[_central scanForPeripheralsWithServices:_serviceUUIDs options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @NO}];
但是有些類型的手機(jī)一鎖屏或進(jìn)入home頁,關(guān)閉藍(lán)牙后再開啟藍(lán)牙,雖然調(diào)用了掃描方法,但是就是掃描不到任何設(shè)備,查了很多資料比如這里、這里、這里都說外設(shè)需要遵循蘋果規(guī)定的廣播頻率:



技術(shù)實(shí)現(xiàn)
實(shí)現(xiàn)流程圖

具體實(shí)現(xiàn)代碼見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
- 開發(fā)使用 iBeacon 的 iOS 7 應(yīng)用
- 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

