I am using the Socket Mobile Capture SDK, which provides an easy method for receiving data from bluetooth connected barcode scanners assuming you can add a method to an android Activity class.
Below is the MainActivity code I have created. The onData method correctly fires each time an external bluetooth scanner is used. I would like to forward this information to the inAppBrowser. Is this possible or is there a better way to do this?
public class MainActivity extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
Capture.builder(getApplicationContext())
.enableLogging(BuildConfig.DEBUG)
.build();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onData(DataEvent event) {
System.out.println("onData fired from MainActivity");
DeviceClient device = event.getDevice();
String data = event.getData().getString();
System.out.println(data);
}
}
Here is my code from index.js which sets up the InAppBrowser:
var app = {
// Application Constructor
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
onDeviceReady: function() {
this.receivedEvent('deviceready');
alert('device ready');
var url = environment == 'Development' ? developmentUrl : productionUrl;
inAppBrowserRef = cordova.InAppBrowser.open(url, '_blank', 'location=no'); //open the in app browser with no location bar
inAppBrowserRef.addEventListener( "loadstop", function() { //Fired when browser is finished loading
alert('inappbrowser loaded');
});
},
// Once the InAppBrowser finishes loading
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
I was able to get this to work by using loadUrl("javascript:onData(\""+data+"\")"). Below are the changes I made:
Modify MainActivity.java onData method to:
Add this function to my index.js file: