12

I'm trying to create a BLE pairing user-experience screen in my app. The app scans for our BLE device, and when discovered, automatically connect it to the centralManager. The System then shows a Bluetooth Pairing Request dialog (not part of my app, but iOS's). The user has two options - Pair/Cancel. Thing is - Even when the Cancel action is being selected by the user - The peripheral still stays connected to the centralManager. (the centralManagerDidConnect function is being trigger) Even though it's not paired with the phone. Is there any way that I can know if the user chose to pair the device, or to cancel the pairing ??? Thanks!

Shahar Biran
  • 171
  • 1
  • 7
  • 2
    If pairing is successful you will be able to read an encrypted characteristic successfully – Paulw11 Nov 29 '17 at 07:10
  • Hi, I still manage to read characteristics even if I choose to cancel..., – Shahar Biran Nov 29 '17 at 09:14
  • 1
    Then why are you pairing? Pairing (actually bonding) is only initiated when an encrypted characteristic is encountered. If you don't have any need for encryption then you have no need for pairing. – Paulw11 Nov 29 '17 at 09:15
  • I may have misunderstood what do you mean by "encrypted characteristic". I first call "ConnectToPeripheral" function, and then "Discover characteristics" (after of course discovering the services). I get a positive callback even when i hit the "cancel" button in the dialog. Is that what you meant ? Our BLE device uses HID - I think that's why there's a need for pairing. – Shahar Biran Nov 29 '17 at 12:51
  • 1
    The HID portion and pairing is nothing to do with CoreBluetooth and your app. HID is managed by iOS alone. For a GATT profile (which is what is used with Core Bluetooth) pairing is only initiated when one of the characteristics is configured with encryption. This is why you can still read the characteristic regardless of the pairing state; pairing isn't required for your characteristic. You could require encryption on one of your characteristics and then if you can read that you know that pairing has taken place or you could expose the pairing state from your peripheral via a characteristic – Paulw11 Nov 29 '17 at 19:03
  • And then your app could retrieve this characteristic value to determine if the HID pairing is in place – Paulw11 Nov 29 '17 at 19:04
  • Pairing is successful and encryption is successful (Bluetooth sniffer shows Pairing confirm and encryption confirm responses), But still getting Unauthenticated error from peripheral when tried to read encrypted characteristic. Any idea? Is there any other way to know that pairing is successful other than reading the encrypted characteristic again? BTW, My peripheral has TI stack. – mdkr Dec 08 '17 at 20:44
  • Can anybody tell me that how to create Bluetooth pair request in swift? – Kudos Apr 16 '21 at 07:51

2 Answers2

5

Find my detailed answer to a similar question here.

You should basically write to a characteristic of the device after pairing and check if the write was successful

Gurunath Sripad
  • 1,308
  • 2
  • 14
  • 24
4

Apple is pretty stupid to not have any API, callback or whatsoever about pairing. The only thing I know is that you let your BLE device send a notification you listen to in your app how the pairing goes. iOS will send a "Pairing Failed" SMP command when the user presses cancel for example.

Emil
  • 16,784
  • 2
  • 41
  • 52