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:

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can add an intent filter and listen to the following events: ACTION_ACL_CONNECTED and ACTION_ACL_DISCONNECTED: BluetoothDevice | Android Developers

Search the forum for: intent filter
There are several example of using the manifest editor to add intent filters.
Note that you also need to add the android.permission.BLUETOOTH permission.
 
Upvote 0

SoyEli

Active Member
Licensed User
Longtime User
bluetooth connected filter

Mr. Erel

Can you please elaborate on this.

After you add the filter to the manifest, then what ?
is there a event or how do you test from a app. ?


Thank you for your HELP :sign0188:
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Create a service named s2 with this code:
B4X:
Sub Service_Start (StartingIntent As Intent)
   Log(StartingIntent)
   Log(StartingIntent.ExtrasToString)
End Sub

Add the following code to the manifest editor:
B4X:
AddReceiverText(s2, <intent-filter>
 <action android:name="android.bluetooth.device.action.ACL_CONNECTED"/>
 </intent-filter>)
 AddPermission(android.permission.BLUETOOTH)

Make a connection and check the logs.
 
Upvote 0

SoyEli

Active Member
Licensed User
Longtime User
Thank you, that only works only when BT is connected.
What about when it is disconnected ?

Do I use this in the manifest:

AddReceiverText(BtConnected, <intent-filter> <action android:name="android.bluetooth.device.action.ACTION_ACL_DISCONNECTED
"/>
</intent-filter>)

Do I have to use another service for this, or the same one ?

Thank you for your HELP :)
 
Upvote 0

SoyEli

Active Member
Licensed User
Longtime User
Thank you:

I got it to work: :sign0060:

Thank you for all your help!

By the way the 1.9 is fantastic :wav:
 
Upvote 0

sz4t4n

Member
Licensed User
Longtime User
Hi,

maybe someone have an apk example which give feedback when BT is disconnected? I was trying use Erel code but I dont really know how to use it. I dont have any log's.
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
How exactly to you use the code to catch the disconected event ? I did all the tests but did not succeded ! Where I am doing wrong ?
B4X:
AddReceiverText(s2, <intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED"/>
<action android:name="android.bluetooth.device.action.ACTION_ACL_DISCONNECTED"/>
</intent-filter>)
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
Here is the code I am using !
I tried on two device so be sure and I have this problem.

B4X:
'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"/>)

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

SetActivityAttribute(main, android:windowSoftInputMode, adjustResize|stateHidden)'tastatura

AddApplicationText(
<activity android:name="com.tapit.adview.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation"/>
)

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


AddActivityText(main,  <intent-filter android:priority="1000">
      <action android:name="android.intent.action.VOICE_COMMAND" />
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
)


AddPermission(android.permission.MODIFY_AUDIO_SETTINGS)
AddPermission(android.permission.BLUETOOTH_ADMIN)
AddPermission(android.permission.BROADCAST_STICKY)

AddPermission(android.permission.CALL_PHONE)
AddPermission(android.permission.CHANGE_NETWORK_STATE)
AddPermission(android.permission.SEND_SMS)
AddPermission(android.permission.RECEIVE_SMS)
AddPermission(com.android.alarm.permission.SET_ALARM)
AddPermission(android.permission.CHANGE_WIFI_STATE)
AddPermission(android.permission.WRITE_SMS)


Can you post the code in s2 service?
B4X:
Sub Service_Start (StartingIntent As Intent)
Log(StartingIntent)
Log(StartingIntent.Action)

If StartingIntent.Action="android.bluetooth.device.action.ACL_CONNECTED" Then
CallSubDelayed(Main,"show_btmode") 'actually this sub is showing a bluetooth bitmap to inform user that a connection is present (one way only :))
End If

End Sub
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
I am running my app in the release mode and all that I can see on logs are:
Good to know that the code looks good .... By not cacthing ACL_Disconected I can not make a reliable automatic state detection ... for now
I will enclose option to manually set bluetooth headset... mode
Will still search and test further more...

** Activity (main) Pause, UserClosed = false **
** Service (s2) Start **
(Intent) Intent { act=android.bluetooth.device.action.ACL_CONNECTED cmp=hs.easy.romt/.s2$s2_BR (has extras) }
android.bluetooth.device.action.ACL_CONNECTED
sending message to waiting queue (CallSubDelayed - show_btmode)
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
the message in waiting queue is ok, when the activity is started by user the show_btmode sub will start and change an image backgound state ..so I guess I understood how callsubdelayed works...

Give me some time to test on other devices the code and I will come with an answer ....

'I will try this code
B4X:
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 As Intent)
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
 
Upvote 0

Roy Raymundo

Member
Licensed User
Longtime User
Create a service named s2 with this code:
B4X:
Sub Service_Start (StartingIntent As Intent)
   Log(StartingIntent)
   Log(StartingIntent.ExtrasToString)
End Sub

Add the following code to the manifest editor:
B4X:
AddReceiverText(s2, <intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED"/>
</intent-filter>)
AddPermission(android.permission.BLUETOOTH)

Make a connection and check the logs.

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?
 
Upvote 0
Top