Android Question How to close an app or disconnect bluetooth

Altra

New Member
Licensed User
Great software! Have been able to do quite a bit without asking questions, thanks to this forum and it's contributors

Is there any way to add a click event to disconnect or stop the bluetooth service in the B4A code from post number one in link?

https://www.b4x.com/android/forum/threads/hc-05-classic-bluetooth.66677/

I have few variations of the same application on the phone. I run into problems switching between the apps. The OS will stay connected or try to reconnect to the last one that was open. The newly opened app can't connect and keeps searching.

Thanks

Add:
Found some more info. Would this be a good way to disconnect bluetooth?

In main
B4X:
Sub btnDisconnect_Click
    CallSub(Starter, "Service_Destroy")
End Sub

In starter
B4X:
Sub Service_Destroy
    Serial.Disconnect + AStream.Close.
End Sub
 
Last edited:

Altra

New Member
Licensed User
Thanks for your reply.

I'll study the tutorial you referenced and see how to integrate AsyncStreamsText

While waiting for help, I found in the original HC-5 classic code. The sub "Activity_Pause" was empty. So when exiting the application the Astreams remained running.

I added this to the Main section
B4X:
Sub Activity_Pause (UserClosed As Boolean) 'modified
    If UserClosed Then
        CallSub(Starter, "AST_Close_Starter")
    End If
End Sub

And in the Starter section this
B4X:
Sub AST_Close_Starter
    ast.Close
    connected = False
    CallSub(Main, "SetState")
End Sub

Now when I exit the app it disconnects the bluetooth. Not sure if this is the best way. Attached is full project.
 

Attachments

  • Arduino_Bluetooth_v2_B4A.zip
    10 KB · Views: 159
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Now when I exit the app it disconnects the bluetooth. Not sure if this is the best way
It all depends on your requirements. If you want the connection to end when the activity is paused then it is a good solution. In many cases it is simpler not to tie the connection to the activity life cycle and never close it.
 
Upvote 0
Top