CodenameOne background task for BluetoothLe connec

2019-04-12 11:41发布

问题:

I am developing an app using Codename One, which connects to a Bluetooth LE proprietary external device and logs received data (on average data arrives approximately once per second). I am using the Bluetooth LE CN1 library (a wrapper for Cordova Plugin).

The app should be able to receive and log the data even when not in foreground, which I have no idea how to do in CodenameOne in a way that the system wont shut it down on its own.

I've written a native Android app, where I use a service with a persistent notification for the duration of the connection with the external BTLE device - the background service exits when connection is lost (or user cancels the connection). Is there any way to do the same thing in CodenameOne, which will work both on Android and iOS? I know I could write native code to do that, but I'm sceptical if I can still use the plugin - I would like to avoid writing the whole bluetooth connection module of the app native for both OSes, as that kind of negates the point of using CodenameOne.

I haven't tried doing it by just spawning a java thread, as that looks like it wouldn't really work since it's converted to native code... or does it?

PS: I've searched for a solution to my problem, the closest i could find is CodenameOne Background Music playback, but this is meant only for music playback, as far as I can understand it.

回答1:

We aligned background behavior to what is possible on iOS where you are limited to specific background tasks as explained here: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

Since that is far more restrictive and limiting than the Android equivalent we are mapping this one use case at a time. Currently we support location, music playback & fetch.

Since the bluetooth support itself is rather new we don't have support for that at this time although it might be possible to support it by including the ios.plistInject build hint that enables this background behavior. This might require some native code to bind this behavior properly.