Reputation: 3574
I have a device that sends me it's location. It usually sends the coordinates obtained from the GPS, but if there isn't good GPS signal, it makes a GSM triangulation and sends me the MCC, MNC, LAC and CELLID values.
Having these 4 values, I need to obtain the position coordinates (latitude, Longitude). To achieve this, I'm trying to use the google's GLM hidden service (http://www.google.com/glm/mmap). It is supposed that pasing these 4 values to that service, it returns the latitude/longitude coordinates, but there is not much information about this service on the net.
With the recolected information, I have coded a small app that does this operation, but is not working. This is what Locgact shows:
11-21 13:02:29.692: W/IInputConnectionWrapper(11990): endBatchEdit on inactive InputConnection
11-21 13:02:31.264: W/IInputConnectionWrapper(11990): endBatchEdit on inactive InputConnection
11-21 13:02:43.506: W/System.err(11990): android.os.NetworkOnMainThreadException
11-21 13:02:43.506: W/System.err(11990): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
11-21 13:02:43.506: W/System.err(11990): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-21 13:02:43.506: W/System.err(11990): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-21 13:02:43.506: W/System.err(11990): at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-21 13:02:43.506: W/System.err(11990): at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
11-21 13:02:43.506: W/System.err(11990): at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
11-21 13:02:43.506: W/System.err(11990): at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
11-21 13:02:43.506: W/System.err(11990): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
11-21 13:02:43.516: W/System.err(11990): at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
11-21 13:02:43.516: W/System.err(11990): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
11-21 13:02:43.516: W/System.err(11990): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
11-21 13:02:43.516: W/System.err(11990): at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
11-21 13:02:43.516: W/System.err(11990): at com.gsmlocation.MainActivity.transform(MainActivity.java:114)
11-21 13:02:43.516: W/System.err(11990): at com.gsmlocation.MainActivity$1.onClick(MainActivity.java:80)
11-21 13:02:43.516: W/System.err(11990): at android.view.View.performClick(View.java:4445)
11-21 13:02:43.526: W/System.err(11990): at android.view.View$PerformClick.run(View.java:18446)
11-21 13:02:43.526: W/System.err(11990): at android.os.Handler.handleCallback(Handler.java:733)
11-21 13:02:43.526: W/System.err(11990): at android.os.Handler.dispatchMessage(Handler.java:95)
11-21 13:02:43.526: W/System.err(11990): at android.os.Looper.loop(Looper.java:136)
11-21 13:02:43.526: W/System.err(11990): at android.app.ActivityThread.main(ActivityThread.java:5146)
11-21 13:02:43.536: W/System.err(11990): at java.lang.reflect.Method.invokeNative(Native Method)
11-21 13:02:43.536: W/System.err(11990): at java.lang.reflect.Method.invoke(Method.java:515)
11-21 13:02:43.536: W/System.err(11990): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
11-21 13:02:43.536: W/System.err(11990): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
11-21 13:02:43.536: W/System.err(11990): at dalvik.system.NativeStart.main(Native Method)
And this is my code:
public Location transform(int cellID, int lac, int mcc, int mnc) {
try {
URL providerAddress = new URL("http://www.google.com/glm/mmap");
HttpURLConnection connection = (HttpURLConnection) providerAddress.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.connect();
OutputStream outputStream = connection.getOutputStream();
writePlainData(outputStream, cellID, lac, mcc, mnc);
InputStream inputStream = connection.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
dataInputStream.readShort();
dataInputStream.readByte();
int code = dataInputStream.readInt();
if (code == 0) {
double lat = dataInputStream.readInt() / 1000000D;
double lon = dataInputStream.readInt() / 1000000D;
coordinates.setLatitude(lat);
coordinates.setLongitude(lon);
return coordinates;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void writePlainData(OutputStream out, int cellID, int lac, int mcc, int mnc) throws IOException {
DataOutputStream dos = new DataOutputStream(out);
dos.writeShort(0x0E); // Fct code
dos.writeInt(0); // requesting 8 byte session
dos.writeInt(0);
dos.writeShort(0); // country code string
dos.writeShort(0); // client descriptor string
dos.writeShort(0); // version tag string
dos.writeByte(0x1B); // Fct code
dos.writeInt(0); // MNC?
dos.writeInt(0); // MCC?
dos.writeInt(3); // Radio Access Type (3=GSM, 5=UMTS)
dos.writeShort(0); // length of provider name
// provider name string
dos.writeInt(cellID); // CID
dos.writeInt(lac); // LAC
dos.writeInt(mnc); // MNC
dos.writeInt(mcc); // MCC
dos.writeInt(-1); // always -1
dos.writeInt(0); // rx level
dos.flush();
}
The line that the LocCat points is:
connection.connect();
Upvotes: 1
Views: 1030
Reputation: 3339
u r geating android.os.NetworkOnMainThreadException
so run your code in AsyncTask
class LoadingLocation extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params)
{
public Location transform(int cellID, int lac, int mcc, int mnc) {
try {
URL providerAddress = new URL("http://www.google.com/glm/mmap");
HttpURLConnection connection = (HttpURLConnection) providerAddress.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.connect();
OutputStream outputStream = connection.getOutputStream();
writePlainData(outputStream, cellID, lac, mcc, mnc);
InputStream inputStream = connection.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
dataInputStream.readShort();
dataInputStream.readByte();
int code = dataInputStream.readInt();
if (code == 0) {
double lat = dataInputStream.readInt() / 1000000D;
double lon = dataInputStream.readInt() / 1000000D;
coordinates.setLatitude(lat);
coordinates.setLongitude(lon);
return coordinates;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
return null;
}
}
Upvotes: 1