gpt4 book ai didi

swift - 扫描特定 CBUUID IOS 时,核心蓝牙找不到外围设备(前台和后台)

转载 作者:行者123 更新时间:2023-11-30 12:27:58 25 4
gpt4 key购买 nike

我遇到了核心蓝牙在扫描特定 CBUUID 时找不到外围设备的问题,并且我还想在前台和后台运行我的应用程序。但它是 didDiscover 所有近外围设备扫描,没有 CBUUID “manager.scanForPeripherals( withServices: nil, options: nil)".am 使用“MactsAsBeacon”进行广播信标模拟器。但是为什么它不能使用特定 CBUUID 的 scanForPeripherals 工作?以及它将如何在后台工作? “我启用了蓝牙中心”我想在后台做任何额外的工作吗?

var manager:CBCentralManager!
var peripheralCB:CBPeripheral!
var peripherals = [CBPeripheral]()

override func viewDidLoad() {
super.viewDidLoad()

manager = CBCentralManager(delegate: self, queue: nil)

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {
if let peripheralsObject = dict[CBCentralManagerRestoredStatePeripheralsKey] {
// 2
let peripherals = peripheralsObject as! Array<CBPeripheral>
// 3
if peripherals.count > 0 {
// 4
peripheralCB = peripherals[0]
// 5
peripheralCB?.delegate = self
}
}
}

func centralManagerDidUpdateState(_ central: CBCentralManager) {
var consolMessages = ""

switch central.state
{
case .poweredOff:
consolMessages = "BLE is powered off"

case.poweredOn:
consolMessages = "BLE is powered on"
let serviceUUIDs = [CBUUID(string: "B0702880-A295-A8AB-F734-031A98A512DE") as AnyObject]
let dictionaryOfOptions = [CBCentralManagerScanOptionAllowDuplicatesKey : false]
manager.scanForPeripherals(withServices: serviceUUIDs as? [CBUUID], options:dictionaryOfOptions)
// manager.scanForPeripherals(withServices: nil, options:nil)

case.resetting:
consolMessages = "BLE is resetting"

case.unauthorized:
consolMessages = "BLE is unauthorized"

case.unknown:
consolMessages = "BLE is unknown"

case.unsupported:
consolMessages = "unsupported"

}
print("\(consolMessages)")
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

print("peripheral \(peripheral)")
print("peripheral Name \(peripheral.name)")

if #available(iOS 9.0, *) {
peripheral.accessibilityAssistiveTechnologyFocusedIdentifiers()
} else {
// Fallback on earlier versions
}
print("peripheral Name \(peripheral.name)")
peripherals.append(peripheral)
manager.connect(peripheral, options: nil)

let AdvertsatingData = advertisementData[CBAdvertisementDataManufacturerDataKey]

print("AdvertsatingData\(AdvertsatingData)")


}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {

print("peripheral Connected")
print("peripheral didConnect \(peripheral)")
print("Connected peripheral Name \(peripheral.name)")


}

func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {

let alert = UIAlertController(title: "Alert", message: "didFailToConnect", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {

print("peripheral Disconnectd")

print("Disconnect peripheral Name \(peripheral.name)")
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {

if error != nil{

}
else {
print("didDiscoverCharacteristicsFor")

}
}[![This is the Beacon Simulator /image/diyWA.png ][1]][1]

最佳答案

这不起作用的几个原因:

  • MactsAsBeacon 发送 iBeacon 广告,这些广告是 BLE 制造商广告,并且没有蓝牙服务 UUID。 请勿将 iBeacon ProximityUUID 与您用于通过 CoreBluetooth 蓝牙进行扫描的服务 UUID 混淆。它们是根本不同的东西。

  • 除非提供服务 UUID,否则 CoreBluetooth 将不会获得后台回调。这是苹果公司施加的限制。

  • 即使在前台,CoreBluetooth 也无法从 iBeacon 数据包的数据中读取信标标识符。 Apple 会清除 iOS 上收到的 iBeacon 数据包的这些数据。

要在 iOS 上读取信标数据包,您必须使用 CoreLocation API。没有其他办法。

请小心确保您可以使用现成的 iOS 信标检测器检测来自 MactsAsBeacon 的传输。 Mac 程序无法在某些版本的 MacOS 上传输。

关于swift - 扫描特定 CBUUID IOS 时,核心蓝牙找不到外围设备(前台和后台),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43935559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com