Bluetooth auto connection

Discussion in 'Android Questions' started by mrossen, Aug 25, 2011.

  1. mrossen

    mrossen Active Member Licensed 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.

    Last edited: Aug 25, 2011
  2. Erel

    Erel Administrator Staff Member Licensed User

    Using the new BluetoothAdmin object (part of the Serial library) you can periodically search for devices in range and try to connect.
  3. KY Leng

    KY Leng Member Licensed User

    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,
  4. Erel

    Erel Administrator Staff Member Licensed User

    I'm sorry but I cannot build the application for you.

    We can help with any specific question.

    Note that it is not possible to detect the signal strength.
  5. AlpVir

    AlpVir Well-Known Member Licensed User

    I am making an app that does what you ask about. I used the code od the post "Serial example" and "GPS - Bluetooth, decode and convert to NMEA strings LOCAL datum". I have not found much difficulty.
  6. rbsoft

    rbsoft Active Member Licensed 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"

    '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)
       MyDeviceName = 
    "EaSS-V040305BT"  'Put the name of your coupled device here!
       ToastMessageShow("Trying to connect to " & MyDeviceName, True)
       timBT.Enabled = 
    ToastMessageShow("No BlueTooth Device visible..."True)
    End Try
    End Sub

    Sub Activity_Resume
    'Start Bluetooth
    If BT.IsEnabled = False Then
                lblBT.Color = 
    'connect to device
    End If
    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
    Log("BT Connect")
          timBT.Enabled = 
    Log("BT Disconnect")
    End If
    End Sub

    Sub BTConnectToDevice
    Dim PairedDevices As Map 
       PairedDevices = Serial1.GetPairedDevices
    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)
          timBT.Enabled = 
    Else   'disconnected
          ToastMessageShow("Connection to " & Serial1.Address & _
    " broken!"True)
          timBT.Enabled = 
          btConnected = 
    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

    Attached Files:

    Last edited: Feb 6, 2012
    nhz, jeny la loka and WanieHikmah like this.
  7. Erel

    Erel Administrator Staff Member Licensed User