Android Question asyncstream close requirement

mterveen

Member
Licensed User
Longtime User
when i close the application from main activity i have the following question regarding the simplified code below.

1) do i need to close the asyncstream specifically when i stop the logger service?

i've read you should not reuse an asyncstream. however, since it is in the starter service (and assuming the starter service hasn't been killed) can i just initialize it again as shown?

B4X:
[starter service]
Sub Process_Globals
   Public usb As UsbSerial  
   Public AStreamUsb As AsyncStreams
End Sub

[main activity]
sub startlogging
   starter.usb.open(9600,1)
   startservice(logger)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   If UserClosed Then
      'clean up some stuff
      StopService(BTLogger)
      Activity.Finish
   End If
End Sub

[logger service]
Sub Service_Create
   Starter.AStreamUsb.Initialize(Starter.usb.GetInputStream,Starter.usb.GetOutputStream,"astreamusb")
   Service.StartForeground(1,notes)
End Sub

Sub Service_Destroy
   Starter.usb.Close
   '?????  Starter.AStreamUsb.Close
End Sub

Sub astreamusb_Error
   Starter.AStreamUsb.Close
End Sub

Sub astreamusb_Terminated
   Starter.AStreamUsb.Close
End Sub
 
Last edited:

mterveen

Member
Licensed User
Longtime User
followup info. i was running into 2 problems with the code as written above.

1) when restarting the app, and in particular restarting the logging service, the starter.astreamusb.initialize code would generate a java null reference error. i would have assumed since it was a public variable associated with the starter service it would still be "valid"
2) when terminating the app, the starter.usb.close line would generate an error saying it was ignoring the event since the service was already destroyed. don't quite understand that one.

anyhow, here is how i had to mod the 2 subroutines to get everything to work.

B4X:
[logger service]
Sub Service_Create
 
dim a as asyncstreams
Starter.AStreamUsb = a
Starter.AStreamUsb.Initialize(Starter.usb.GetInputStream,Starter.usb.GetOutputStream,"astreamusb")
   Service.StartForeground(1,notes)
End Sub

Sub Service_Destroy
  '       Starter.usb.Close
  '      Starter.AStreamUsb.Close
End Sub

so, what's up with 1) and 2)? i'm glad i got everything to work, would just like to know why the other setup didn't work!
 
Upvote 0
Top