Check bluetooth headset is connected

brelto85

Active Member
Licensed User
Longtime User
After my post, i need to determinate if the bluetooth headset is connected

looking for the internet i found the isBluetoothScoOn() method:

B4X:
Sub IsBluetoothHeadsetOn As Boolean
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
    Return r.RunMethod("isBluetoothScoOn")
End Sub

but this method not works on my phone and in this link explains that this feature works only on rooted devices

There are other solutions?
someone has tested?

thanks
alberto
 

brelto85

Active Member
Licensed User
Longtime User
it works fine
but suppose to have a bleutooth headset already connected before to start application
when i start the application and the listener service (s2), the first log are:

(Intent) Intent { cmp=com.breltolab.drivi/.servicebluetooth }
no extras


subsequent log are then corrected based on the state of connection of the headset bluetooth
any idea?
 
Upvote 0

brelto85

Active Member
Licensed User
Longtime User
ok, but my problem is that the bluetooth is already connected and when i run the service the StartingIntent.ExtrasToString variable the first time returns "noextras"
Also StartingIntent.Action is empty.

Only disconnecting and reconnecting the bluetooth during the service run, the service intercept the intent with value ACTION_ACL_CONNECTED and ACTION_ACL_DISCONNECTED (in property StartingIntent.ExtrasToString)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Have you added the android.permission.BLUETOOTH permission in the manifest file?

Your process should start when the Bluetooth device is connected. Check the logs while you connect a device (when your app is not running).

Your process might be later killed. This means that you need to save the current state in a file and later load it when your activity is started.
 
Upvote 0

brelto85

Active Member
Licensed User
Longtime User
yes the permission is in manifest file and infact the listener service works perfectly if i connect or disconnect the bluetooth device

My goal is to check if is connected or not when i start the application
Also if the bluetooth device is connected when run the application (that run the listener service) StartingIntent.ExtrasToString is empty
 
Upvote 0

brelto85

Active Member
Licensed User
Longtime User
I know Erel, infact it works perfectly!!

I repeat that my goal is to check if the bluetooth headset is connected when i start the application not only when it is disconnected or connected during application execution.

If the bluetooth device is already connected when i start application (and listener service), the log are empty!!!!!!!!
 
Upvote 0

brelto85

Active Member
Licensed User
Longtime User
I do not think an optimal solution

First, the first time i run the app, i do not know if the bluetooth is connected or not (I know just after the start, when the status BT changed)

Also:

1. first run, save state BT (connected or not. suppose yes)
2. close application
3. disconnect BT
4. run application
5. load state BT (connected) when in really is not

I could use a static function that works on all devices
 
Upvote 0

brelto85

Active Member
Licensed User
Longtime User
In fact, repeat that if the device is already connected before starting the application, the StartingIntent.ExtrasToString (when you start the application and then the listener service) is empty because there was no event (about disconnect/connect BT)

After the service listener it works fine when you disconnect or connect the bluetooth device but the first time i do not know the status

I hope i explained correctly

thanks
 
Upvote 0

SoyEli

Active Member
Licensed User
Longtime User
This could work:

try to connect to the bluetooth after loading completes with try catch.
 
Upvote 0

SoyEli

Active Member
Licensed User
Longtime User
I'm not a expert, but this worked for me.
you need to search for serial samples

Hope this helps:

Try
Serial1.Connect3(Your bluetooth,1)
Catch
Msgbox("Error connecting to " & your bluetooth,"")
End Try
Sub Serial1_Connected (Success As Boolean)
If Success Then
Log("Bt_Connected")
Else
Log("Bt_Not_Connected")
End If
End Sub
 
Upvote 0

brelto85

Active Member
Licensed User
Longtime User
Thanks for your reply but connect3() requires a MacAddress parameter and i must search from devices connected

I also notice that the action "ACTION_ACL_CONNECTED" in the listener service, catch all Bluetooth device

i need to check the status (connected or not) only for bluetooth headset devices

there's a method using the context to manage BluetoothHeadset class?
 
Upvote 0

brelto85

Active Member
Licensed User
Longtime User
I read the following from android documentation

...
As the ACTION_SCO_AUDIO_STATE_UPDATED intent is sticky, the application can check the SCO audio state before calling startBluetoothSco() by reading the intent returned by the receiver registration. If the state is already CONNECTED, no state change will be received via the intent after calling startBluetoothSco(). It is however useful to call startBluetoothSco() so that the connection stays active in case the current initiator stops the connection.

Unless the connection is already active as described above, the state will always transition from DISCONNECTED to CONNECTING and then either to CONNECTED if the connection succeeds or back to DISCONNECTED if the connection fails (e.g no headset is connected).

...

could help?
 
Upvote 0
Top