Open
Description
Environement:
Phone that has disabled mobile data and wifi, but has a bluetooth tetering enabled with another device that runs a flask server.
Bug description:
The GET request is never executed or it fails out (I haven't investigated much)
My local fix:
Open socket and let Android handle the connection:
BluetoothDirectClient class that has the sendRequest function
package com.mendhak.gpslogger.senders.customurl;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class BluetoothDirectClient {
public static void sendRequest(
String url
) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Handler handler = new Handler(Looper.getMainLooper());
executor.execute(() -> {
URI uri = null;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
// Extract my info
String clientIp = uri.getHost();
int port = uri.getPort();
String path = uri.getRawPath();
if (uri.getRawQuery() != null) {
path += "?" + uri.getRawQuery();
}
// Write on socket, let Android handle connection
try (Socket socket = new Socket(clientIp, port)) {
OutputStream outputStream = socket.getOutputStream();
String httpRequest = String.format(
"GET %s HTTP/1.1\r\nHost: %s\r\n\r\n",
path,
clientIp
);
outputStream.write(httpRequest.getBytes());
outputStream.flush();
// Read response
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line).append("\n");
}
Log.d("Bluetooth", "Response:\n" + response);
} catch (IOException e) {
Log.e("Bluetooth", "Error: " + e.getMessage());
}
handler.post(() -> {
//UI Thread work here
});
});
}
}
Then, I called it in GpsLoggerService at onLocationChanged, as i couldn't reach the doWork function inside the CustomUrlWorker
String url = "http://IP:PORT/PATH?lat=%LAT&long=%LON&alt=%ALT&time=%TIME";
url = url.replace("%LAT", String.valueOf(loc.getLatitude()));
url = url.replace("%LON", String.valueOf(loc.getLongitude()));
url = url.replace("%ALT", String.valueOf(loc.getAltitude()));
url = url.replace("%TIME", String.valueOf(Strings.getUrlEncodedString(Strings.getIsoDateTime(new Date(loc.getTime())))));
BluetoothDirectClient.sendRequest(
url
);
Mind you, I am in no way an Android expert, and I have not tested much my solution, just wanted to expose a limitation :)
Metadata
Assignees
Labels
No labels
Activity