B4J Question How to stop a non-ui App?

Mark Read

Well-Known Member
Licensed User
Longtime User
I have my app working as a non-ui app (on a RPi), communicating with a B4A app on a tablet via bluetooth. If I start the app via B4J-IDE in release mode, all is fine. To stop the app I can use kill process.

How can I stop the app when not using B4J and bridge? I see only the UI from Raspian on my Pi and no indication that the app is running.

Would it help if I create a .desktop object and run the app in a terminal window? In this case would the log commands also go to the terminal console?

Many thanks.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Yes, running your app in a terminal window will display the app's logging messages. Closing the terminal window will kill the app (unless you used the nohup command). This won't be a graceful closing, though. You won't be given the opportunity to release resources and save files and stuff. There are a few other options for gracefully closing non-ui apps. You can use the jColorLogger library to enable terminal input and you make it so that typing "exit" into the terminal gracefully ends the app. You can also use the jFileWatcher library to watch a directory and gracefully close the app when it detects a certain file deletion or something.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
jColorLogger - Nice library! I think I still have problems here. I propose to use the RPi headless and without keyboard. Therefore my only possible way to exit the program would be to send a command over bluetooth from the tablet which then runs a shutdown sub on the Pi.

I tried to do this but nothing happened. I should add that the Pi is sending data every second to the tablet via bt.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
What do you mean nothing is happening? The app isn't shutting down? Are you calling
B4X:
ExitApplication
? Are you gracefully closing down your BT/Serial ports and Timers and such?
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
On the tablet I have b4A running with the bt connection. When I press a button it sends a value (say 9) via bt to the Pi. The Pi should react in its "Sub astream_NewData (Buffer() As Byte)" but it doesn't. I tried slowing the data transfer to 2 seconds, thinking it might need more time but no effect. It would seem that the more commands I put into this sub, the less it reacts. I have now reduced the sub to this:

B4X:
' Called when stream gets new data
Sub astream_NewData (Buffer() As Byte)
   rcvStr = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
   Log("Newdata received: " & rcvStr)
   If rcvStr.Contains("Logger") And DebugMode=True Then
     Timer1.Enabled=Not(Timer1.Enabled)
   Else
     SensorMode
   End If
End Sub

My tablet sends to commands at present, "Start Logger" and "Stop Logger". The sub SensorMode switches between debug mode and sensor mode.

If you wish I can post both apps here. They can be tested in debug mode as the sensor must not be connected.
 
Last edited:
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
I recommend you use AsyncStreamsText to manage the communication. This will mostly take care of control bytes and stuff like that. Don't forget to append CRLF to the end of each message.
 
Upvote 0
Top