7

I have a peripheral device which supports A2DP profile and gatt over BR/EDR. Various gatt services are registered in the sdp record for bluetooth classic. I have developed an Android app to communicate with it successfully as follows :

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

Now I need to develop an iOS app, after lots of searches, It seems that GATT over BR/EDR does supported by CoreBluetooth after iOS 13, but unlike android,it cannot scan the classic Bluetooth devices, but I do find the sample code apple sample code here, so I write my test code as follows:


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!")
        }
    }
}

When I connect to my device from iOS Bluetooth Settings, nothing happens, The callback is not called,so do I miss something? Any help will be appreciated.

Tiger Wang
  • 119
  • 9

0 Answers0