我想知道是否有一种方法可以从一个IOBluetoothDevice *对象中获得一个CBPeripheral *对象,因为我正在制作一个高级蓝牙控制器框架(它将基于IOBluetooth),并且我正在使用它来扫描Bluetooth Classic和Bluetooth Low Energy设备。以下是一些问题:
IOBluetooth确实允许您搜索这两个网络,但出于某种原因,它没有显示CoreBluetooth的所有Bluetooth Low Energy设备。CoreBluetooth搜索Bluetooth Low Energy设备,我将无法获得以后使用所需的地址。那么,有什么方法可以从IOBluetoothDevice中获取CBPeripheral对象呢?
谢谢:D
发布于 2018-03-20 06:27:44
我发现我可以通过/Library/Preferences/com.apple.bluetooth.plist > CoreBluetoothCache搜索,它恰好包含UUID及其字典DeviceAddress = address ;)。
因此,我可以使用该方法获得CBPeripheral的UUID。
NSString *uuid = [[<*CBPeripheral*> identifier] UUIDString]; 然后在属性列表中查找
NSDicionary *btdict = [NSDictionary dictionaryWithContentsOfFile:@"/Library/Preferences/com.apple.bluetooth.plist"];
NSDictionary *bleDevices = [btDict objectForKey:@"CoreBluetoothCache"];
NSString *address = [[bleDevices objectForKey:uuid] objectForKey:@"DeviceAddress"];然后使用该地址创建一个新的IOBluetoothDevice:
IOBluetoothDevice *device = [IOBluetoothDevice deviceWithAddressString:address];就这么简单:D
发布于 2021-02-06 14:55:31
Swift 5.3解决方案:
fileprivate func getDeviceAddress(for cbPeripheral: CBPeripheral) -> String?
{
if let userDefaults = UserDefaults(suiteName: "/Library/Preferences/com.apple.Bluetooth")
{
if let coreBluetoothCache = userDefaults.object(forKey: "CoreBluetoothCache") as? [String : Any]
{
if let deviceData = coreBluetoothCache[cbPeripheral.identifier.uuidString] as? [String : Any]
{
return deviceData["DeviceAddress"] as? String
}
}
}
return nil
}用法:
if let deviceAddress = self.getDeviceAddress(for: peripheral)
{
if let bluetoothDevice = IOBluetoothDevice(addressString: deviceAddress)
{
// Do your stuff
}
}https://stackoverflow.com/questions/49144183
复制相似问题