iBeacon定位+BLE自動喚醒a(bǔ)pp技術(shù)方案調(diào)研

[toc]

業(yè)務(wù)描述

(因項目隱私問題僅簡要敘述)
產(chǎn)品希望用戶可以不必主動打開app,也能讓硬件和app建立藍(lán)牙連接進(jìn)而進(jìn)行數(shù)據(jù)傳輸,最終結(jié)合產(chǎn)品需求以及節(jié)省用戶手機(jī)電量的初衷出發(fā),梳理出最理想的解決方案如下:

  1. 用戶回到家,app自動被喚起,并開始掃描連接硬件
  2. 用戶離開家,app自動斷開與硬件的連接以節(jié)省手機(jī)電量。

但是以上業(yè)務(wù)場景有兩個問題:

  1. 從外面回到家里,app如何知道用戶已回到家中,進(jìn)而自動發(fā)現(xiàn)家中的硬件設(shè)備?一直在后臺掃描周圍的設(shè)備是不現(xiàn)實(shí)的,何況app有可能被系統(tǒng)或用戶殺死。
  2. 發(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

  1. 藍(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- 不能 不能
  1. 定位權(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

  1. Longer intervals for scanning
  2. Must Explicitly specify one or more services to scan
  3. CBCentralManagerOptionShowPowerAlertKey option is ignored

Pitfalls

Can't discover any peripherals when scanning in background

確信已經(jīng)按照蘋果開發(fā)文檔藍(lán)牙后臺操作的步驟來做:

  1. Capabilities->Background Modes->Uses Bluetooth LE accessories

    D0BEADCB-F635-45A5-B215-54D6391ACA66.png

  2. 掃描方法中的serviceUUIDs參數(shù)不能為空:

    [_central scanForPeripheralsWithServices:_serviceUUIDs options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @NO}];

但是有些類型的手機(jī)一鎖屏或進(jìn)入home頁,關(guān)閉藍(lán)牙后再開啟藍(lán)牙,雖然調(diào)用了掃描方法,但是就是掃描不到任何設(shè)備,查了很多資料比如這里、這里、這里都說外設(shè)需要遵循蘋果規(guī)定的廣播頻率:

92093A4A-4167-4FD3-A177-34C9E56C65A9.png

A85D829E-2576-41D8-B0C5-CDC36707EA1A.png
C3A06CA9-F3DB-4A81-A9F3-54FBEA852289.png

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

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

實(shí)現(xiàn)流程圖.png

具體實(shí)現(xiàn)代碼見NMVboxBeaconManager類。

參考

  1. iOS beacon region monitoring require to enable bluetooth
  2. iOS: stay connected to an external BLE device as much as possible
  3. Determining the Proximity to an iBeacon
  4. Detecting beacons via iBeacon Monitoring & Ranging vs CoreBluetooth scanForPeripheralsWithServices
  5. Region Monitoring and iBeacon
  6. Energy Effiency and the User Experience
  7. Working with iBeacons on iOS - Zero to BLE Bonus
  8. Core Bluetooth Background Processing for iOS Apps
  9. Best practice: How to deal with Bluetooth Low Energy in background
  10. Practical Core Bluetooth in IoT & Wearable projects
  11. Background Execution
  12. iBeacon and Background Modes
  13. 開發(fā)使用 iBeacon 的 iOS 7 應(yīng)用
  14. With bluetooth-central UIBackgroundModes enabled iPhone is not discovering BLE device in background not
  15. Using the correct Bluetooth LE Advertising and Connection Parameters for a stable connection
  16. Conditions Under Which Bluetooth State Restoration Will Relaunch An App
  17. Zero to BLE on iOS – Part Three
  18. How to trigger Core Bluetooth state preservation and restoration
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容