2019年10月7日 星期一

iOS swift 5: iOS 13.x CoreBluetooth Framework 發生 sigabrt錯誤

iOS 13.x CoreBluetooth Framework 發生 sigabrt錯誤

簡單的說 就是只要使用到 CBCentralManager, 就要增加這個Key
在 info.plist 增加 NSBluetoothAlwaysUsageDescription 機碼

 Privacy – Bluetooth Always Usage Description
  NSBluetoothAlwaysUsageDescription

如果要能在 iOS 12.x 繼續使用, 則保留以前使用的 NSBluetoothPeripheralUsageDescription

實務上,  info.plist 共使用下面幾個
  <key>NSBluetoothAlwaysUsageDescription</key>
  <string>Play with BLE Compatible devices</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Play with BLE Compatible devices</string>
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
<string>bluetooth-peripheral</string>
</array>


如果系統設定的 Privacy -> Bluetooth -> authorized被關閉
程式的 central.state就會回傳 .unauthorized









 func centralManagerDidUpdateState(_ central: CBCentralManager)
 {
   switch( central.state)
   {
    case .poweredOff:
        // "Bluetooth powered off"
    case .poweredOn:
        // "Bluetooth powered on"
        // Do Scanner Peripherals
    case .resetting:
        //   
    case .unauthorized:
        // "Bluetooth unauthorized"
    case .unknown:
        // 
    case .unsupported:
        // 
    default:
        // 
   }
 }

 另外別太相信有些教學的 RePaired功能
 已經把 Peripherals 斷電, 但 retrievePeripherals() 還是回傳找到 Peripherals....
 我的作法是重新呼叫 CBCentralManager.init() 一且重頭來
 這也是為何連線一些藍芽耳機要把藍芽關閉重開的原因

解決方案來源:
The Ultimate Guide to Apple’s Core Bluetooth
Property List Key: NSBluetoothAlwaysUsageDescription
 A message that tells the user why the app needs access to Bluetooth.


Bluetooth permission recommendation in iOS 13

沒有留言:

張貼留言