Android Question BLE runtime exceptions

dagnabitboy

Active Member
Licensed User
Longtime User
Developing an app/RN4020 Bluetooth product, which is nearing completion. During testing I'm operating the app under as many varied conditions I can think of! When I exit the app (after normal error free execution) and quickly start it up again I get two run time exceptions. Sometimes I get "Service not found", meaning that it thinks the Bluetooth service is not ready, and mostly I get "no device connected". My app shows that a connect event occurred even though scanning for the RN4020 has not started. These problem occur when I exit the app using "activity.finish". I make sure prior to exit to issue a disconnect and stop the Bluetooth service. Interestingly these problems go away when I use "ExitApplication" to exit the app, which I know is not recommended. These happen on Android versions 4, 6 and 7.

Does anyone have thoughts or suggestions on this?

Thanks in advance!!
 

dagnabitboy

Active Member
Licensed User
Longtime User
Can you post the code that closes the connection and how you call it?

Here's the basic code:

B4X:
Sub Activity_Pause (UserClosed As Boolean)
   If UserClosed Then
     scanTimer.Enabled = False   
     commTimer.Enabled = False
     CallSub(BT_Service, "Disconnect")
     StopService(BT_Service)
     Activity.Finish     ' if I use "ExitApplication" problems gone.
   End If
End Sub


' I also call the same user exit from a button

Sub btnExit_Click
   Activity_Pause(True)
End Sub
 
Upvote 0

dagnabitboy

Active Member
Licensed User
Longtime User
Is there a way I can upload it to you privately? I can't reveal the source in a public forum as it's for a commercial product.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Send him a private message.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I checked your code.

1. Never block the main thread with a busy loop. Use Sleep instead.
2. You should use the Starter service for all the communication. The advantage of the Starter service is that it is always started before the main activity so it will always be ready.
3. It is better to put all public global variables in the starter service.
4. Move anything that you can to the service instead of the activity.
5. Why do you need to close the connection when the activity is paused? Let the OS kill the process when it wants.
 
Upvote 0

dagnabitboy

Active Member
Licensed User
Longtime User
Thanks Erel, I appreciate your input. Much of my code has been gleaned from examples and I was not aware of the starter service, and it's purpose and preferred usage. As to your question #5 I close the BLE connection so other users/devices can access the same RN4020 device.
 
Upvote 0
Top