CoreBluetooth pairing feedback / callback
Asked Answered
S

1

22

It feels like I'm missing something here, but how can I get feedback on whether pairing a passcode protected peripheral failed or succeeded?

When I to connect a peripheral which is password protected the password UIAlertView pops up and the peripheral connects (didConnectPeripheral is called) and disconnects (didDisconnectPeripheral) immediately.

[bluetoothManager connectPeripheral:peripheral options:nil];

Now whether I enter the correct passcode, the wrong passcode or simply press cancel: on all occasions I don't receive any feedback from the CoreBluetooth delegate methods.

The question is how can I get feedback on this process?

Swath answered 8/1, 2015 at 9:7 Comment(5)
can you please explain he situation more vividly.If possible, do post some code explaining your problem.Carillonneur
I don't know how to explain it better than this? I added the code I use to connect to the Peripheral but I doubt if that's of any help.Swath
Why do you need the callback at all? The OS should handle pairing on its own.Paranoid
I'd like to know whether it succeeded or not, and then perform some stuffSwath
If I remember well, when trying to read the data from a characteristic, it should show an error if the pairing (needed) failed.Glorygloryofthesnow
P
19

Faced the same issue after years of the question being posted here. Surprisingly Apple does not provide any callbacks on whether the pairing was successful. However the following steps can be used to conclude the same:

  1. Declarations and initializations:
var centralManager: CBCentralManager?
var myPeripheral: CBPeripheral?
var peripheralManager: CBPeripheralManager?

centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)
peripheralManager = CBPeripheralManager.init(delegate: self, queue: DispatchQueue.main )

  1. Scan for devices when the CBCentralManager is in .poweredOn state:
func centralManagerDidUpdateState(_ central: CBCentralManager) {
   if central.state == .poweredOn {
       centralManager?.scanForPeripherals(withServices: [CBUUID.init(string: "SERVICE-ID")])
   }
}
  1. Identify and connect to the device of interest:
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    //Identify the device to be connected
    if peripheral.name?.hasSuffix("DEVICE-SERIAL-NUMBER") ?? false {
        myPeripheral = peripheral
        peripheral.delegate = self
        centralManager?.connect(myPeripheral!, options: nil)
    }
}
  1. Discover the services of the connected device and then the characteristics of those services
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        peripheral.discoverServices([CBUUID.init(string: "SERVICE-ID-STRING")])
    }

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {       
    let services = peripheral.services!
    let charId = CBUUID.init(string: “CHARACTERISTIC-ID”)
    for service in services {
        peripheral.discoverCharacteristics([charId], for: service)
    }
}
  1. For one of these characteristics which has the .notify property, write some data with write type as .withResponse
    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    let value = 1234
    let data = withUnsafeBytes(of: value) { Data($0) }
    for characteristic in service.characteristics!
    {
        if characteristic.properties.contains(.notify) {
            peripheral.setNotifyValue(true, for: characteristic)
            peripheral.writeValue(data, for: characteristic, type: .withResponse)   
        }
    }
}
  1. Check the response of this write to identify if the pairing was successful:
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) { }

If the pairing was not successful because of invalid passcode entry or cancellation by the user, you will get an error saying “Authentication is insufficient”

Else the write to the characteristic will be successful and error object will be nil.

Proviso answered 12/2, 2019 at 6:48 Comment(4)
Any other solution for this issue? What happens if the characteristics were discovered before a successful bonding? Is it reasonable to try writing to a characteristic without identifying if bonding was successful?Heteromerous
@NehaVipin Facing the Same issue. Did you manage to solve it? any help please?Coracorabel
@Sudhanshu, Our firmware added characteristics that notify if it is paired to the iPhone. When the BLE device connects to the phone, the app keeps the characteristics with notification for use later then once the app receives this notification app will start enabling notifications for the other characteristics.Heteromerous
this was edifying even if I don't know whether it's part of my solutionPuckett

© 2022 - 2024 — McMap. All rights reserved.