Bluetooth auto connection

mrossen

Active Member
Licensed User
Longtime User
Hi there,

I have made a application that uses bluetooth.

My bluetooth device is paired to the phone.

My problem is : I have to connect to the device manualy every time I need it.
Is there a way I can autoconnect when the device in in range.

Mogens
 
Last edited:

KY Leng

Active Member
Licensed User
Longtime User
Using the new BluetoothAdmin object (part of the Serial library) you can periodically search for devices in range and try to connect.

Dear Erel,
Could you please send me the example how to deal with this BluetoothAdmin? I cannot find the way to :
- save the lastest MAC address of a connected device.
- search a new device.
- detect signal streight of the current connected device.
- auto connect to the lastest device if the device is in range again.
- enable bluetooth working in background even the phone screen is off.
Hope I can get the solution from you soon.
Best regards,
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Here is a very basic example that might you start going. I extracted it from one of my apps. Project file is attached.
The code is also based on the "Serial example"

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim BT As BluetoothAdmin 
   Dim Serial1 As Serial 
   Dim btConnected As Boolean 
   Dim timBT As Timer 
   Dim AStreams As AsyncStreams 

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim MyDeviceName As String
   
   Dim lblBT As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout(1)
   
   MyDeviceName = "EaSS-V040305BT"  'Put the name of your coupled device here!
   ToastMessageShow("Trying to connect to " & MyDeviceName, True)
   timBT.Initialize("timBT",5000)
   timBT.Enabled = False 
   
   Try
      BT.Initialize("BT")
      Serial1.Initialize("Serial1")
   Catch
      ToastMessageShow("No BlueTooth Device visible...", True)
   End Try
   
   
End Sub

Sub Activity_Resume
      'Start Bluetooth
      Try
         If BT.IsEnabled = False Then
            lblBT.Color = Colors.Black 
            BT.Enable
         Else
            'connect to device
            BTConnectToDevice
         End If
      Catch
      End Try

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub BT_StateChanged(NewState As Int,OldState As Int)
   If NewState = BT.STATE_ON Then
      BTConnectToDevice
      Log("BT Connect")
   Else 
      Serial1.Disconnect 
      timBT.Enabled = False
      Log("BT Disconnect")
   End If
End Sub

Sub BTConnectToDevice
   Dim PairedDevices As Map 
   
   PairedDevices = Serial1.GetPairedDevices
   Try
      Serial1.Connect3(PairedDevices.Get(MyDeviceName),1)
   Catch
      ToastMessageShow("Device not available",True)
   End Try
End Sub

Sub Serial1_Connected (Success As Boolean)
   Dim msg As String 
   
   If Success = True Then
      ToastMessageShow("Bluetooth connected to " & Serial1.Address, False)
      AStreams.Initialize(Serial1.InputStream,Serial1.OutputStream,"AStreams")
      timBT.Enabled = True
   Else   'disconnected
      ToastMessageShow("Connection to " & Serial1.Address & _
                  " broken!", True)
      timBT.Enabled = False
      btConnected = False
   End If
End Sub

Sub timBT_Tick
   'Communicte with the device here
   'if the device answers, fine
   'if there is no response, communication might be lost
   'Stop the timer and you should call BTConnectToDevice again
End Sub

Rolf
 

Attachments

  • BTBasics.zip
    6.7 KB · Views: 1,053
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…