Search code examples
androidionic-frameworkcapacitorcapacitor-plugin

Adding a background bluetooth android service in Ionic Capacitor


I have an app utilizing Ionic, Capacitor, and Angular. I need to be able to listen for my device type constantly in the background and establish a connection if discovered. I am currently using the Capacitor BLE package.

I tried using setInterval to establish a connection with background mode enabled; unfortunately background mode is no longer working in Android 12 API 31.

Consequently I decided I should probably create a service within Android studio that could launch when the app launches and run constantly.

I tried to add a service directly in android studio in the same folder as MainActivity but this seemed to be unsuccessful since I can't add anything to MainActivity.java to start the service without errors. I don't understand the folder structure created by Capacitor so I do not understand where the service needs to actually be added and what activity file needs to kick off the service.

Any advice is appreciated.


Solution

  • So in the end it came down to the capacitor LE library not allowing Gatt autoconnect.

    The simplest solution was to port the app to something with more direct control.

    The new app was implemented in Kotlin and the reconnection of bonded devices works as expected.