Android Question Need architecture advice with background application

Spavlyuk

Active Member
Licensed User
Longtime User
Hello,

I want to build a companion app, which runs an HTTP api service and establishes an BLE connection when an NFC tag is scanned.
Afterwards, when an api request is received and a BLE connection is established, forward the command to the BLE device.
The purpose of the app is to run as a service only, without requiring user interaction, besides the NFC tag tapping.

So far I have a B4XPages project with a B4XMainPage, Starter service, a foreground service and a StartAtBootReceiver.
When an NFC tag is tapped, android starts the application, B4XMainPage appears and it tries to establish a BLE connection.

However, what I've made so far isn't entirely ideal.
The communication between the main page and the ble service is a bit awkward. I have a BleHelper class which is a public field in BleService.
The main page uses the Appear/Disappear events to register a callback.

B4XMainPage:
Private Sub B4XPage_Appear
    If Not(IsPaused(BleService)) Then
        BleService.Ble.SetCallback(Me)
    End If
End Sub

Private Sub B4XPage_Disappear
    BleService.Ble.SetCallback(Null)
End Sub

Then from BleHelper I have the sub RaiseUpdatedEvent to raise connected/disconnected events.

BleHelper:
Private Sub RaiseUpdatedEvent
    If CallbackTarget <> Null Then
        CallSub(CallbackTarget, "Ble_StatusUpdated")
    End If

    CallSub(BleService, "Ble_StatusUpdated")
End Sub

Is there a better way to communicate between the service and the main page?
Is it a good idea to use B4XPages? Is the Starter service needed?
Is it possible to avoid having the application/activity appear and only have the foreground notification?

Any advice would be greatly appreciated.
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
Why are you clearing the callback on Disappear event? The page will still be there, even if not visible.
I think the BleService is a regular Service running in foreground mode, not just a Receiver, so you should be able to call B4XPages.MainPage.Ble_StatusUpdated directly.
 
Upvote 0

Spavlyuk

Active Member
Licensed User
Longtime User
B4XMainPage is gonna be in the background pretty much all of the time, there's really no point in updating it.
Also, at some point android might decide to kill if it's in the background for too long, calling B4XPages.MainPage.Ble_StatusUpdated directly seems a bit scary as I don't want the service crashing.
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
I think if the OS is going to kill MainPage, it will also kill the service, since they are in the same process.
a more competent advice by senior developers would be great.
 
Upvote 0
Top