Thread 1: “State restoration of CBCentralManager is only allowed for applications that have specified the \”bluetooth-central\” background mode”

春麗 S.T.E.M.
4 min readMay 6, 2024

--

目錄
1
. CoreBluetooth
2. Info.plist

⦿ CoreBluetooth
⦿ Info.plist

CoreBluetooth

當 APP 加入藍芽功能時,我們會 import CoreBluetooth,欲呈現像下方這樣的頁面。

即是藍芽搜尋設備,等到佈建好藍芽的程式,當 Build 後卻 Crash,debug area 出現如下訊息:

*** Assertion failure in -[CBCentralManager initWithDelegate:queue:options:], CBCentralManager.m:370
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'State restoration of CBCentralManager is only allowed for applications that have specified
the "bluetooth-central" background mode'

這表示說在 CBCentralManger 在 Restore 的時候必須要加入背景執行權限,下面我們就來在 Info.plist 加入相應權限。

回目錄

繼續閱讀|回目錄

Info.plist

點進專案下方的 Info.plist,從空白處右鍵 add row。

輸入 UIBackgroundModes,在 Information Property List 中,就會自動轉換成型別為 Array 的 Required background modes。

接著,在 Item 0 後方輸入 bluetooth-central,系統也會自動轉換成 App communicate using CoreBluetooth。

我們可以對 Info.plist 以 Source Code 檢視,會呈現如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

<dict>
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
</array>

<key>UIApplicationSceneManifest</key>

在之前的文章說過 Xcode 中有些檔案以 Source Code 呈現是 XML,像是 Storyboard,參考如下:

而 Info.plist 的 Source Code 也是如標記式語言一樣排列,在 dict(字典)中,key 是 UIBackgroundModes,而 value 是 Array,第一個(Item 0)為 bluetooth-central,於是就成了我們看到 List。

這次就分享到這,感謝您的閱讀。

回目錄

繼續閱讀|回目錄

--

--

春麗 S.T.E.M.
春麗 S.T.E.M.

Written by 春麗 S.T.E.M.

Do not go gentle into that good night, Old age should burn and rave at close of day; Rage, rage, against the dying of the light.

No responses yet