Bluetooth auto connection

Discussion in '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.

    Mogens
     
    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 Active 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"

    Code:
    '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
     

    Attached Files:

    Last edited: Feb 6, 2012
    WanieHikmah likes this.
  7. Erel

    Erel Administrator Staff Member Licensed User

Share This Page

Loading...