Android Question Listening for BT connections with Serial

joermo

Member
Licensed User
Hi!

I've been trying to make a service that listens for my phone to connect to my car's bluetooth. For development purposes I listen for my BT ear piece.
I want the service to activate my phone's hotspot, using the Hotspot library, when I get in the car
which will feed my tablet with internet. The tablet will be my car's new radio, as they've killed the FM radio in Norway, and I do not want to buy a DAB+ adapter.

This is my code now:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim HS As HotSpot
    Dim Ser As Serial
    Dim Tim As Timer
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
    HS.Initialize("Hotspot")
    Ser.Initialize("Serial")
    Tim.Initialize("Timer",1000)
End Sub

Sub Service_Start (StartingIntent As Intent)
    Tim.Enabled = True
    Ser.Listen
    EnableHotspot
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

Public Sub Serial_Connected(Success As Boolean)
    Log("Connection attempted")
    If Success Then
        Log("Connected to: " & Ser.Name)
    Else
        Log("Connection failed")
    End If
  
    EnableHotspot
End Sub

Public Sub EnableHotspot()
    If (Ser.Name = "BH-105" Or  Ser.Name = "KIA MOTORS") Then
        HS.enableHotSpot
        ToastMessageShow("Hotspot for BilRadio aktivert!", True)
    Else
        HS.disableHotSpot
    End If
End Sub

Public Sub Timer_Tick()
    If  Ser.IsEnabled = False Then
        Log("Vennlist slå på blåtann!")
    End If
End Sub

First of all, the Serial_Connected sub does not work, unless I manually turn BT of, and then on again.
Then it starts listening, but the Success always turns out False, even if I have connected a device while it was listening...

I have also added this to the manifest:

B4X:
'End of default text.

AddPermission(android.permission.ACCESS_COARSE_LOCATION)

AddPermission(android.permission.CHANGE_WIFI_STATE)
AddPermission(android.permission.ACCESS_WIFI_STATE)
AddPermission(android.permission.INTERNET)
 

joermo

Member
Licensed User
OK! Thank you! I am quite new to the Android programming world, so could you direct me how to do it?

I added this to the manifest:

B4X:
'End of default text.

AddReceiverText(Starter,
<intent-filter>
    <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
    <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
    <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
</intent-filter>
)

And in Starter i added this:

B4X:
Sub Service_Start (StartingIntent As Intent)
    Log(StartingIntent.Action)
    If StartingIntent.Action = "android.bluetooth.device.action.ACL_CONNECTED" Then
        Log("Koblet til BT!")
    End If
End Sub

But, nothing happens so far when I connect a BT-device...
 
Upvote 0

joermo

Member
Licensed User
OK! I've got it to register a BT connection/disconnection successfully, but how can I tell which device was connected (by name/MAC)? Serial seems to still be useless for this purpose...
 
Upvote 0
Top