ios - 发现的 UUID 与使用 nRFConnect 检查的 UUID 不匹配

标签 ios swift bluetooth-lowenergy uuid cbcentralmanager

我正在使用scanForPeripherals从我附近的设备检索 UUID 的方法。

我能够列出 UUID,但是,它们与使用 nRF Connect 发现的 UUID 不相似。应用。我尝试了各种测试场景,其中我为外围设备(在另一个应用程序中,安装在不同的手机上)配置了特定的 UUID,并尝试使用我的代码发现它,但这也被证明是不成功的(是的,nRF Connect 托管查看正确的 UUID)。

请查看此简化版本的代码:

class ViewController: UIViewController {
    var centralManager      : CBCentralManager!
    var peripherals         : [CBPeripheral] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //Initialise CoreBluetooth Central Manager
        centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)
    }
}

extension ViewController: CBCentralManagerDelegate {
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if (central.state == .poweredOn){
            self.centralManager?.scanForPeripherals(withServices: nil, options: nil)
        }
        else {
            // do something like alert the user that ble is not on
        }
    }
    
    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        peripherals.append(peripheral)
        print(peripheral.identifier.uuidString)
    }
}

我想知道为什么 UUID 看起来不同,以及如何才能发现与 nRF Connect 相同的 UUID。

最佳答案

BLE 设备本身通常没有 UUID。 CBPeripheral.identifier 是操作系统创建的内部标识符,仅供内部使用。您要查找的 UUID 是服务 UUID,它可能通过广告数据包获得,也可能只能通过发现获得。 (广告数据包中不应该存在无法通过发现获得的 UUID,但就在上周,我确实遇到了一个固件有问题的设备,就发生了这种情况,所以这是可能的。) p>

如果正在公布服务 UUID,则您可以将扫描范围限制为仅包含该 UUID 的设备,方法是将其传递给 scanForPeripherals。这是首选的扫描方式。您可以使用 CBAdvertisementDataServiceUUIDsKey 键从 advertisementData 字典中获取广告服务列表。

如果该服务未公布,则您需要调用 discoverServices。当回调 didDiscoverServices 时,您将在 peripheral.services 属性中找到服务列表。

关于ios - 发现的 UUID 与使用 nRFConnect 检查的 UUID 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63114435/

相关文章:

ios - Xcode 7 无法与源代码管理正常工作

swift - 从发送阵列数据的核心蓝牙外围设备读取数据?

swift - 从父 View 控制 NSTabViewController

android - BluetoothDevice getName() 方法在 Android 12 中返回 null

android - 原生 Android BLE 实现本质上是同步的吗?

ios - 移至下一个文本字段后立即检查 emali 验证

ios - Swift 如何计算一次值(value)并在整个应用程序中共享

ios - ionic 内容滚动在 ios 14 上停止工作

ios - 使用 Alamofire 解码 json 时出错

ios - 在 Swift 中将值从 1 个函数传递到另一个函数