Read data from socket if new data arrives in Android - android

I have an application that communicates to server. I want my application to READ ONLY the socket's inputstream when NEW DATA is available for fetching.
Currently, I create a timer that scheduled for the reading of socket's inputstream every 20ms(polling) using SocketChannel class of java.nio.channels. This is not that good because it will end up of reading the socket even if there's no available data. And it drains the battery fast.
Is there an API for Android that will tell or send a flag to the connected client whenever a new data is available so that it is the only time I will read the inputstream?

See this, but the short answer is no, there is no API for this. BTW, SocketChannel/Selector won't actually read the socket if not data is available, just check it's status.
For push notifications, you might check the Android Cloud to Device Messaging Framework.
AFIAK, it's implemented in a similar fashion: they keep a socket open, and send notifications
when available. It is most probably already tuned for low battery usage, and is part of the OS, so might be worth a try (2.2 and above).

Related

How do I go about sending data between 2 devices which are paired via Bluetooth in Android Studio?

I'm still new to Android studio and I'm having a lot of trouble with Bluetooth, I'm currently making an app where I need to send data between devices. I'm letting the phone's default Bluetooth setup do the pairing but now I need to know how to send the data, I know I need to use input and output streams but I don't know exactly how.
And yes I have searched all over Google, I've followed a lot of Bluetooth tutorials but none of them seem to really explain how to send data from one device to another.
Thanks in advance.
After you establish a secure/insecure connection via bluetooth the rest is just socket programming simply. That is lets think about sending a text. We convert the text to byte and send that by Java OutputStream. In the same manner for the data received we can get it by InputStream.
But remember you need to maintain bunch of code and thread/handler to maintain state and others. Though the basic thing is simply socket programming over Bluetooth socket using the Bluetooth adapter. Have a look at the below repository in github. This creates a chatroom over bluetooth. i.e it sends and receives string data
https://github.com/zahansafallwa/Android-bluetooth-chat-with-emoji/tree/master/app/src/main/java/com/zahan/safallwa/donttalk
Specially have a look at the BluetoothChatService class. That contains codes related to sending data. BluetoothChatService
Edit:
As per your comment lets think that your devices are paired and also connected. Now you only need to send the text. Declare a outputstream
private final OutputStream mmOutStream;
Suppose you have a string. We convert it to byte. Then get our socket outputstream and send data by write() method
String message="this is test data";
byte[] send = message.getBytes();
mmOutStream = socket.getOutputStream(); // here socket is the bluetooth socket you establish
mmOutStream.write(send);//this is what sends the message
Remember:
Edited code is for your understanding only. It is prescribed to use separate thread for sending data.

How do i send information via a setup bluetooth connection

So im working around with bluetooth and trying to figure out how to send two strings via a bluetooth connection. From one android device to another.
I found this guide http://developer.android.com/guide/topics/connectivity/bluetooth.html
but it talks alot about setting up the connection. So i went straight down to the chapter about Managing a Connection. The reason i do this is that in the apps i create i plan to setup the bluetooth connection before opening the apps (via the phones usual bluetooth setup) and then open the apps and send when it is necessary.
So my question is how do i find the bluetooth socket that should be setup? Since that should be what im searching for to create the sending and recieving threads?
Hope this is enough information, else tell what more you need and ill try and answer to the best of my ability.
Best Regards Drakthal
The usual bluetooth setup only pairs between devices, it doesn't create a data connection between them (And even if it would, you wouldn't be able to access this Socket object because it's not created in your process).
After Bluetooth is turned on, you can call BluetoothAdapter.getBondedDevices() to get a set of the paired devices. You can then iterate over them, and initiate a connection to the one you want. You can't avoid the connection creation :( If you want a simplified example, you can look here (An answer I posted a while ago, regarding the whole pairing/connecting/sending/receiving subject with bluetooth).
Once you acquired an open connection, sending the 2 string is easy.
String s1 = "A", s2 = "B";
byte[] buf1 = s1.getBytes(), buf2 = s2.getBytes();
OutputStream os = connection.getOutputStream();
os.write(buf1);
os.write(buf2);
os.flush();
connection.close();

Android data backend one way synchronisation

I create a monitoring application who :
if there is a network connection available : she sends periodically
measurement data to the server using json
if there is no network available, she stores the data in the sd card and sends it when the network connection is back.
Actually I use a circular buffer in memory that I empty when data are sent
Is there already something usefull in the framework or I have to write that completly ?
Thanks
I would check tape library from square. I've never tried it but looks what you are looking for.

Reading Bluetooth input stream in android

I am developing an application that read data from biometric devices using Bluetooth when I send request to biometric device for sending data, biometric device show response with updating its display screen but when I call function for read input stream for getting response the function in_stream.available() return 0. I am not able to trace out the root of problem. I have test same biometric device with some other app it work fine.
Help me if any one have idea about this issue.
Thanks in advance.
Do no use available() method. It is broken in most implementations. You should be constantly reading with read() or read(byte []). If the protocol lets you know the size of the expected data (i.e. some first bytes telling how much data is coming afterwards) you can just read that amount of data.
If the amount of data is unknown or you expect asynchronous data comming then you should manage the writing/reading to/from the streams in a separate thread. This does not only applies to Bluetooth but also to any basic stream handling (network, files, etc.)

Android Socket High CPU usage

I did a client/server(android/pc) and it seems that network usage from client uses a lot of CPU. Like to receive only 4k-5k from network, the cpu rises to 33 milliseconds. The cpu can be higher than 90-100 milliseconds if data is higher like ~32k.
First, I've tried the client(network part) in java version and after in c and the problem is still there.
I profiled the server part that send data and it uses about 0 millisecond.
Some details:
TCP connection.
The client connects to the server, client sends request, server sends
data (chunk of 4-10k), client send request, server sends...
Network part is threaded.
Get data with (recv or recv/select).
Smart Phone: Nexus one.
Tested in profiler mode (only network part and display fps/milliseconds).
Tested in Wifi (computer, phone, network are close).
Let me know if you have any suggestions or questions.
Thanks.
Are you using BufferedOutputStream on Android side to write the data? If not, it writes it byte by byte, which would explain the high CPU usage.
If this is not the case, please add some source code to the question.

Categories

Resources