正确使用IOBluetoothHandsFree接收设备数据

时间:2018-10-21 09:03:01

标签: swift iobluetooth

我正在尝试从连接的蓝牙设备接收数据(例如音频电平)。

该设备已连接到我的Mac,并且工作正常。该设备支持我想用来接收一些设备信息的HFP(免提配置文件)。

代码如下:

func connect()
{
    if let bluetoothDevice = IOBluetoothDevice(addressString: "Bluetooth Device ID")
    {
        if !bluetoothDevice.isPaired() || !bluetoothDevice.isConnected() { continue }                    
        // Device is connected and paired, everything works fine until here.
        if bluetoothDevice.isHandsFreeDevice
        {
           // This line will also be executed, so the device supports the HFP.
           let handsFree = IOBluetoothHandsFree(device: bluetoothDevice, delegate: self)
           // handsFree?.connect()
           // Swift.print(handsFree?.isConnected) // returns false.
           let batteryValue = handsFree?.indicator(IOBluetoothHandsFreeIndicatorBattChg)
           Swift.print(batteryValue) // Always returns zero, can't be true.
           Swift.print(handsFree?.outputVolume) // Always returns 0.5.
        }
    }
}

extension Controller : IOBluetoothHandsFreeDelegate
{
    func handsFree(_ device: IOBluetoothHandsFree!, disconnected status: NSNumber!)
    {
        Swift.print("disconnected")
    }
    func handsFree(_ device: IOBluetoothHandsFree!, scoConnectionClosed status: NSNumber!)
    {
        Swift.print("scoConnectionClosed")
    }

    func handsFree(_ device: IOBluetoothHandsFree!, scoConnectionOpened status: NSNumber!)
    {
        Swift.print("scoConnectionOpened")
    }

    func handsFree(_ device: IOBluetoothHandsFree!, connected status: NSNumber!)
    {
        Swift.print("connected status: " + status.description)
    }

    func handsFree(_ device: IOBluetoothHandsFreeDevice!, batteryCharge: NSNumber!)
    {
         Swift.print("batteryCharge: " + batteryCharge.description)
    }
}

执行该行时

let handsFree = IOBluetoothHandsFree(device: bluetoothDevice, delegate: self)

我收到以下错误:

  

子类必须重写(IOBluetoothSDPUUID *)deviceUUID   子类必须重写(IOBluetoothSDPUUID *)localUUID   从IOServiceOpen()-0xe00002c7收到错误。清空io_service_t。   [AudioHAL_Client] AudioHardware.cpp:1046:AudioObjectAddPropertyListener:AudioObjectAddPropertyListener:没有具有给定ID 0的对象

我假定必须在委托中重写这些属性,但是没有此类属性。我想念什么?如何正确连接到设备(或使用已连接的设备)与它们通信?

0 个答案:

没有答案