aws / amazon-freertos-ble-android-sdk

Android SDK for FreeRTOS Bluetooth Devices.
Apache License 2.0
49 stars 41 forks source link

PIN Based Bluetooth Connection #50

Closed UVohra closed 1 year ago

UVohra commented 1 year ago

I am trying to connect to a Bluetooth device which is restricted with Pin Authentication but cannot see an option to pass that PIN via the Android SDK.

System info Android version: 13

To Reproduce Frequency: No Support Steps to reproduce the behavior: Connect with Bluetooth device restricted with pin

shubnil commented 1 year ago

Hi, Can you elaborate more on this. We always try to have queries specific to FreeRTOS, so it might be more helpful if you post this queries in other forums like EmbedTLS.

kstribrnAmzn commented 1 year ago

From what I can tell, the general flow is something like this:

  1. Your peripheral will need to broadcast out it's intent to pair using a pin. This intent to pair is given through an action which in turn will have it's "extra int" field set (aka EXTRA_PAIRING_VARIANT) to the PAIRING_VARIANT_PIN value which indicates the need to pair with a pin.
  2. Your broadcast receiver will receive this, discover the action, and then set the BLE devices own pin with the setPin method before the BLE object can initiate the connection process.

For some code pointers:

For reference, I found this StackOverflow post helpful. There is also example code provided with it. The BLE 4.2 corespec volume 3, part H, chapter 2.3.5 may also be helpful.

mukesh-simform commented 1 year ago

@kstribrnAmzn Thank you for your suggestions here.

As suggested I’ve updated Broadcast Receiver with ACTION_PAIRING_REQUEST and added logic there to pass the custom PIN using setPin method.

Screenshot 2023-06-06 at 3 01 17 PM

Before that even registered the ACTION_PAIRING_REQUEST receiver. But I’ve observed that registerBondStateCallback method never gets triggered in my case and as a result, I’m not able to setPin.

Screenshot 2023-06-06 at 3 01 46 PM Screenshot 2023-06-06 at 3 02 25 PM

Peripheral already broadcasting intent to Pair with PIN and I’m getting a default OS popup to enter the PIN that I don’t want instead I want to pass a custom PIN and even don’t want the user to see that default OS popup

Screenshot 2023-06-05 at 5 55 57 PM

Maybe it will require some additional changes. Kindly let me know if that's the case and suggest how can I manage to setPin using this SDK without showing user a default popup

kstribrnAmzn commented 1 year ago

Thanks for really diving into this @mukesh-simform! I wonder if entering the pin manually is really needed. This stack overflow post seems to suggest that the dialog is in error and goes away once the setPin call is made. The user found abortBroadcast(); was a way to remove this dialog. That being said this post is 3 years old so I'm not sure if this was some sort of bug which was patched out.

Overall - I'm not sure the issue here is due to our SDK. There are a number of questions around this various topic posted online and all answers suggest this dialog is a system dialog and cannot be hidden. I would recommend either...

  1. Using 'Just Works' pairing. By doing so, there is no pin pop up. You could have the initiating device verify the pin is sent by the requesting device once the connection is established, like is suggested here.
  2. Considering an already loaded pin (if you must have the pin) however this will lead to a popup AND is a bad solution if your embedded device has no way of displaying the pin. See this Bluetooth Blog post for more.
AniruddhaKanhere commented 1 year ago

@mukesh-simform can you please take a look at the above comment from @kstribrnAmzn and verify whether that is the case?

Thanks, Aniruddha

mukesh-simform commented 1 year ago

@AniruddhaKanhere @kstribrnAmzn I've managed this with some additional changes in a local copy of SDK and It's working fine on Android

AniruddhaKanhere commented 1 year ago

Thank you for replying to us @mukesh-simform.

I shall close this issue now.