无法通过CoreBluetooth(macos Swift)发现蓝牙鼠标和键盘

时间:2018-05-30 08:46:26

标签: swift macos bluetooth core-bluetooth

我使用CoreBluetooth框架的设备有问题。基本上我需要在OSX中复制标准蓝牙发现应用程序的功能,所以我做了以下几点:

let bluetoothManager = CBCentralManager()

//Lifecycle

 override func viewDidLoad() {
    super.viewDidLoad()
    bluetoothManager.delegate = self
}

//delegate method

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

此代码仅发现iphone和电视,但忽略了苹果的键盘和鼠标。有什么我想念的吗?也许我需要一些服务来定义withServices:?

现在我只是将外围设备打印到控制台:

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

我搜索了bluetooth.com规格,但没有运气

任何建议都会很好!

1 个答案:

答案 0 :(得分:2)

核心蓝牙只能发现BLE GATT配置文件设备。对于旧版设备和配置文件(例如键盘公布的HID配置文件),您需要使用IOBluetooth framework