Keep getting messages, Socket - android

I have a scenario which I need to resolve. Currently I'm able to connect to an embedded system through socket connection, via android device.
I was able to use asynctask to send xml commands and receive them back, update UI with the results. But on the last step I need to use a command which will start the system to work, and I will keep getting messages from the system. it will be sent variously and the time can be different (we are talking about few 200-500 ms).
So my question is:
Asynctask wouldn't work. Because the 'work' varies more than 100ms and I'm not sure when the messages will be send, So I can't use async and show dialog for unknown time.
I have read that intent-service or service can do this work, but I'm not sure yet if it will be a good solution.
What would be a good solution for receiving these messages and for updating the UI?

Related

How to have a task in the background which can read and write data from a Bluetooth connection?

I'm currently making an app to exchange data between phone and a bluetooth device. I need this exchange to be in the background so when the device send some data, the app can read it and act from it, whenever the app is active or not.
I already establish some read and write data in the app, but only when it is active for the user. Now I want to take the next level and make the same read and write data but in the background, so the connection between the devices is establish and maintain.
I already know about jobschedule and service. Also, I know that, with the new versions of Android OS, services are killed by some time passed or in Doze mode. Other things I found out was to use threads in the background, but I'm still searching for that. But in the end, I'm still a newbie and maybe I'm missing out some information or I couldn't understand the concepts in their fullness, because I can't see yet how to make a process in background that could react upon a message received from the device or react upon a message received from the phone and create this exchange data.
In all, this raised me a question: how to have this kind of task in the background which read and write data from bluetooth connection between phone and a device? Is it jobschedule? Threads? Or there is one that I'm missing out?
Yet, in SO exist some questions about this related topic, some are outdate about the killing service which android does now, but if this question is a repeated one, please link me one that can solve this problem. I'll be gratefull.
Side note: I would like to know if there is a way to turn this task on and off by some condition or by the user's preference.
EDIT:
Some questions I found:
How to keep Bluetooth connection background? - This one is recent, but I don't know if this service will not be killed by the system.
Android 8.0 background services for bluetooth device - This is recent too and it has in his answer the bluetooth connection could be killed too.

Proper error handling when sending an XMPP push notification using go-gcm?

