im currently writing an android app, which should keep track of other devices in the same wifi network using JmDNS.
The discovery process works correctly, but i don't know how to keep track of other devices especially noticing their removing.
As mentioned in
JmDNS device removal detection it seems like i have to implement this callback myself but I dont have any clue how to do this. It seems like the JmDNS Api doesn't provide any method to publish service messages myself.
My ideas so far:
Use the discovered socket connection to keep track of changes
Use JmDNS.requestServiceInfo() from time to time to check if the service is still available
Un-/Register services + listeners so they can find each other again
Does anyone know another way to solve this issue or could tell me how to trigger the serviceRemoved() callback?
PS: before taking this approach I tried Androids-NSD API, which seems to be quite unstable
So here are some things i figured out while experimenting:
The DNS-cache is set to one hour, which means that listeners won't remove a service as long as that counter didn't finish.
The mistake i made was unregistering my service after the wifi was switched off. Services send broadcast message that they aren't available anymore. This message can't be sent if the wifi connection was shutdown.
So instead of calling JmDNS.unregisterAllServices() after the wifi connection I have to call it when it's available. This leads to the onServiceRemoved() callback beeing fired in the listener
Related
I'm trying to figure out if it is plusible to make an app constantly look for any kind of internet connection so once it finds any it makes a comparison of its local database with the one in the cloud and upload anything that is not in the cloud.
I was thinking about a code that triggers every time the phone connects to the internet but my research turned to be inconclusive and I'm running out of time.
You wouldn't do it that way. There's methods of being notified when an internet connection is made in Android, but you really wouldn't even do that. Instead you'd use WorkManager to schedule a job that requires internet, and let it launch the job at an appropriate time for you. See https://developer.android.com/topic/libraries/architecture/workmanager
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.
If two (or more) devices are connected to the same network, and each has my apk installed, how might one device efficiently 'talk' to the other? Google Play services, Wifi Direct, and bluetooth is unfortunately not available on these devices.
I thought of using a 3rd party push notification service, but ideally I need the response between either device to be as fast as possible, and long-lived.
I have managed to get two devices sending messages to one another using the old client-server Network Discovery Sample app in the docs. However, if either of the apps is closed or leaves memory, the connection is obviously broken. Therefore I'm trying to figure out if this is possible through a Service, which I understand exists outside of the Activity lifecycle.
I understand how an Activity might connect to a Service to send a message (good sample on that located here), but from what I gather this all happens locally on the device. Is it possible to have this exchange happen over a local network, from one app to another? I guess what I'm saying is how can I set up a basic client server socket relationship between two apps that won't die?
It has been a long time but it should still work.
The problem here, as I understand it, is to have something that keeps running when the app is gone.
I remember using IntentService for this purpose. In the onHandleIntent() we made it loop while(!stopCondition) {...}
It was a stable solution then but it was around kitkat's time.
I'd try with the solution in your first paragraph being executed and managed by the IntentService which should keep it available.
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.
OS: Android
Given: User has stated that they wish to remain connected to app's server to receive updates, etc.
Goal: To ensure that users are connected to app's server even when app is in background state.
Question: One problem has been occasional disconnects from the network. If a user loses data network connectivity (loss of 2G, 3G, WiFi) and then later regains connectivity, our app is left without a connection. I am currently trying to make use of PhoneStateListener's in order to detect various network changes and thereby restart connectivity with the server when appropriate. On my Nexus One (2.1), I find that onSignalStrengthsChanged and onDataConnectionStateChanged aren't called except when the listeners are originally registered, but not afterwards. The listeners are registered inside of a Service so they are continuously listening as long as the Service is alive (which we can assume to be 'forever' for purposes of this question). Has anyone else had any issues with listening to the state of the Data Connection?
onServiceStateChanged seems to be the most reliable so far, but I wanted to know if people have had success with the other listeners as well.
I guess you'll have to send keepalive messages at regular intervals to check whether the connection is still there. If not, reestablish it. There is a smorgasbord of reasons why your connection might drop, and you won't be able to check all of those client side.
Might consider using google's cloud service for what you're doing though, since they already keep an connection open for that. That way your user's phones won't have the overhead of keeping yet another connection around (which can be quite expensive)
Since StackOverflow doesn't allow me to close the question otherwise, I will provide the answer to why my PhoneStateListeners were not working:
I discovered that my problem was simply that I wasn't registering my listeners in a bitwise manner, but rather successively (i.e.
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
telephonyManager.listen(listener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
.
.
.
instead of (the correct):
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE..............);)
I will close this question and perhaps later open a different question asking for design suggestion on maintaining 'always alive' connections to a server on Android. Thanks.
As far as i know listeners could not be called when app is in background. You should try use services not activity.
And also remember to retrieve TelephonyManager in proper way
TelephonyManager mTelephonyMgr = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
I would think you would use the ConnectivityManager for this.
Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get an instance of this class by calling Context.getSystemService(Context.CONNECTIVITY_SERVICE).
http://developer.android.com/reference/android/net/ConnectivityManager.html
per the docs, it monitors network connectivity and sends broadcast intents on change to applications.