I want to develop an application that use the internet connection and the bluetooth connection in background. Each 5min my application get information from my bluetooth device and send the data on a server via the internet connection. It's possible to have internet and bluetooth connection in background on android?
Yes this would be possible, You would want to use some form of service or ASyncTask to handle this which could sleep for five minutes to avoid any excess battery drain. You will also need to ensure that the app does not close when the user leaves the app so that you can continue sending and receiving data.
Here's some help on the internet side of things:
http://developer.android.com/training/basics/network-ops/connecting.html
and bluetooth:
http://developer.android.com/reference/android/bluetooth/package-summary.html
That should be enough for you to get started, best of luck! :)
Related
After searching a lot on Internet we have came to one conclusion in order to ensure an persist connection with XMPP server we have to create a service,
We have created one which uses Smack library to connect with XMPP server and it is working fine with mobile and wi-fi network.
Every time you make something design approach always matter!!!, Smack have this reconnection mechanism already implicitly implemented in there library which listen to connection and if connection drops Smack try to reconnect with XMPP server at some interval of time.
Our use case scenario::
INTERNET connectivity can be because of wifi or data network,here if connection go is idle state of someone turn off screen cpu goes to sleep now any data is sent to server on this connection there will be no response because server is no more listening to client ,on client side XMPP connection is already in connected mode connection listener is not detecting any disconnection from server,so here flow gets completed.
After searching on INTERNET we found that possible solution to solve this is to ping server after a fix (we are using 1 min as fix period),after ping fail detected ,we have implemented reconnection mechanism same as smack(idea taken from Smack reconnection mechanism itself)by making use of timer task.
Problem:: only problem we have is battery draining ,if user is still connected with INTERNET and reconnection interval increases it will drain batty.
1). What is the possible solution of above problem?
2). Should we have to take another approach?
How To Create Service In Android Which Makes Persist Xmpp Connection
With XMPP Server?
Two things
Reestablish the connection, by listening for CONNECTIVITY_CHANGED intents and determine if the currently used data connection went down (and was replaced by another).
Ensure that the connection is established by pinging the server
Remarks about
Listening for CONNECTIVY_CHANGED is not enough, you need to compare the previously active connection with the now active one. And if it's not the same, re-establish the XMPP connection.
Smack 4.1 comes with ServerPingWithAlarmManager, which will check if a ping is required according to the settings of PingManager every 30 mintues. This value is hardcoded an can not be changed.
Using 1 minute as Ping interval is way to much! As you have experienced, it will drain you battery very fast. A reasonable ping interval is something > 15 minutes and I recommend 30 minutes. Smack 4.1 will also ensure that a ping is only send if there was no received stanza withing the Ping interval.
Also use XEP-0198: Stream Management when possible.
I recommend looking at the various open source apps that follow these guidelines and achieve a stable, permanent connection without draining the users battery1.
1: Just following these advises can not guarantee that the battery will drained. There are more factors to take into consideration.
I am trying to build an application/service that will execute some coding everytime I connect to a specific wifi network (to log into that network and then keep the connection active ). I do already have the code for the login part and it is working fine, but I don't really know how I can automatically run this code when I connect to a network.
Ideally, I would like to build a service that starts when I join a specific wifi network, keeps the connection active (like makes a request every 10 min to keep the connection active) and finally stops when I leave that network.
I know how to build the service and keep it active, but don't know how to start and stop it automatically when joining / leaving a wifi network. Any suggestions on how to do this is appreciated. If my approach is not correct and there is a better/simpler way to do that, please let me know
Regards,
Marcel
you need a broadcast receiver.
this broadcast receiver will receives broadcasts of Wifi connected and wifi disconnected actions and from there you can start and stop your service.
Implement a BroadcastReceiver listening for the scan results from WifiManager.startScan()
Have a look at Android Scan for Wifi networks
i'm writing a client-server application which uses TCP socket connection. In my android project, Service creates a thread for listening the TCP socket.
Everything seems OK. But i have got one problem.. My network service running in background fine; But some time later (10-15 min..), when i try to open my application (main activity) again, I can't get responses from the socket connection. It freezes or something?? i cant send or get TCP messages from the socket.. What can be the reason of this? I'm working on my phone, via 3G connection.
(Besides, the app running in the emulator hasn't got such this problem; I assume Its connection is stable, long-during )
Thank you for your answering.
Due to power optimizations and perhaps changes in connectivity (GPRS/HSDPA/Wifi) it's very likely your connection is being dropped.
In order to maintain a connection, your background service needs to claim a wakelock using the PowerManager class. This prevents the device from going to power-saving mode and thus disconnecting your socket. But beware, this significantly lowers the battery life of the device.
Also, you need to handle changes in connectivity which break your open connection. Android sends out a broadcast message named android.net.conn.CONNECTIVITY_CHANGE to notify of changes in connectivity.
Depending on your use-case I would poll with when the device is in the sleep-mode and only build a connection when the device is actively in use or just use C2DM push notifications
When I have experienced something like this in my apps, it has usually been because of power optimisations on the phone (which cant be overridden). If the socket has been idle for too long, it is automatically closed and needs to be reopened.
Are you sending data from time to time? Like implementing a heartbeat protocol ? if you are not, you should...or maybe it has to do with socket READ/WRITE TIMEOUT
Like many apps my app depends on WIFI and Http Connection to operate. What I am not clear on is how I can time a wait until WIFI is enabled AND device is attached to WIFI network is. What is the best way to delay on application startup long enough for this to happen? during this time I would for example keep buttons deactivated. Also is there any way to make the device attempt to connect to its preferred network? Thanks
Take a look at ConnectivityManager and WiFiManager. Using these you can get events when network state changes
Use WifiManager to get the state of the connection.
Generally you will have to try. A Wifi connection can show as connected but not be able to actually send/receive because the signal is too weak. Once the connection is up, the app should try connecting and only when it succeeds, the buttons should be activated and so forth.
Iam connection to services trougth my app, I woul like to check if the connections gets broken, alert a message than.
Or is this taken carre of in android?
If not, How should I take care of it? is there a timer?
If you are connecting to built-in services of Android, for example Connection with GPRS, WIFI or MOBILE 3G connection, then you can register a Broadcast Receiver that will tell the user when the connection is lost.
But if you are connecting to your custom-made services, for example ABC Application, then you would have to make your own service, that will periodically (example every 15mins) will check the connectivity with ABC Application. Hope this helps.