0

I am kind of desperate. I am working on an application that is supposed to visualize pollution data. The application connects to the sensor via WiFi direct, the sensor is the group host. Then I am supposed to connect to port 5001 and retrieve data every 2 minutes. This data is supposed to be sent to a google maps activity where it will be visualized in an infowindow.

At this moment I have only been able to create a TCP connection that closes down immediately, I followed this Really simple TCP client.

So I need to establish a TCP connection that does not close and also runs when I am in other activities. I do not need to send any messages, only retrieve data.

I looked into this How to keep the android client connected to the server even on activity changes and send data to server? but did not understand how I am to implement that code for my case.

How do you do this? Appreciate any help.

Community
  • 1
  • 1
Kspr
  • 665
  • 7
  • 19

1 Answers1

1

Your android App is a Client, every time you need to get data you can ask it from the Server.

If Server working in Blocking mode you can send a request to the Server and Server sent Response through the opened connection

Here is some examples of Client/Server connection with Socket :

https://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/

https://github.com/codepath/android_guides/wiki/Sending-and-Receiving-Data-with-Sockets

You can find more examples

At this moment I have only been able to create a TCP connection that closes down immediately

It relies on your Server design, Client app will send the request to the Server and if Server send back the response you can get it, but if the Server have no Response to sent your connection will be closed

So I need to establish a TCP connection that does not close and also runs when I am in other activities. I do not need to send any messages, only retrieve data

See the links above, there is no need to create a connection that does not close, you should send your request and get the response from Server every time you need

Mahmoud_Mehri
  • 1,643
  • 2
  • 20
  • 38
  • Hi! Thank you for the answer. So I have not designed the server at all. Previously I have only created the TCP client. This is because the sensor will act as the server. I am only to connect to a port and get the data that the sensor sends every 2 minutes. Can I do this with the above code you linked? Or do you think that my old code might even work? The one in the "Really simple TCP client" link? – Kspr Apr 08 '17 at 18:16
  • I am not supposed to send commands to the server, I will just read from the socket continously. – Kspr Apr 09 '17 at 08:12