1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-02 06:52:55 +01:00

two-way comms test

This commit is contained in:
Gordon Williams 2022-03-24 11:31:53 +00:00
parent 9b5a7424d6
commit 8d2b2491db

View File

@ -9,6 +9,7 @@ import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient; import android.webkit.WebViewClient;
import android.widget.AdapterView; import android.widget.AdapterView;
@ -23,6 +24,9 @@ import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
@ -30,11 +34,13 @@ import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.banglejs.BangleJSDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.QHybridSupport; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.QHybridSupport;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class AppsManagementActivity extends AbstractGBActivity { public class AppsManagementActivity extends AbstractGBActivity {
private static final Logger LOG = LoggerFactory.getLogger(AppsManagementActivity.class);
private WebView webView; private WebView webView;
private GBDevice mGBDevice; private GBDevice mGBDevice;
private DeviceCoordinator mCoordinator; private DeviceCoordinator mCoordinator;
@ -80,12 +86,49 @@ public class AppsManagementActivity extends AbstractGBActivity {
} }
}; };
public class WebViewInterface {
Context mContext;
WebViewInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void bangleTx(String data) {
LOG.info("WebView RX: " + data);
bangleRx("Hello world");
}
}
// Called when data received from Bangle.js
public void bangleRx(String data) {
JSONArray s = new JSONArray();
s.put(data);
String ss = s.toString();
final String js = "bangleRx("+ss.substring(1, ss.length()-1)+");";
LOG.info("WebView TX cmd: " + js);
webView.post(new Runnable() {
@Override
public void run() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript(js, null);
}
else {
webView.loadUrl("javascript: "+js);
}
}
});
}
private void initViews() { private void initViews() {
//https://stackoverflow.com/questions/4325639/android-calling-javascript-functions-in-webview //https://stackoverflow.com/questions/4325639/android-calling-javascript-functions-in-webview
webView = findViewById(R.id.webview); webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient()); webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://banglejs.com/apps/"); webView.addJavascriptInterface(new WebViewInterface(this), "Android");
webView.loadUrl("https://www.pur3.co.uk/tmp/android.html");
webView.setWebViewClient(new WebViewClient(){ webView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String weburl){ public void onPageFinished(WebView view, String weburl){