I'm trying to test barcode scanner in a very simple phone gap app. I'm trying to test the app in PhoneGap Developer App. I've been following a bunch of different solutions I've found while googling:
http://www.raymondcamden.com/2014/3/5/Barcode-Scanner-sample-and-new-repo-for-Cordova-examples
https://stackoverflow.com/a/19591607/418549
Here's my HTML:
<div class="app">
<h1>PhoneGap</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
<p></p>
<p></p>
<button id="startScan">Start Scan</button>
<div id="results"></div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/barcodescanner.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
Here's my initialize code:
var resultDiv;
var app =
{
// Application Constructor
initialize: function ()
{
this.bindEvents();
},
bindEvents: function ()
{
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function ()
{
app.receivedEvent('deviceready');
document.querySelector("#startScan").addEventListener("touchend", startScan, false);
resultDiv = document.querySelector("#results");
},
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);
}
};
function startScan()
{
try
{
var scanner = cordova.require("com.phonegap.plugins.barcodescanner.BarcodeScanner");
//var scanner = cordova.require("cordova/plugin/BarcodeScanner");
scanner.scan(
function (result)
{
var s = "Result: " + result.text + "<br/>" +
"Format: " + result.format + "<br/>" +
"Cancelled: " + result.cancelled;
resultDiv.innerHTML = s;
},
function (error)
{
alert("Scanning failed: " + error);
}
);
}
catch (e)
{
console.log('-------------------------- ERROR 1');
console.log(e);
}
}
When running this code in PhoneGap Build App, I always get 'module com.phonegap.plugins.barcodescanner.BarcodeScanner not found' error message. I think the problem is the Java file is not getting loaded.
PhoneGap Developer App is pointing to the root www folder. Should it be pointing to the www folder for Android instead?
Thanks
Tom