Android Question Bluetooth and Background. b4a 8.3

kohle

Active Member
Licensed User
Longtime User
I am using the bluetooth example for test. When the app goes into the ackround (pause) then
the starter service is not receiving data anymore.

I use b4a 8.3, android 8 and I thought it should work, because
Update: Starting from B4A v8.3 the starter service will never be killed unless the whole process is killed. This means that you can always assume in your code that the starter service is running.
It is done internally by changing the starter service context to the application context.

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

Sub Process_Globals
    Private serial As Serial
    Private admin As BluetoothAdmin
    Private ast As AsyncStreamsText
    Public connected As Boolean
    Public connecting As Boolean
End Sub

Sub Service_Create
    serial.Initialize("serial")
    admin.Initialize("admin")
End Sub

Public Sub Connect
    admin.StartDiscovery
    connecting = True
    CallSub(Main, "SetState")
End Sub

Private Sub admin_DeviceFound (Name As String, MacAddress As String)
    Log($"Device found: ${Name}"$)
    If Name.Contains("central433") Or Name.Contains("zentral433") Then
        Log("Trying to connect...")
        admin.CancelDiscovery
        serial.Connect(MacAddress)
    End If
End Sub

Private Sub admin_DiscoveryFinished
    connecting = False
End Sub

Private Sub Serial_Connected (Success As Boolean)
    If Success Then
        If ast.IsInitialized Then ast.Close
        ast.Initialize(Me, "Ast", serial.InputStream, serial.OutputStream)
        Log("Connected")
        connected = True
      
    Else
        Log(LastException)
    End If
    connecting = False
    CallSub(Main, "SetState")
End Sub

Public Sub SendMessage(msg() As Byte)
  
'    Dim m As String
'   
'    m = BytesToString(msg,0,msg.Length,"")
'   
'    Log(m)
  
    ast.astreams.Write(msg) 'sending bytes, not text
End Sub

Private Sub ast_NewText (Text As String)
    CallSub2(Main, "MessageFromDevice", Text)
End Sub

Private Sub ast_Terminated
    connected = False
    CallSub(Main, "SetState")
End Sub



Sub Service_Start (StartingIntent As Intent)
  
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub
 
Last edited:

kohle

Active Member
Licensed User
Longtime User
The app seems not to be killed. When I open it from the taskmanager, bluetooth is still connected and receiving data.
When I click the home button, the app dont receive bluetooth data anymore.
I put a log("destroyed") command in the Service_Destroy sub of the starter service and its not triggered.
Also I put :
B4X:
Sub Service_Start (StartingIntent As Intent)
    Sleep(0)
    Service.StopAutomaticForeground
End Sub

Its am using the bluetooth example from b4a.

It use the module AyncStreamsText :

B4X:
#Event: NewText (Text As String)
#Event: Terminated

'version: 1.00
'Class module
Sub Class_Globals
    Private mTarget As Object
    Private mEventName As String
    Public astreams As AsyncStreams
    Public charset As String = "UTF8"
    Private sb As StringBuilder
End Sub

Public Sub Initialize (TargetModule As Object, EventName As String, In As InputStream, out As OutputStream)
    mTarget = TargetModule
    mEventName = EventName
    astreams.Initialize(In, out, "astreams")
    sb.Initialize
End Sub

'Sends the text. Note that this method does not add end of line characters.
Public Sub Write(Text As String)
    astreams.Write(Text.GetBytes(charset))
End Sub

Private Sub astreams_NewData (Buffer() As Byte)
    Dim newDataStart As Int = sb.Length
    sb.Append(BytesToString(Buffer, 0, Buffer.Length, charset))
    Dim s As String = sb.ToString
    Dim start As Int = 0
    For i = newDataStart To s.Length - 1
        Dim c As Char = s.CharAt(i)
            If i = 0 And c = Chr(10) Then '\n...
            start = 1 'might be a broken end of line character
            Continue
        End If
        If c = Chr(10) Then '\n
            CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
            start = i + 1
        Else If c = Chr(13) Then '\r
            CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
            If i < s.Length - 1 And s.CharAt(i + 1) = Chr(10) Then '\r\n
                i = i + 1
            End If
            start = i + 1
        End If
    Next
    If start > 0 Then sb.Remove(0, start)
End Sub
Private Sub astreams_Terminated
    CallSubDelayed(mTarget, mEventName & "_Terminated")
End Sub

Private Sub astreams_Error
    Log("error: " & LastException)
    astreams.Close
    CallSubDelayed(mTarget, mEventName & "_Terminated")
End Sub

Public Sub Close
    astreams.Close
End Sub
 
Upvote 0

kohle

Active Member
Licensed User
Longtime User
I found something :

The CallSub2 is not triggered in the Starter event when the app is in the background.
My log("receive") I get.


B4X:
Private Sub ast_NewText (Text As String)
    Log("receive")
    CallSub2(Main, "MessageFromDevice", Text)
End Sub
 
Upvote 0
Top