How does a Bluetooth Socket connection work on Android? - java

I am new to bluetooth. I understand on Android I can connect to another bluetooth device as a client through a BluetoothSocket connection. How do I handle data that is received? In what format does data get sent from the server to the client?

Going through your questions:
How do I handle data that is received?
The ideal way to handle data is with threads (although it is possible to use a service). A single connected thread running through an infinite loop will attempt to try to read data from the stream that is provided by the socket connection.
In what format does data get sent from the server to the client?
Data is sent from server to client via bytes (specifically arrays of bytes). If you know the type of data being received, there are plenty of functions to convert this data to the desired variable type.
For a complete example of how bluetooth works between devices, I'd highly recommend taking a look at the source of the BluetoothChat Sample application. There's also the Bluetooth overview.

Related

Fetching data from another device

I'm working on an shopping list app where all the family can connect to the same list.
How can i make and save the list on one device and then connect from all the devices to the list?
What you need is the concept of Websockets.
WebSockets represent a long awaited evolution in client/server web technology. They allow a long-held single TCP socket connection to be established between the client and server which allows for bi-directional, full duplex, messages to be instantly distributed with little overhead resulting in a very low latency connection.
Thats means if you create a server with a web socket connection, and you allow other clients(android) to connect to that connection. You can send messages back and forth and every device connected will recieve it. All connected is how i like to call it. Think socket.io and node.js.
You can't fetch the device data without having a medium or network in between them and if you choose Bluetooth or wifi then your data will be local and other family member living another city won't be able to contribute. so at last if i'm guessing right you need a synchronised database which can give you realtime updates from another user.
This is where Firebase Realtime database comes in. you can use it in your project, it's easy and takes few minutes to configure.
since all the user will have their own app they can contribute to same database and others can see it instantly.
Read the docs here for android

Send continues data to client through websocket

I have a requirement to send the huge data through the websockets. My actual requirement is, the client communicates with my server for huge test data. My server will send the data assume the data size is 1GB. Its very hard to send this 1GB of data in a single response. So I choose the websockets. I am very new to this topic. I read about the websocets in multiple blogs and everyone gave a chat application example. But in my case client will ask once and my server needs to send the continues data. Is it possible to send the continues test data to the client with the websocets.? Can any one help me on this and if possible can you please provide an example.?
Note: I am using JAVA.
Thanks & Regards,
Amar.T
You already have the chat example. Use it. Try it on single computer. Probably you will need to create own protocol for sending/receiving data. Try to send by limited sizes blocks (for example 10 kb). So you will have 2 applications: client and server used websockets. I think that the main problem here is - What to do if connection was lost?

How does a chat app communicate with it's server?

I currently am developing a semi-simple chat app. I want the user to be able to communicate with one other person in a private chat. Just plain text will be sent. Currently my system calls php scripts from a webpage, passes in parameters and then parses the data returned. I have it so that the client sends the message, which calls a send message script on my webserver, the script then makes a message file on the webserver and then returns a success or failure back to the client. Then for the client to view this message, it would have to call a script that checks the server for a message file with a message for him. If it finds one, it sends the message back, if not, it sends a response about not having messages.
This technique works perfectly besides the fact that the client either would have to manually refresh to check to see if he had messages, or a background thread would have to refresh every few seconds. That would be fine, however that would use data if the user was on a mobile network. Not to mention the kind of resources a background loop would pull if it was refreshing at a speed that would be convenient.
So, I decided on a second idea, this would be a server programmed in Java, which would communicate over sockets. The user would send the message as a packet over the socket and the server would check to see who it was meant to go to. If the person is online, it passes the message along to that user. However this method requires a constant connection between the client and the server and that is unreliable because what if the user is in the car and data cuts out. Or some other situation where the connection gets severed. Then it would throw errors and have to reconnect.
Anyhow, my question is which technique is better. Or are they both terrible? If so, what is the correct way of doing this? Thanks in advance.
AngularJs and Ajax will be the perfect solution for you , try to learn
for actually real time messaging Use AngularJs
If the amount of data is very less ..say 20-25 messages per day...you can REST APIs on your server to transfer actual text messages and Google Cloud messaging for pushing notifications..Recently I followed this approach to develop private chat for one of my friend.

Server Sockets in java

I am creating an app for android which uses server socket over wifi-connectivity to exchange data between devices. I am able to send and receive data from server (hotspot) device.
My problem is that I want to send data to all other client devices as soon as it is received on server device from a client. Any IDEA how implement it??
Also the data I am exchanging is an Audio stream. Any suggestions will be appreciated..!!

Restlet streaming data

I have this task that I'm undertaking where I would be reading data from a device and make it available over a web service. The data is read 4 times a second. I want the web clients to be have an open HTTP connection and get the device readings as a stream using chunked transfer as long as the client keeps the connection open.
As a proof of concept, I want to start with a service that constantly generates a random number, 4 times a second, wraps it in json and stream that to clients. I'm trying to model it loosely based on twitter streaming api.
I'm using restlet 2.1.2 to create that webservice but I'm not sure which Representation I should be using to achieve this. I tried searching for this but didn't find anything useful. Could someone point me in the right direction as to what I should be using and maybe some examples, perhaps.
Thanks
To achieve what you are trying to do, I'd use the WriterRepresentation (but see my answer to your other question), but I'm quite sure that you are going in the wrong architectural direction.
Indeed the following image from the documentation you linked
shows how even the Twitter streaming api is not intended to be connected by users, but by background processes that download messages in a store accessible by the HTTP. Users poll only the HTTP server, that reads the messages from the store and sends the back to the clients.
As a disconnected protocol, HTTP enable massive scalability that would not be possible otherwise. If each client establishes a persistent TCP connection backed by a dedicated server thread, you will rapidly exaust server resources! Moreover any HTTP proxy between the User Agent and the server could cause unexpected behaviours.
Thus, if you are bound to the HTTP protocol, the User Agent should poll. You can reduce the network load with headers like Last-Modified/If-Modified-Since or Etag/If-None-Match.
However, if you can adopt a different protocol, I strongly suggest to try a service bus over a connected TCP protocol.

Categories