-
-
Save peterdemartini/b48e7798a181c389b592 to your computer and use it in GitHub Desktop.
Get messages in Android from Meshblu: https://github.com/octoblu/gateblu-android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- https://raw.githubusercontent.com/octoblu/gateblu-android/master/app/src/main/java/com/octoblu/gateblu/WebViewDevice.java --> | |
<html> | |
<script src="https://cdn.octoblu.com/js/meshblu/latest/meshblu.bundle.js"></script> | |
<script> | |
var meshbluConn = meshblu.createConnection(meshbluJSON); | |
meshbluConn.once('ready', function(){ | |
console.log('Connected to meshblu'); | |
meshbluConn.on('config', function(device){ | |
console.log('on config', device); | |
ConnectorEventListener.on('config', JSON.stringify(device)); | |
}); | |
}); | |
meshbluConn.on('notReady', function(){ | |
console.log('notReady'); | |
}); | |
</script> | |
<body> | |
<h1>This shouldn't show.</h1> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/octoblu/gateblu-android/blob/master/app/src/main/java/com/octoblu/gateblu/WebViewDevice.java | |
package com.octoblu.gateblu; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.support.annotation.Nullable; | |
import android.util.Log; | |
import android.webkit.JavascriptInterface; | |
import android.webkit.WebSettings; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
import org.json.JSONObject; | |
public class WebViewDevice { | |
private static final String TAG = "WebViewDevice"; | |
public static final String CONFIG = "config"; | |
private final String uuid, token; | |
private final Context context; | |
private WebView webView = null; | |
public WebViewDevice(String uuid, String token, Context context) { | |
this.uuid = uuid; | |
this.token = token; | |
this.context = context; | |
this.webView = null; | |
} | |
public String getUuid() { | |
return uuid; | |
} | |
public void start() { | |
Log.i(TAG, "start: " + uuid); | |
stop(); | |
this.webView = generateWebView(); | |
} | |
public void stop(){ | |
if(this.webView == null){ | |
return; | |
} | |
this.webView.destroy(); | |
} | |
private WebView generateWebView() { | |
WebView webView = new WebView(context); | |
WebSettings settings = webView.getSettings(); | |
settings.setJavaScriptEnabled(true); | |
settings.setAllowFileAccess(true); | |
settings.setAllowFileAccessFromFileURLs(true); | |
webView.addJavascriptInterface(new ConnectorEventEmitter() { | |
@Override | |
@JavascriptInterface | |
public void on(String event, String jsonData) { | |
Log.i(TAG, "on(" + event + "): " + jsonData); | |
if (event.equals("config")) { | |
Log.i(TAG, "Received Config!"); | |
} | |
} | |
}, "ConnectorEventListener"); | |
webView.clearCache(true); | |
webView.loadUrl("file:///android_asset/www/asset.html"); | |
webView.evaluateJavascript("window.meshbluJSON = {uuid: \"" + uuid + "\", token: \"" + token + "\"};", new Util.IgnoreReturnValue()); | |
return webView; | |
} | |
public interface ConnectorEventEmitter { | |
@JavascriptInterface | |
void on(String event, String jsonData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment