gpt4 book ai didi

bluetooth - 在 OSX 和 iOS (GAP/GATT) 上发现外设时,CBPeripheral 广告数据不同

转载 作者:行者123 更新时间:2023-12-03 16:15:36 29 4
gpt4 key购买 nike

我希望将我的一些 CoreBluetooth 代码从 iOS 移植到 OS X。我已经设置了一组共享的 CoreBluetooth 包装器,iOS 应用程序和 OS X 应用程序使用相同的 BLE 设备以完全相同的方式使用这些包装器.

外围设备扫描:

override init() {
super.init()
let queue = DispatchQueue.global(qos: .background)
centralManager = CBCentralManager(delegate: self, queue: queue)
}

func startScanning() {
let options: [String: Any] = [CBCentralManagerScanOptionAllowDuplicatesKey: true]
let deviceUUID = CBUUID(string: Project.Service.Device)
let recoveryUUID = CBUUID(string: Project.Service.DFURecovery)
centralManager?.scanForPeripherals(withServices: [deviceUUID, recoveryUUID], options: options)
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber){
// Inspect advertisementData here to decipher what kind of device
}

在我的 iOS 应用中,didDiscoverPeripheral 被触发。然后当我检查广告数据时,我得到了我期望的所有键/值:
{
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = "My Device";
kCBAdvDataManufacturerData = <34045254 5877f283 43fdd12d ff530978 45000000 000050c2 6500>;
kCBAdvDataServiceData = {
Battery = <64>;
};
kCBAdvDataServiceUUIDs = (
"My Inforamtion"
);
}

但是,当从 OS X 应用程序运行相同的代码(扫描相同的设备)时,广告数据会丢失某些字段。
{
kCBAdvDataIsConnectable = 1;
kCBAdvDataManufacturerData = <34045254 5877f36e 43fdd12d ff530978 45000000 000050c2 6500>;
}

AdvertedData 中缺少以下键/值对。
kCBAdvDataLocalName
kCBAdvDataServiceData
kCBAdvDataServiceUUIDs

我已经尝试将这些键添加到 scanForPeripherals 调用中,如下所示:
    let options: [String: Any] = [CBCentralManagerScanOptionAllowDuplicatesKey: true,
CBAdvertisementDataLocalNameKey: true,
CBAdvertisementDataServiceDataKey: true,
CBAdvertisementDataServiceUUIDsKey: true]
let deviceUUID = CBUUID(string: Nightlight.Service.Device)
let recoveryUUID = CBUUID(string: Nightlight.Service.DFURecovery)
centralManager?.scanForPeripherals(withServices: [deviceUUID, recoveryUUID], options: options)

没有效果。

最佳答案

OSX 可能会在每个设备上多次调用 didDiscoverPeripheral,每次调用都使用不同的 adsData。我想出的解决方案是写一个缓存。

import Foundation
import CoreBluetooth

class AdvertisementDataCache {
/// A dictionary of advertised data to peripheral.uuid
private var cache: [UUID: [String: Any]] = [:]

/// Appends advertisementData to our cache
/// for each unique `peripheral.uuid`.
func append(
advertisementData: [String: Any],
to peripheral: CBPeripheral
) -> [String: Any] {
// Join our cached adverts (our `cache` ivar)
// with new adverts (`advertisementData`)
let joined = advertisementData.reduce(
cache[peripheral.identifier] ?? [:]
) {
var all = $0
all[$1.key] = $1.value
return all
}

// write back to our private iVar
cache[peripheral.identifier] = joined

// Return all cached averts for this peripheral
return joined
}

/// Purges all cached avertisements for all peripherals
func clear() {
cache.removeAll()
}
}
然后在 didDiscoverPeripheral 中:
private var advertisementDataCache = AdvertisementDataCache()

public func centralManager(
_ central: CBCentralManager,
didDiscover peripheral: CBPeripheral,
advertisementData adPart: [String: Any],
rssi RSSI: NSNumber
) {
let advertisementData = advertisementDataCache.append(
advertisementData: adPart,
to: peripheral
)

// advertisementData now contains all data contained in multiple callbacks
}

关于bluetooth - 在 OSX 和 iOS (GAP/GATT) 上发现外设时,CBPeripheral 广告数据不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41628114/

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