Amiya Amiya - 11 days ago 4x
Android Question

Open the barcode scanner when opening the app

I'm trying to make my own barcode scanner app. It should open like Snapchat which basically just means that when i'm opening the app the barcode scanner should showup immediately and not through clicking a button.
I'm using Cordova and Ionics and I'm currently using the barcode scanner plugin from Phonegap which actually works fine BUT i can't make it work through just opening the app. The screen will just be white. It's working when i'm using a button but in the other case not.

I'm new to Cordova and JavaScript and it would help me a lot if some of you can help!

Thanks a lot :)

<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

<link rel="manifest" href="manifest.json">
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">

<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>

<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

<!-- your app's js -->
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/services.js"></script>

<script type="text/javascript">
function scan() {
cordova.plugins.barcodeScanner.scan(function(result) {
//success callback

}, function(error) {
//error callback


<body ng-app="starter">
<div class="bar bar-header">
<button class="button icon ion-navicon"></button>
<h1 class="title">Header Buttons</h1>
<button class="button">Edit</button>
<ion-content class="has-header">
<!--<button class="button button-positive" onclick="scan()">Click here to scan</button>-->
<script type="text/javascript">


i would guess that your scripts haven't fully loaded, therefore you need to run scan() once this is complete. A simple way to do this is to add an onLoad attribute to your body tag:

<body onload="scan()">