【问题标题】:How to exclude certain devices from BLE search query?如何从 BLE 搜索查询中排除某些设备?
【发布时间】:2018-11-15 23:13:42
【问题描述】:

我正在编写一个自定义蓝牙连接器,我希望搜索查询与 mac os 中的标准蓝牙应用程序完全相同。我怎样才能做到这一点?我正在使用 CoreBluetooth 和 IOBluetooth 框架来实现这一点,但得到了包括电视等在内的各种设备。

docs我了解到我想在其中提供特定的服务

.scanForPeripherals(withServices: [CBUUID], options: nil)

但是什么服务呢?我还找到了默认的 Xcode 蓝牙连接器,但它不包含源代码。

现在我的代码是:

let cbuuids = [CBUUID(string: "0x180A"), CBUUID(string: "0x1801"), CBUUID(string: "0x1800"), CBUUID(string: "0x1812")]

func centralManagerDidUpdateState(_ central: CBCentralManager) {
    switch central.state {
    case .poweredOn:
        print("powered on")
        central.scanForPeripherals(withServices: cbuuids, options: nil)
    case .unknown:
        print("state unknown")
    case .resetting:
        print("resetting")
    case .unsupported:
        print("unsupported")
    case .unauthorized:
        print("unauthorized")
    case .poweredOff:
        print("powered off")
    }
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    print(peripheral.name)
    devices.append(peripheral)
    devices = devices.unique

    tableView.reloadData()
    scrubber.reloadData()

}

我正在将搜索查询加载到触摸栏的洗涤器,但这不是重点。

有没有办法匹配标准应用程序中的原始设备列表?任何建议表示赞赏。

【问题讨论】:

    标签: swift macos bluetooth core-bluetooth


    【解决方案1】:

    你的代码看起来不错,但是

    let cbuuids = [CBUUID(string: "0x180A"), CBUUID(string: "0x1801"), CBUUID(string: "0x1800"), CBUUID(string: "0x1812")]

    您正在包括默认 UUID,例如 0x180A设备信息服务 我认为它包含在所有设备广告中。

    因此,当您使用此 UUID 进行搜索时,所有设备都会出现在发现中。 阅读更多关于 Gatt 服务的here

    所以解决方案是只在搜索中添加用户定义的自定义服务 UUID。

    另请注意,UUID 必须包含在您尝试在搜索中查找的设备的广告数据包中。 你可以阅读更多关于广告数据here

    【讨论】:

      猜你喜欢
      • 2020-01-04
      • 2012-05-30
      • 2012-05-12
      • 2011-06-21
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      相关资源
      最近更新 更多