Detect if your are connected to bluetooth

mrossen

Active Member
Licensed User
Longtime User
Hi,

Is it possible to see if you are connected to a bluetooth device.

Can I catch an event if I move out of bluetooth range and again when I come in range.

My device aut. connect to the phone when I it is in range

Mogens
 
Last edited:

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hi

Just adding my 2 cents worth. If you are using the android.bluetooth.device.action.ACL_CONNECTED and android.bluetooth.device.action.ACTION_ACL_DISCONNECTED intents we use seperate services to manage these.

B4X:
#Region  Service Attributes
   #StartAtBoot: False
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub
Sub Service_Create
   
   mod_functions.writelog("svc_bt_acl_connected(),Service_Create")
   
End Sub

Sub Service_Start (StartingIntent As Intent)

   mod_functions.writelog("svc_bt_acl_connected(),Service_Start")
   ' // we can restart the svc_event_handler from here after we have been woken up
   If Not(IsPaused(svc_serial)) Then
     mod_functions.writelog("svc_bt_acl_connected(),Service_Start, svc_serial is running, call remote_connect")
     CallSub(svc_serial,"remote_connect")
   Else
     mod_functions.writelog("svc_bt_acl_connected(),Service_Start, Starting svc_serial")
     StartService(svc_serial)
   End If
   ' // no need to keep this alive
   StopService("")
   
End Sub

Sub Service_Destroy
   
   mod_functions.writelog("svc_bt_acl_connected(),Service_Destroy")
   
End Sub

The svc_serial service is called. Now, in the svc_serial it will aquire a partial lock for as long as the service is requried.
B4X:
#
Sub Service_Start (StartingIntent As Intent)
   
   mod_functions.writelog("svc_serial(),Service_Start ")
   
   pw.PartialLock
   com_listen
     
   tmrServiceHook.Enabled = True
   
End Sub


Sub Service_Destroy
   
   mod_functions.writelog("svc_serial(),Service_Destroy")
   pw.ReleasePartialLock

End Sub

The reason for the partial lock is so android will not kill the real serial service.

By using this method you can manager power manager very well on the phone so you are not in a perpetual state of listening for a connection.

Hope this helps you. An example of our product is www.ursosbutton.com

Regards

John
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
I have added these lines. One to the code itself. And one to the manifest editor. I also declared this following codes:

Dim admin As BluetoothAdmin
Dim serial1 As Serial

My codes are of no errors before. But after I added the lines that you want us to add, the error message I get is: "Module:s2_br not found". How come?

In this example svc_bt_disconnect is the name of the service in your project. This service will be called when the intent android.bluetooth.device.action.ACL_DISCONNECTED is received.

This is the manifest entry
B4X:
AddReceiverText(svc_bt_disconnect, <intent-filter> <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED"/></intent-filter>)

Hope this helps.

John.
 
Upvote 0

Roy Raymundo

Member
Licensed User
Longtime User
What is the service name? If is is not s2 then you need to update the manifest text.
I have fix this. I thought I have to code directly, but instead, I should use Service module in Project menu. This code:

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim BT_Service_connected As Boolean
End Sub
Sub Service_Create
BT_Service_connected = False
End Sub
Sub Service_Start (StartingIntent AsIntent)
If StartingIntent.Action = "android.bluetooth.device.action.ACL_CONNECTED" Then
Log("BT_Connect")
BT_Service_connected = True

' THE STATEMENT BELOW IS NOT EXECUTING IN MY CASE... !!
Else If StartingIntent.Action = "android.bluetooth.device.action.ACL_DISCONNECTED"Then
Log("BT_Disconnect")
BT_Service_connected = False
CallSubDelayed(Main, "BT_Disconnect")
End If
Log(StartingIntent)
Log(StartingIntent.ExtrasToString)
End Sub


This android.bluetooth.device.action.ACL_DISCONNECTED might not be the way to determine if the bluetooth is disconnected. It fails to detect if it is disconnected. But as connected, it can detect by android.bluetooth.device.action.ACL_CONNECTED. Any suggestions?
 
Last edited:
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
I have fix this. I thought I have to code directly, but instead, I should use Service module in Project menu. This code:

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim BT_Service_connected As Boolean
End Sub
Sub Service_Create
BT_Service_connected = False
End Sub
Sub Service_Start (StartingIntent AsIntent)
If StartingIntent.Action = "android.bluetooth.device.action.ACL_CONNECTED" Then
Log("BT_Connect")
BT_Service_connected = True

' THE STATEMENT BELOW IS NOT EXECUTING IN MY CASE... !!
Else If StartingIntent.Action = "android.bluetooth.device.action.ACL_DISCONNECTED"Then
Log("BT_Disconnect")
BT_Service_connected = False
CallSubDelayed(Main, "BT_Disconnect")
End If
Log(StartingIntent)
Log(StartingIntent.ExtrasToString)
End Sub


This android.bluetooth.device.action.ACL_DISCONNECTED might not be the way to determine if the bluetooth is disconnected. It fails to detect if it is disconnected. But as connected, it can detect by android.bluetooth.device.action.ACL_CONNECTED. Any suggestions?

Hi can you post your manifest file please, also what BT device are you using to do this ?
 
Upvote 0

Roy Raymundo

Member
Licensed User
Longtime User
This is the code of the manifest:


'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)

AddReceiverText(s2, <intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED"/>
</intent-filter>)
AddPermission(android.permission.BLUETOOTH)

SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

And this is the bluetooth device where I connect :
http://www.e-gizmo.com/KIT/egbt-04.htm
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
you are not currently listening for the disconnect.

This is what I use for handling the same :

B4X:
AddReceiverText(svc_bt_disconnect, <intent-filter> <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED"/></intent-filter>)
AddReceiverText(svc_bt_connected, <intent-filter> <action android:name="android.bluetooth.device.action.ACL_CONNECTED"/></intent-filter>)


Regards

John.
 
Upvote 0

Roy Raymundo

Member
Licensed User
Longtime User
you are not currently listening for the disconnect.

This is what I use for handling the same :

B4X:
AddReceiverText(svc_bt_disconnect, <intent-filter> <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED"/></intent-filter>)
AddReceiverText(svc_bt_connected, <intent-filter> <action android:name="android.bluetooth.device.action.ACL_CONNECTED"/></intent-filter>)


Regards

John.
Then I have to create another service for svc_bt_disconnect this time I think. Because I only have a service for s2, which is service connected.
 
Last edited:
Upvote 0

danoveb

Member
Licensed User
Longtime User
Old thread but this was the nearest I could find for my current problem. Have a new phone with Android 13, and now I get no Mac address from the StartingIntent when I recieve the "android.bluetooth.device.action.ACL_CONNECTED" event. I'm on plattform 30. Worked with mu previous phone that had Android 11.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
was the nearest I could find for my current problem
It does not matter. Always create a new thread for your issues. Posting to old threads is a mistake
 
Upvote 0
Top