Android Question B4A serial.listen in a service crashes

DickD

Active Member
Licensed User
I'm trying to write a Bluetooth/AsyncStreams program using a listener in a service. I have had other BT/AS programs work properly before but this is the first time I am separating a request sender on one phone to a receiving/listening service on another. The code of both sender and listener are below. The listener side crashes when the request is sent with the error message: "An error occurred: Line 34 (End Sub). java.lang.Exception: Sub serial1.connected signature does not match expected signature". I have a feeling I'm missing something really simple and obvious but can't figure it out.

B4X:
'This is the request sending activity
Sub Activity_Create(FirstTime As Boolean)
     serial1.Initialize("serial1")
     admin.Initialize("admin")
     MacAddress = DisplayMatches.MacAddress
     Name = DisplayMatches.Name
     Log("MacAddress = " & MacAddress)
 Try
     ProgressDialogShow("Trying to connect to " & Name & " at " & MacAddress)
     serial1.Connect(MacAddress)
 Catch
     Log(LastException.Message)
 End Try
End Sub

Sub Serial1_Connected (Success As Boolean)
      ProgressDialogHide
      Log("Connected: " & Success)
      If Success = False Then
          Log(LastException.Message)
          ToastMessageShow("Error connecting: " & LastException.Message, True)
      Else
      If AStream.IsInitialized = False Then
          AStream.InitializePrefix(serial1.InputStream, True, serial1.OutputStream, "AStream")
      End If
      AStream.Write("Send".GetBytes("UTF8"))
      Log("Request sent, waiting for reply")
      End If
End Sub

Sub AStream_NewData (Buffer() As Byte)
...

B4X:
'This is the listener service
Sub Service_Create
      serial1.Initialize("serial1")
      admin.Initialize("admin")
End Sub

Sub Service_Start(StartingIntent As Intent)
 Try
      Log("Bluetooth state: " & serial1.Isenabled)
      serial1.Listen
      Log("Listener started")
      Catch
           Log(LastException.message)
      End Try
End Sub 'This is line 34

Sub serial1_Connected
      log("Connected")
      Try
           Log("Is Astream initilaized?")
           If Astream.IsInitialized = False Then
               Astream.InitializePrefix(serial1.InputStream, True, serial1.OutputStream, "AStream")
           End If
      Log("Astream initialized") 
      serial1.StopListening
      StopService("")
      Catch
           Log(LastException.message)
      End Try
End Sub

Sub AStream_NewData (Buffer() As Byte)
.....
 

DickD

Active Member
Licensed User
You should always use services for network communication.

The event sub signature should be:
B4X:
Sub Serial1_Connected (Success As Boolean)
Yes, that was it. I knew it was something simple that I just couldn't see. Thanks.
 
Upvote 0
Top