Android Question Close Bluetooth connection correctly

D

Deleted member 103

Guest
Hi,
With my app I create a connection to a Bluetooth device.
All bluetooth routine are included in a classe.
The class is initialized in starter service.
B4X:
Sub Process_Globals
    Public bluetooth As clsBluetooth
End Sub

Sub Service_Create
    'Bluetooth für die Externe Taste initialisieren.
    bluetooth.Initialize
End Sub

The connection is started in the Activity Main.
B4X:
Sub mnuBtButton_Click
    If Starter.bluetooth.IsBtConnected Then        ToastMessageShow(Starter.language.value("strBluetoothButtonIsConnected"), True)
    Else
        Starter.bluetooth.ConnectBtButton
    End If
End Sub

When you quit the Activity Main, the Bluetooth connection is closed.
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        'Bluetooth verbindung aufheben
        Starter.bluetooth.DestroyConnection
    End If
End Sub

'Classe clsBluetooth
Public Sub Initialize
   IsOldAdminEnabled   = False
   IsBtConnected     = False
     
   admin.Initialize("admin")
   serial1.Initialize("serial1")

   tmpList.Initialize
   foundDevices.Initialize

   If mBBL.HasFeature("android.hardware.bluetooth") Then   
     'Festhalten ob Bluetooth beim Start eingeschaltet ist.
     Try
       If admin.IsEnabled Then
         IsOldAdminEnabled = True
       End If
     Catch
       Log("Bluetooth-Initialize ERRROR: " & LastException)
     End Try
   End If
End Sub

Public Sub DestroyConnection
    IsBtConnected = False
    If serial1.IsInitialized Then serial1.Disconnect
    If AStream.IsInitialized Then AStream.Close
    'Bluetooth nur dann ausschalten wenn beim Start ausgeschaltet war.
    If admin.IsInitialized And Not(IsOldAdminEnabled) Then admin.Disable
End Sub

If I now start the app again and want to reconnect the Bluetooth device, then comes this error message: java.io.IOException: read failed, socket might closed or timeout, read ret: -1

Only if I add "ExitApplication" when quitting the Activity Main, I can connect the Bluetooth device with no problems at the next start.

How should I close the Bluetooth connection so that there are no problems with the new connection at the next start?
 
D

Deleted member 103

Guest
Service_Create will only be executed once. This means that your clsBluetooth class will not be initialized again. You should initialize it again if you want to create a new connection later.
That sounds reasonable.
That means I should initialize the classe somewhere else, right?
And best in sub "mnuBtButton_Click".
B4X:
Sub mnuBtButton_Click
    If Not(IsMenuEnabled) Then Return
    If Starter.bluetooth.IsBtConnected Then
        ToastMessageShow(Starter.language.value("strBluetoothButtonIsConnected"), True)
    Else
        If Not(Starter.bluetooth.IsInitialized) Then Starter.bluetooth.Initialize
        Starter.bluetooth.ConnectBtButton
    End If
End Sub
 
Upvote 0
D

Deleted member 103

Guest
Hi Erel,

as long as I'm making the bluetooth connection with the same app it works fine.

But if I try the Bluetooth connection with another app, it will not work properly anymore.
This means that the resourse of the first app is not released and therefore the connection with the second app does not work.

That would only work if I use the "ExitApplication" function, but as you always say, that would not be the right solution.

The problem is still not resolved.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
That would only work if I use the "ExitApplication" function, but as you always say, that would not be the right solution.

Yep, that's right but who needs to stop the whole all when you can start and stop a service like you want to (which releases all resources)


What I do with my Scanner App using BT(Admin):

1. Move all the BT(Admin) code to a fresh service
2. In the Activity call

B4X:
Sub Activity_Resume
    StartService(ScannerService)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    CallSub(ScannerService,"StopScannerService")
End Sub

3. In the service "ScannerService"

B4X:
Sub StopScannerService
        StopService(Me) 'Yep, that is cool
End Sub

Sub Service_Destroy
    serial1.Disconnect
    AStream.Close
   'as the service will be stopped/killed all the resources are freed
End Sub

With this you are stopping the service which will release all recources When the activity starts again, the service will be created again via Resume
 
Upvote 0
D

Deleted member 103

Guest
Hi Klaus,

1. Move all the BT(Admin) code to a fresh service
2. In the Activity call

das hört sich vernünftig an.
D.h. dann für mich, ich muss die klasse "clsBluetooth" in ein Service "smBluetooth" umwandeln.

That sounds reasonable.
That means for me, I have to convert the class "clsBluetooth" into a service "smBluetooth".
 
Upvote 0
D

Deleted member 103

Guest
You just need to close the connection when you no longer need it (maybe when the activity is paused).
I've been doing this all the time (see my code above), but that does not work - the recources are not released.
I have to kill the main activity with "ExitApplication" or the starter service.

If I apply now the solution of @KMatle, then I can kill the new service "smBluetooth" and all recources are free again.
I do not convert the class clsBluetooth into a service, but create a new service and initialize the class clsBluetooth.

I'll try this solution tonight, let's see if it gets better.
 
Upvote 0
D

Deleted member 103

Guest
It's desperate, I can do what I want, without the magic word "ExitApplication" it does not work.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
I can do what I want, without the magic word "ExitApplication" it does not work

Will try to figure this out tomorrow. If I get you right:

1. You have 2 Apps using BT
2. If you send your app to the background (= home button/swipe) without "ExitApplication" the 2nd cannot connect to BT
3. Only your app can

Q: What is the 2nd app? An own, too or "some other app from the store"
 
Upvote 0
D

Deleted member 103

Guest
1) I have 2 app that can connect to a bluetooth device.
2) I start the first app, and connect to the device.
3) I finish the first app with the back button
4) Now start the second app and try to connect to the Bt device. That will not do!
 
Upvote 0
D

Deleted member 103

Guest
Stopping a service doesn't release any resource, so it will not help.
In my Bluetooth class this code is:

B4X:
Public Sub DestroyConnection
    IsBtConnected = False
    If serial1.IsInitialized Then serial1.Disconnect
    If AStream.IsInitialized Then AStream.Close
    'Bluetooth nur dann ausschalten wenn beim Start ausgeschaltet war.
    If admin.IsInitialized And Not(IsOldAdminEnabled) Then admin.Disable
End Sub

and this code is called in Main Activity, but the connection is not closed properly.
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        'Bluetooth verbindung aufheben
        Starter.bluetooth.DestroyConnection
    End If
End Sub
What shall I do?
The only thing that currently helps is "ExitApplication".
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…