iOS CoreBluetooth connectionEventDidOccur没有被调用

时间:2020-04-10 00:40:31

标签: ios swift core-bluetooth gatt bluetooth-gatt

我有一个外围设备,它支持A2DP配置文件和 BR / EDR上的gatt 。在蓝牙经典功能的sdp记录中注册了各种gatt服务。 我已经开发了一个Android应用程序,可以成功进行如下通信:

//here scan the classic Bluetooth to get its address
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(**CLASSIC_BLUETOOTH_ADDRESS**);
device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_BREDR);

现在,我需要开发iOS应用程序,经过大量搜索,似乎iOS 13之后CoreBluetooth确实支持基于BR / EDR的GATT,但是与android不同,它无法扫描经典的蓝牙设备,但是我确实找到了样例代码 apple sample code在这里,所以我将测试代码编写如下:


import CoreBluetooth
import UIKit
import os.log

struct BTConstants {
    static let uuidService = CBUUID.init(string: "66666666-8888-7777-5555-123456780000") //our gatt service uuid(modified)
}

class CentralViewController: UIViewController {
    private var cbManager: CBCentralManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        cbManager = CBCentralManager(delegate: self, queue: nil)
    }
}

extension CentralViewController: CBCentralManagerDelegate {
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state {
        case .resetting:
            os_log("Connection with the system service was momentarily lost. Update imminent")
        case .unsupported:
            os_log("Platform does not support the Bluetooth Low Energy Central/Client role")
        case .unauthorized:
            os_log("Something went wrong. Cleaning up cbManager")
        case .poweredOff:
            os_log("Bluetooth is currently powered off")
        case .poweredOn:
            os_log("Starting cbManager")
            let matchingOptions = [CBConnectionEventMatchingOption.serviceUUIDs: [BTConstants.uuidService]]
            cbManager.registerForConnectionEvents(options: matchingOptions)
        default:
            os_log("Cleaning up cbManager")
        }
    }

    func centralManager(_ central: CBCentralManager, connectionEventDidOccur event: CBConnectionEvent, for peripheral: CBPeripheral) {
        os_log("connectionEventDidOccur ")
        switch event {
        case .peerConnected:
            os_log("Peer disconnected!")
        case .peerDisconnected:
            os_log("Peer disconnected!")
        default:
            os_log("The default!")
        }
    }
}

当我通过iOS蓝牙设置连接到设备时,没有任何反应, 回调不被调用,所以我会错过吗?任何帮助将不胜感激。

0 个答案:

没有答案