Я использую Socket Mobile Capture SDK, который обеспечивает простой метод получения данных от подключенных по Bluetooth сканеров штрих-кода, при условии, что вы можете добавить метод в класс Activity для Android.
Ниже приведен код MainActivity, который я создал. Метод onData корректно срабатывает каждый раз, когда используется внешний Bluetooth-сканер. Я хотел бы направить эту информацию в inAppBrowser. Это возможно или есть лучший способ сделать это?
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);
}
}
Вот мой код из index.js, который устанавливает 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();