BLE connection to Android mobile phone

Hi,

I'm using NRF52840 to connect to an Android mobile phone.

The application scans for advertisement and when the NRF device starts advertising - connection is established.

The problem is that after bonding - connection isn't always established when advertising starts. Only when the application is open on Android mobile phone, everything works.

Is there any definition on NRF device that can solve that problem? To make sure the Android will connect on every advertisement after bonding?

Thanks!

Parents Reply Children
  • HID stands for Human Interface Device, which is a keyboard, mouse, a joystick, etc. Input from such devices is supported by Android, so you may connect your keyboard to the phone/tablet and use it instead of soft keyboard (displayed on the screen). The example you mentioned, ble_app_hids_keyboard, demonstrates how to implement such. But if you're not working on such device, you should rather look into how this sample is advertising. Have a look at advertising_init method (if you're using nRF5 SDK) and see that the keyboard sample is using directed advertising.

    I'm now trying to implement the same in Proximity sample, which is not natively supported by Android, and see it the phone connects to it automatically as well without any app asking for it.

  • Thank you Aleksander!

    I think I understand now.

    Yes - I can see that there is a directed advertising in the example. Do you think it can help me to connect via Android even when the app isn't open?

    As far as I understand - direct advertising requires saving of peer's BLE address, isn't it?

    It can't help me if the peer changes its address.

    Am I right?

    Thanks!

  • Still, you need to scan on Android, and when you get a scan result, you need to connect. Direct advertising has this advantage, that it's very fast, but lasts only for 1.25 sec after boot, so I understand it's good for quick reconnections. After that the device start advertising with a whitelist.

    On Android, you may call connectGatt method with autoConnect param set to true. This should keep make Android automatically reconnect if needed, but you cannot close the returned BluetoothGatt object. So for a long running operation you'd have to have a foreground service that holds this object and awaits connection. In practice, on Android 8+ it's better to scan using a PendingIntent with correct filter. You don't need to have the service in that case and your app can be even killed. When the device is scanner, you'll get the Intent which can launch BroadcastReceiver, or a Service, from which you may promote the serivce to foreground and connect to the device with autoConnect  = true.

    Actually, we should do such sample, as it's quite common. I'll add it to our backlog.

  • The address is saved on the device side in whitelist butter, yes. But I'm completely not an expert when it comes to embedded, so I don't know how it's done. Perhaps automatic by the peer manager.

Related