I'm using https://github.com/google/go-gcm to send push notifications from our Go backend to Android devices. Recently, these push notifications started failing because the call to SendXmpp() was returning with the following error:
write tcp <IP>:<port>-><IP>:<port>: write: connection timed out
Restarting the Go process that called SendXmpp() makes this error go away, and push notifications start working again. But of course, restarting the Go process isn't ideal. Is there something I can do explicitly to handle this kind of error? For instance, should I close the current XmppClient and retry sending the message, so that the retry instantiates a new XmppClient and opens a new connection?
I would recommend using or implementing a (exponential) backoff. There are a number of options on GitHub here; https://github.com/search?utf8=%E2%9C%93&q=go+backoff though that's surely not a comprehensive list and it's not terribly difficult to implement.
The basic idea is you pass the function you'd like to call in to the back off function which calls it until it hits a max failures limit or it succeeds. Between each failure the amount of time waited is increased. If you're hammering a server, causing it to return errors, a method like this will typically solve your problems and make your application more reliable.
Additionally, I'd recommending looking for one that has an abort feature. This can be implemented fairly easily in Go by passing a channel into the backoff function (with the function you want to call). Then if your app needs to stop you can signal on the abort channel so that the back off isn't sitting there with like a 300 second wait.
Even if this doesn't resolve your specific problem it will generally have a positive effect on your apps reliability and 3rd party API's you interact with (don't want to DOS your partners).

Best way to deal with connection errors when connecting to web services

I have an android app that sends and receives information from a vb.net web service. Things are great if there is a solid and fast internet connection but otherwise, everything becomes shaky.
The process goes like this:
1. The app sends information (such as an ID)
2. The web service returns information based on the ID (like a name)
3. The app updates the database within it based on the info sent by the web service.
The problem comes when the internet connection is weak. What happens is the app still sends the information but sometimes the information being received is incomplete or a connection timeout occurs; and the app still goes into step 3.
The app shouldn't be updating the database because the transaction was either incomplete or with errors. Any ideas on how I should go about this?
I was thinking of making sure that the transaction is complete before updating the DB. So the web service also sends a sort of 'complete' signal to tell the app that it's time to update the DB. Is this a good idea or is there a better way to go into this?
As for the connection timeout issue any way to catch that? I've seen a catch for the timeout but are there other alternatives which are better? Also if the connection times out what happens to the data that was sent and received?
Much obliged

In Android how do you get an activity to continually listen for a message from a server?

I've tried searching around and I can't find a good answer that makes sense.
Basically when the activity first starts it should connect to a server and continually listen for a message. When it gets it, it'll parse it and if it's a particular message, close the connection and then do some other stuff. I should also be able to close it if the user pushes a button.
I tried searching around and I honestly can't tell if I should use servlets or services or what (edit: or how to use them). I do know that it probably needs to be on it's own thread.
So what should I use?
Edit: I'm not sure how to use the servers/services/etc. to make it happen
An IntentService is what you're gonna want. It's the easiest to implement and it's perfect for network operations. It automatically handles running the service in its own thread. Once you establish a connection with the server, it should stay open until you respond with some message from the server.

Complicated android multithreading problem, looking for some guidance

New to stackoverflow, been very helpful searching, but alas the time has come to ask a question.
I am trying to use an android 2.2 single core phone to do some research. I have implemented an algorithm that does quite a few calculations and produces a lot of data. These data must be processed, and the solution presented back to a client app within a 40ms time frame, then process again with new state data coming from the client. Also, the result of the calculations must be stored to the SD card as a data log. So being new to multithreading and android both, what should I use to do the following in my app: (As a side note, this phone, when in research mode is not intended to be used as a phone, phone will be in airplane mode with wireless off, and all apps that can be turned off will be turned off, and there is no need for UI display or interaction once it is up and running...)
need to process packets coming in over adb on serial port, these packets are state data that the program needs to perform its calcs on. These packets will be coming every 40ms, so I planned on using their arrival to trigger the start of the processing.
need to know if the algorithm is taking longer than 40ms and cancel it if so and send a message back on the serial port that it overran.
the calculation results need to be sent back over the serial connection via tcp and adb
The calculation intermediate data need to be recorded to SD. This can be quite a lot of data, on order of 140k, every 40ms.
So I have had trouble getting all the pieces together. I can't get my head around how a single core is going to keep up with all this going on at once?
So here is my thought, please tell me if I am headed in the right path. I am not asking for you to solve my problem, only any advice on how to break this beast down:
So i start a service to process the tcp packets coming in from the client
Use a service bound to the main worker thread to handle writes to the SD card
So assuming this setup, can i make the algorithm part of this somewhat deterministic so that it always runs if it gets a new tcp packet, and preempts the SD write going on in the background?
Argh...should have picked something simpler for my first program
Thanks.
Yes I think you are right, that it would be better to pick something easier for your first App ;)
But as far as I understand what you are trying to do, I don't think, that you need asynchronous multiprocessing. You get some data want to process it and pass a result. I think a HandlerThread is exactly what you are looking for. It is able to recieve Messages
with data inside. You send them to the Handler and process them in an overridden handleMessage(Message m) method. So everytime you recive a Message you could just log the Time
and see if the last one is older than your limit. If it is, you could just throw the Message or the whole queue, or send a Message to your serial-port inicating the overflow.
This could be implemented as you suggest in a Service. Another HandlerThread can be started with Thread.PRIORITY_BACKGROUND to write everything to SD.
You can send Messages even very compfortable if you apply a Messenger to the Handlers

Categories

Resources