Bluetooth as service

mrossen

Active Member
Licensed User
Longtime User
Hi,

I am planing an app there aut. connecting to a device in a car.

The setup is that the bluetooth device and the phone aut. connect when ignition is on the car.

My question is: If I make all the bluetooth com programming as a service, to catch the events, the cars bluetooth device sends, Can I then send text from the main activity though the bluetooth in the service module if I open the app?

Does this make sense ?

Mogens
 
After using this for a while I found another problem.

When the app is paused and the service i running the serial1_connected event was not raised. Then I have moved serial1_connected sub and the init for this to the service also.

Now the serial1_connected event is not raised.

This is my service module

Code:
'Service module
Sub Process_Globals
   
   Dim Serial1 As Serial
   Dim BluetoothAdmin1 As BluetoothAdmin
   Dim AStreams As AsyncStreams
   Dim Notification1 As Notification
   
End Sub
Sub Service_Create

   Serial1.Initialize("Serial1")
   BluetoothAdmin1.Initialize("BluetoothAdmin1")
   
   Notification1.Initialize
    Notification1.Icon = "icon" 'use the application icon file for the notification
    Notification1.Light = False
   Notification1.Sound = False
   Notification1.Vibrate = False
   Notification1.SetInfo("xxx", "Disconnected to xxx", Main)
       
    'Make sure that the process is not killed during the download
    Service.StartForeground(1, Notification1) 

End Sub

Sub Service_Start (StartingIntent As Intent)

   Dim BluetoothConnectionStatus, BluetoothConnectionID As String

   StartServiceAt("", DateTime.Now + 30 * DateTime.TicksPerSecond, True)
   
   BluetoothConnectionStatus = StartingIntent
   BluetoothConnectionID = StartingIntent.ExtrasToString

'   If BluetoothConnectionStatus.Contains("ACL_CONNECTED") AND BluetoothConnectionID.Contains(Main.DeviceID) Then
'      Notification1.SetInfo("xxx", "Connected to Cruise", Main)
'      Notification1.Notify(1)
'   End If
   
   If BluetoothConnectionStatus.Contains("ACL_DISCONNECTED") AND BluetoothConnectionID.Contains(Main.DeviceID) Then
      Notification1.SetInfo("xxx", "Disconnected to xxx", Main)
      Notification1.Notify(1)
   End If   
   
   Log(StartingIntent)
    Log(StartingIntent.ExtrasToString)
   
End Sub

Sub Service_Destroy

End Sub

Sub AStreams_NewData (Buffer() As Byte)
    
   Dim msg As String
   
    msg = BytesToString(Buffer, 0, Buffer.Length, "ISO-8859-1")
   Main.RecievedText = Main.RecievedText & msg
   
   Log(Main.RecievedText)
   'ToastMessageShow(Main.RecievedText, False)
   
End Sub

Sub AStreams_Error
    
   ToastMessageShow(LastException.Message, True)
   Log(LastException.Message)
   
   If LastException.Message = "java.io.IOException: Broken pipe" OR LastException.Message = "java.io.IOException: Connection reset by peer" OR LastException.Message = "java.io.IOException: Software caused connection abort" Then 
      Serial1.Disconnect
      Main.connected = False
      ClearRecievedData
   End If
   
End Sub

Sub Serial1_Connected (Success As Boolean)
   
   If Success Then
      BluetoothConnected
      BluetoothSend("*")
   Else
      Main.connected = False
      ToastMessageShow(Main.mtconnectionerror, False)
   End If
   
End Sub

Sub BluetoothSend(dataString As String)

   Dim buffer() As Byte
   
   ' Is Bluetooth connected
   If AStreams.IsInitialized = False Then
      ToastMessageShow(Main.mtnoconnedtiontoecruise, False)
      Return
   End If
   
   Log(dataString)
   
   ' Send data to stream
    buffer = dataString.GetBytes("ISO-8859-1")
   AStreams.Write(buffer)
   
End Sub

Sub BluetoothConnected

   ToastMessageShow(Main.mtconnected, False)
   AStreams.Initialize(Serial1.InputStream, Serial1.OutputStream, "AStreams")
   Notification1.SetInfo("xxx", "Connected to xxx", Main)
   Notification1.Notify(1)
   Main.connected = True
         
End Sub

Sub BluetoothAdmin1_StateChanged (NewState As Int, OldState As Int)
   
   Dim i As Int
   
   If (NewState = BluetoothAdmin1.STATE_OFF) Then
      Serial1.Disconnect
      Main.connected = False
      ClearRecievedData
   End If

End Sub

Sub ClearRecievedData

   For i = 1 To 125
      Main.RecievedData(i) = ""
   Next
   
End Sub

Mogens
 
Upvote 0
Hi Erel,

Thanks for yout time.

I call Service.StartForeground(1, Notification1) in Service_Create. Is this not right?

Mogens
 
Upvote 0
Hi Erel,

I have now remarked the ServiceStart out. That don't change any thing.

I have a bluetooth device I make the connection to. When it is connected I have a LED on the device that shows the connected status. This work perfect. I also gets the "ACL_CONNECTED" intent.

But it is not fire the Serial1_Connected event.

The only thing I have changed, it to move "Serial1.Initialize("Serial1")" and the Serial1_Connected sub from the main activity to the service module.

Is it a problem that I have the subs to connect in the main activity?

Is it correct that the Serial_Connected event is fired where the Serial1.Initialize("Serial1") is?

My Problem was, when Serial1_Connected sub was in the main activity and it was paused, my device would not autoconnect. I think it was because of the main activity was paused, and therefore I moved it to the service module

Mogens
 
Upvote 0
Yes. Of all the posts on this subject, this appears to be the closest to what I'm doing. Could either of you post the code that worked? Thanks
 
Upvote 0
Back
Top