0

Hi i want to implement gps devices communication functionality in android devices but i don't know how these devices communicate with server that uses these devices data and save these data on server.I have to question about these devices
1-Can server connect these devices and get data and save data on server? Or these devices connect to server and send data to server.
this question is important for me because i want to write application for android devices that simulate gps devices functionality on android devices!
2:I research about how to connect from server to android devices and get information about mqtt! can i connect from server to android devices with mqtt?
for simulating these devices functionality on android devices on need to know which of server or device connect to other and send data?

user1344766
  • 93
  • 1
  • 2
  • 14
  • If the devices generate the data, it better to send that to server rather than server pooling each device after some time for data. A server communicating with client is good when server has something to tell a device or is asking device for infrequent data. If your GPS data is really infrequent or you want to collect it only at times ( for example where these devices are right now) use mqtt. – Hassan Apr 22 '12 at 22:44
  • And as far as my reading of MQTT tells me, it's a sms base communication model in which your app will intercept SMSes and process those in a specific format from your server. In response you can send data back to server via internet request or SMS. – Hassan Apr 22 '12 at 22:48
  • 1
    Thanks for your help i want to create a web site that has members with android devices and collect their location and show their location information in web site for example i want to get vehicle location of Transportation Company and show theirs information for admins! there are sites for this purpose that use gps devices to get information from vehicles but i don't know which of server or gps devices connect to other and send information!.I want to get timely location of vehicles and show for admins for this purpose server connect to devices or devices connect to servers and send data? – user1344766 Apr 23 '12 at 08:13
  • This project is GPS tracking that use GPS devices but i want to use android devices or iphone or other mobile phones. at this time i want to use android devices – user1344766 Apr 23 '12 at 08:14
  • 1
    @Hassan MQTT has nothing to do with SMS. It's a light weight publish/subscribe messaging protocol that uses TCP. – ralight Apr 23 '12 at 08:44
  • @ralight: Thanks for the info, I will gonna read up more on it as soon I get some time off. – Hassan Apr 23 '12 at 10:43

1 Answers1

1

First, you need to get the location position on the device, and then send it to your server, so you can show this information. Being pragmatic with code, you will need to get the location on the device with something like:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates("gps", 60000, 0, locationListener);

private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Here you have both location.getLatitude() and location.getLongitude()
        }
        public void onProviderDisabled(String provider){}   
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
};

for more information regarding location on Android refer to the official documentation on User Location.

When you are done with the location part, you can start sending it to your server. Consider to use JSON for this.

Let's consider you have a String line with "latitudelongitude", you would need to build the JSON object first:

public JSONObject buildJSONObject(String line) {
        String[] toJson = line.split(" ");
        JSONObject object = new JSONObject();
        try {
            object.put("latitude", toJson[0]);
            object.put("longitude", toJson[1]);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object;
}

And then you would send it to the server with something like this:

public boolean sendTraceLineToServer(JSONObject line) {
    // The mock server IP is 10.0.2.2, just for testing purposes
    // This server receives a JSON with format {"location":{"latitude":xx.xx, "longitude":yy.yy}}
    HttpPost httpPost = new HttpPost("http://10.0.2.2:3000/locations");
    DefaultHttpClient client = new DefaultHttpClient();
    JSONObject holder = new JSONObject();

    boolean sent = false; 

    try {
        holder.put("location", line);

        StringEntity se = new StringEntity(holder.toString());
        httpPost.setEntity(se);
        httpPost.setHeader("Content-Type","application/json");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    HttpResponse response = null;

    try {
        response = client.execute(httpPost);
        sent = true;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        Log.e("ClientProtocol",""+e);
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("IO",""+e);
    }

    HttpEntity entity = response.getEntity();

    if (entity != null) {
        try {
            entity.consumeContent();
        } catch (IOException e) {
            Log.e("IO E",""+e);
            e.printStackTrace();
        }
    }
    return sent;
}

Here you have more examples on how to post JSON to a server.

On the server, on my case I wrote it in Rails, I create a method to receive the JSON as simple as:

# POST /locations
# POST /locations.xml

def create
    @location = Location.new(params[:location])
    respond_to do |format|
      if @location.save
        format.json { render :json => @location, :status => :created, :location => @location }
      else
        format.json { render :json => @location.errors, :status => :unprocessable_entity }
      end
    end
  end

And that is it, location on the device, sending it using HTTP with JSON, and receiving on the example Rails server.

Community
  • 1
  • 1
Eduardo
  • 4,282
  • 2
  • 49
  • 63
  • Thanks for your help in these codes device send information to server i don't know which mechanism used in the sites like http://gpstrackit.com/ Device send information to server or server get information for specific vehicle? – user1344766 Apr 23 '12 at 08:59
  • @user1344766, they provide a product, so if you can buy it does not matter the technology behind it, but whether it solves your problem or not. Since you are posting here a question related to Android, I think you want do-it-yourself, therefore, my answer provides you the bare bones for you to accomplish that. I think you can do it, if the requirements are as simple as send location information. If my answer fits your needs, please mark it as so. – Eduardo Apr 23 '12 at 09:07
  • Can i scale this solution for other platforms like iphone or windows mobile devices? – user1344766 Apr 23 '12 at 10:31
  • @user1344766, I do not know what you mean with scale in this context here, instead, I think you meant portability. If that is the case, no, it is Android solution, of course the server is unified implementation, and from the server perspective it does not matter what kind of device it is since it is sending JSON. And after all, the abstraction is the same, you need to learn how to do that with iPhone and Windows. – Eduardo Apr 23 '12 at 12:39