BLE在后台不起作用-iOS/Swift
创始人
2024-12-22 18:00:11
0次

在应用程序的Info.plist文件中添加一些键值对,允许BLE在应用程序被推送到后台时继续操作。

  1. 添加以下键值对:

Privacy - Bluetooth Always Usage Description Privacy - Bluetooth Peripheral Usage Description

键值对的值应该是一个字符串,用于描述应用程序需要使用蓝牙的原因。

  1. 在AppDelegate.swift文件中,添加以下代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. UIApplication.shared.registerForRemoteNotifications() return true }

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}) print("APNs device token: (deviceTokenString)") }

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { print(error.localizedDescription) }

  1. 在ViewController.swift文件中,添加以下代码:

import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate {

var centralManager: CBCentralManager!

override func viewDidLoad() {
    super.viewDidLoad()

    centralManager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey: "MyCentralManager"])
}

func centralManagerDidUpdateState(_ central: CBCentralManager) {
    switch central.state {
    case .poweredOn:
        print("Bluetooth On")
        break
    case .poweredOff:
        print("Bluetooth Off")
        break
    case .unsupported:
        print("Bluetooth Unsupported")
        break
    case .unauthorized:
        print("Bluetooth Unauthorized")
        break
    case .resetting:
        print("Bluetooth Resetting")
        break
    case .unknown:
        print("Bluetooth Unknown")
        break
    default:
        break
    }
}

}

这段代码会创建一个新的CBCentralManager对象,用于管理蓝牙的连接和断开。这个对象将使用CBCentralManagerOptionRestoreIdentifierKey选项

相关内容

热门资讯

WPK辅助透视挂模块!德普之星... WPK辅助透视挂模块!德普之星透视辅助软件激活码,德扑之星ai多功能透视挂软件1、在德普之星透视辅助...
终于知道辅助器!星悦麻将ai辅... 终于知道辅助器!星悦麻将ai辅助器多功能插件,先前是真的有辅助器教程积累是一款可以让一直输的玩家,快...
2027版作弊器!爱摩罗保定麻... 2027版作弊器!爱摩罗保定麻将作弊器多功能AI插件,爱摩罗保定麻将切实存在有作弊器插件暗藏猫腻,小...
WPK透视版透视技法!wepo... WPK透视版透视技法!wepoker模拟器哪个好用,WPKai多功能透视插件;1、这是跨平台的wep...
透牌教程辅助挂!六六麻将ai辅... 透牌教程辅助挂!六六麻将ai辅助挂多功能插件,好像是有辅助挂攻略指南AI 是一款可以让一直输的玩家修...
2027版开挂!打牌网开挂多功... 2027版开挂!打牌网开挂多功能AI插件,打牌网本来真的有开挂插件一、打牌网游戏安装教程牌型概率发牌...
WPK辅助器辅助器模块!wep... WPK辅助器辅助器模块!wepoker插件程序激活码,WPKai多功能辅助器软件1、实时wepoke...
2027版辅助器!至友互娱辅助... 2027版辅助器!至友互娱辅助器多功能AI方法,至友互娱真是真的有辅助器方法至友互娱辅助器是一种具有...
总算了解!透视!川滇互娱麻将a... 您好:川滇互娱麻将这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
WPK脚本透视总结!wepok... WPK脚本透视总结!wepoker辅助是真的假的,WePoKerai多功能透视攻略运wepoker辅...