Android Question Bluetooth/Serial in a Service not connecting

Graham

Member
Licensed User
Longtime User
I found code here that will autoconnect Bluetooth and set up asych Serial communication.

I am trying to move the autoconnect and asynch to a service that will run in the foreground. I can't get the code to work. If I have all in the Activity it works slick. It doesn't connect though when I try it in a Service. It attempts to connect and that is it. Below is the code for the Activity and Service. At the bottom of the Activity code is commented code that works when all in the Activity.

ANy ideas as to what I am doing wrong?

Activity
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: test
    #VersionCode: 1
    #VersionName: 0
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.  
End Sub

Sub Globals
    Dim imgBTOff,imgBTOn,imgBluetooth As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean) 
   StartService(Test)
   Activity.LoadLayout("CalScreen")
End Sub

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

Sub Activity_Pause (UserClosed As Boolean)

End Sub

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

Sub BTOn   
    imgBTOff.Visible=False
    imgBTOn.Visible=True
    imgBluetooth.Enabled=False
End Sub

Sub BTOff
    imgBTOff.Visible=True
    imgBTOn.Visible=False
    imgBluetooth.Enabled=True
End Sub

'''''''THIS CODE WORKS WHEN ALL IN THE ACTIVITY AND NO SERVICE SAVE THIS CODE
'Sub imgBluetooth_click
'   MyDeviceName = "INTERFACE"  '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
'    BTOff
'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
'      Log("BT Connected")
'      BTOn
'   Else   'disconnected
'      ToastMessageShow("Connection to " & Serial1.Address & _
'                  " broken!", True)
'      'timBT.Enabled = False
'      btConnected = False
'      BTOff
'   End If
'End Sub
'
'Sub AStreams_NewData (Buffer() As Byte)
'Log("data")
'End Sub  
''
'Sub BTOn   
'    imgBTOff.Visible=False
'    imgBTOn.Visible=True
'    imgBluetooth.Enabled=False
'
'End Sub
'Sub BTOff
'    imgBTOff.Visible=True
'    imgBTOn.Visible=False
'    imgBluetooth.Enabled=True
'End Sub
'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 AStreams As AsyncStreams
'   Dim MyDeviceName As String
'  
'End Sub
'
'Sub Globals
'    Dim imgBTOff,imgBTOn,imgBluetooth As ImageView
'End Sub
'
'Sub Activity_Create(FirstTime As Boolean)
'  
'   StartService(Test)
'   Activity.LoadLayout("CalScreen")
'  
'   MyDeviceName = "INTERFACE"  '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
'    BTOff
'  
'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 imgBluetooth_click
'   MyDeviceName = "INTERFACE"  '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
'    BTOff
'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
'      Log("BT Connected")
'      BTOn
'   Else   'disconnected
'      ToastMessageShow("Connection to " & Serial1.Address & _
'                  " broken!", True)
'      'timBT.Enabled = False
'      btConnected = False
'      BTOff
'   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
'
'Sub AStreams_NewData (Buffer() As Byte)
'Log("data")
'End Sub  
''
'Sub BTOn   
'    imgBTOff.Visible=False
'    imgBTOn.Visible=True
'    imgBluetooth.Enabled=False
'
'End Sub
'Sub BTOff
'    imgBTOff.Visible=True
'    imgBTOn.Visible=False
'    imgBluetooth.Enabled=True
'End Sub

Service
B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
   Dim BT As BluetoothAdmin
   Dim Serial1 As Serial
   Dim btConnected As Boolean 
   Dim AStreams As AsyncStreams
   Dim MyDeviceName As String   
End Sub

Sub Service_Create'(SFT As Boolean)
End Sub

Sub Service_Start (StartingIntent As Intent)

Service.StartForeground(0,Null)
  
   MyDeviceName = "INTERFACE"  'Put the name of your coupled device here!
   ToastMessageShow("Trying to connect to " & MyDeviceName, True)
  
   Try
      BT.Initialize("BT")
      Serial1.Initialize("Serial1")
   Catch
      ToastMessageShow("No BlueTooth Device visible...", True)
   End Try
End Sub

Sub Service_Destroy

End Sub

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

Sub BTConnectToDeviceService
   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
      Log("BT Connected")

   Else   'disconnected
      ToastMessageShow("Connection to " & Serial1.Address & _
                  " broken!", True)
      btConnected = False
   End If
End Sub

Sub AStreams_NewData (Buffer() As Byte)
Log("data")
End Sub  
'
 
Top