0

I developing app for IONIC 2. I have requirement that when my BLE device is in range auto connect to it. The case is whether my app is in background or in foreground I want to auto connect my BLE device and also if disconnect then continuously search to my device and reconnect it when in range. Is there a way to do this? I have referred this : https://github.com/don/cordova-plugin-ble-central. Also I have put question about detection of BLE connection:How to register for BLE state notification in IONIC 2 . But no solution is working related to that. But I didn't found anything. Please suggest any way to do this. I want solutionw hich should work on both ios and android.

Community
  • 1
  • 1
Pooja Shah
  • 977
  • 16
  • 45

2 Answers2

1

In Android, the correct way to set up a long running connection with the following properties:

  1. If the device is not in range, it should automatically connect when it finally comes in range (no timeout).
  2. When the device disconnects for any reason, Android should automatically reconnect when it comes in range.

is to use https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback)

where the autoConnect parameter is set to true. If it is set to false, it will have a timeout of 30 seconds and it will also not automatically reconnect when the device disconnects.

That cordova plugin you refer to unfortunately hardcodes autoConnect to false. So you may either change the plugin so autoConnect gets set to true or you can instead set up a scan where you scan forever and connect to a device you're interesting in when it appears. But that cordova plugin uses maxed scanning interval (meaning it scans 100% of the time) where usually one sets up to scan 5-10% of the time on average in order to not occupy the Wi-Fi bandwidth but also to save battery... What I say is that cordova plugin doesn't seem to be created for the purpose where you will have long running background connections that may take some time to connect.

In order to make sure your app does not get killed in the background you need to have a "Foreground Service" (https://developer.android.com/guide/components/services.html#Foreground) running somewhere in the same process that connects to BLE devices.

Emil
  • 16,784
  • 2
  • 41
  • 52
0

I myself only know about android part. I think this is not a good effort because for this to happen, Bluetooth must be always on and device should always look around for device if it is in range.

There are some ways to do it like Alarm Manager or Job Scheduler(which do what you want whether your app is in foreground or background or even reboot) but i don't recommend it because it keeps mobile alive(screen and hardware) and drain battery.

If you want to do it, set your search interval as minimum as possible. You can use below article and pages and so on for more clarification.

Link 1

Link 2

Community
  • 1
  • 1
Mehran Zamani
  • 831
  • 9
  • 31