本人在開發中要用到藍牙連接外設,但是藍牙在沒打開的情況下,系統每次都提示,我們只需檢測藍牙是否打開關閉,下面是解決辦法希望可以幫助到遇到同樣坑的碼農!
467360DB-10DC-446F-91A5-560BF8E70729.png
這樣寫在藍牙未打開時會有系統提示框:
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
關閉代碼:
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
這樣寫就會關閉。
options 參數講解
*
* @seealso CBCentralManagerOptionShowPowerAlertKey 藍牙系統為關閉狀態,是否向用戶顯示警告對話框
* @seealso CBCentralManagerOptionRestoreIdentifierKey
*
還有一個坑iOS 10以上跳轉到藍牙設置界面代碼發生變化,以下為解決代碼:
NSURL *url = [NSURL URLWithString:@"App-Prefs:root=Bluetooth"];
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
}