Error scanning barcode with Ionic/cordova - android

I have an Ionic project and I installed this plugin to scan qrcodes:
cordova plugin add https://github.com/wildabeast/BarcodeScanner.git
But when I run the app on a samsung s5 device with Android 6.0 I get a camera error:
"Sorry, the Android camera encountered a problem. You may need to restart the device"
Any ideas of what causes this issue?
I have tested with:
ionic run android -l -c
ionic run android
This is my code executed when I press a button:
$scope.scanBarcode = function() {
$ionicPlatform.ready(function() {
$cordovaBarcodeScanner.scan().then(function(imageData) {
console.log("Barcode text -> " + imageData.text);
console.log("Barcode Format -> " + imageData.format);
console.log("Cancelled -> " + imageData.cancelled); // prints: cancelled
}, function(error) {
console.log("An error happened -> " + error);
});
});
};

For this issue you can try either of below solutions:
1) It is permission issue with android 6. So to fix the issue configure platform to android 5.0.
OR if you are using MI note 4 you can try below additional steps:
There is another permission required in MIUI secruity system and after enabling it, camera started working.
Following are the steps you can follow to update the setting:
1) Go to Security.
2) Click on “Permissions” and again select “Permissions”
3) Now from the list, click on “Camera”.
4) It will list all the apps installed. Look for your app and enable the permission.
Reff: http://sforsuresh.in/cordova-sorry-android-camera-encountered-problem/

This is what solved my problema, in the config.xml file in the "android" platform section I put:
<platform name="android">
<preference name="android-targetSdkVersion" value="22"/>
</platform>

Related

Ionic 3 native plugin of sms not working on my device

I am using Ionic 3 and my device is 4.4.2 android.
Setup:
ionic cordova plugin add cordova-sms-plugin
npm install --save #ionic-native/sms#4
I have add to app.module
import { SMS } from '#ionic-native/sms';
providers: [..., SMS]
home.ts
...
import { SMS } from '#ionic-native/sms';
...
constructor(..., private sms: SMS){}
sendSMS(){
this.sms.send('0030699999999', 'sos')
}
home.html
<button ion-button block (click)="sendSMS()" margin-top color="dark">SMS</button>
I install the apk to my phone with below command:
ionic cordova run android
When I am pressing the button, my app breaks and exit, without sending a message.
I tried to remove platform android and added again, but nothing.
Allow from app settings on your phone
I had to put a send_sms permission in manifest file (android).
Check in my phone v 4.4.2.
I didn't check it in iPhone.

Cordova Media plugin fails with mp3 internet audio streaming on Android 6 (Marshmallow)

My internet radio station player app failed with the upgrade to Android 6 (Marshmallow). To test I copied the example code (below) from the docs for the Media plugin and built a tiny test app on latest Cordova (6.1.1), Android platform (5.1.1), Browser platform (4.1.0) and Media plugin (2.3.0). I also added necessary whitelist statements (below). On the browser platform it plays both internet files and internet streams. On the Android platform it fails to start mp3 internet streams. The console log shows that a stream is terminated within 1 second and the OnSuccess callback is called. No error messages, just logs "playAudio():Audio Success" and terminates.
Player code is
var src = "http://audio.wgbh.org:8104";
my_media = new Media(src,
// success callback
function () {
console.log("playAudio():Audio Success");
},
// error callback
function (err) {
console.log("playAudio():Audio Error: " + err);
}
config.xml whitelist is (yes unsafe, I'll fix that later)
<plugin name="cordova-plugin-whitelist" source="npm" spec="1" />
<allow-navigation href="*" />
<allow-intent href="*" />
<access origin="*" launch-external="yes" /> <!-- Required for iOS9 -->
A previous Stack Overflow question from Dec 2015 had the same problem "cordova media plugin stopped working on Android 6". It suggests a java patch to the AudioPlayer. I made that change and rebuilt my test app. But that didn't change anything. Is a Cordova build sufficient to make that change?
BTW, testing on a real phone plugged into USB, seeing log via Chrome dev tools.
What am I missing? Or is this a bug in the Media plugin java code or in the Android platform?
To answer my own question, the patch provided previously in cordova media plugin stopped working on Android 6 does answer my problem.
My mistake was applying it in the wrong place. Changing code directly in platforms/android/src/org/apache/cordova/media/AudioPlayer.java and rebuilding the app does work. (I was patching the plugin code instead). Comment out line 354, so it becomes,
//this.seekToPlaying(this.seekOnPrepared);
That does the trick.

Cordova Hybrid App fails, Legacy Android Build Works

I've written a small application to test the working of cordova app on XDK.
The source code is same as this question
However, I am attaching the code here :
(function()
{
"use strict";
/*
hook up event handlers
*/
function register_event_handlers()
{
/* button Login */
$(document).on("click", ".uib_w_9", function(evt)
{
//intel.xdk.notification.showBusyIndicator();
/*$.post("http://url/test.php", {test:'1'},
function(res){
alert(res);
}
);*/
$.ajax({
beforeSend: function(){
intel.xdk.notification.showBusyIndicator();
},
type: "GET",
url: 'http://url/test.php',
data:{test:'1'},
success: function(res){
alert(res);
intel.xdk.notification.hideBusyIndicator();
},
error:function(res){
alert(res);
intel.xdk.notification.hideBusyIndicator();
},
dataType: 'text'
});
});
}
document.addEventListener("app.Ready", register_event_handlers, false);
})();
This is the JS file of the application.
The screenshot of the app in XDK emulator :
Now When I do a build of the app using XDK cloud, I get a .apk file.
On uploading the file to GenyMotion Emulator, screen below :
The click doesn't do anything, nor does the showBusyIndicator() show, nor the alert appear.
What am I missing and what is going wrong ?
I've plans to use this ide for a proper project and hence the testing, some help will be appreciated.
UPDATE
I did a Cordova Hybrid App build initially and that caused the problem.
When I did a legacy android build, then the problem was solved.
So what do I need to do to get it to work with the Cordova Build ?
Build Page :
There is a known issue with AJAX when Intel XDK builds apk with Cordova CLI version 4.1.2. on Android version > 4.4
There is a workaround, go to Project Settings -> Build Settings -> Android, change Cordova CLI version from 4.1.2 to 3.5 and AJAX should work.

device is not defined

Using an existing sencha project and PhoneGap Build, I have the problem that window.device is not defined if I query for it in the microloader/testing.js code.
Ext.blink = function(options) {
...
Ext.microloaded = true;
// Start my microloader enhancement
isPhonegap = typeof window.device != "undefined";
window.alert("isPhonegap: " + isPhonegap);
The config.xml contains the plugin:
<gap:plugin name="org.apache.cordova.device" />
Sencha is uploading everything to PhoneGap Build for me, it is built there, and then I download and adb install -r the resulting apk on my Android 4.0.4 Smartphone.
Result when starting the app, is the alert: isPhonegap: false.
What is the problem here?
Guessing a bit here, but could it be that you're calling that code before deviceReady has fired?
See http://docs.phonegap.com/en/4.0.0/cordova_events_events.md.html#deviceready for details.

Generated links don't open in browser - Phonegap build

I am trying to force the links in my app to open in external browser. I am working with jQuery mobile & Phonegap build.
My app has a page with generated contact details. Among the details there is an link to the person's website:
row+='<div class="accWWW"> '+ data[i].website +'</div>';
I have these 2 functions defined in my code.
//function which forces open in browser
function openBrowser(url){
if(device.platform === 'Android') {
navigator.app.loadUrl(url, {openExternal:true});
} else {
window.open(url, '_system');
}
}
//opens generated URL's
function openGenerated(obj){
var url = obj.getAttribute("href");
console.log(url);
openBrowser(url);
return false;
}
I also have a simple <div class="accWWW"> Google </div> set in my index.html
So... this link to Google opens perfectly in iOS (haven't yet tested on Android because iOS was my biggest problem), but the generated link wouldn't do anything if I hadn't added the openGenerated function. Now there is an action, but it opens inside the app.
Do you have any idea why that is?
Also when checking on the laptop I get these errors in the console( link is my development url):
Uncaught ReferenceError: device is not defined *link*:277 // makes sense probably, because I am not using a mobile device
Uncaught SecurityError: Blocked a frame with origin "http://www.test.com" from accessing a frame with origin "*link*". Protocols, domains, and ports must match.// what is this?
I was thinking maybe it's an access issue, but I added <access origin="*" browserOnly="true" /> in my config.xml, so there shouldn't be a problem with this.
LATER EDIT: On Android the Google link doesn't work at all and the generated one opens inside the app... Apparently device.platform is always null, but the device plugin is added in xml as I can see in phonegap build this list of plugins:
Installed Plugins
Apps will be built with the latest version of a plugin unless you lock the version in your config.xml (see below). -- Plugin installed as a dependency of another plugin
Third Party VERSION LATEST VERSION
com.phonegap.plugin.statusbar 1.1.0 1.1.0
PhoneGap Core
org.apache.cordova.device 0.2.8 0.2.8
org.apache.cordova.inappbrowser 0.3.3 0.3.3
LATER EDIT:
Device.platform doesn't seem to be working, so I modified my function, adding Ved's suggestion, to:
function openBrowser(url){
if(navigator.app)
navigator.app.loadUrl(url, {openExternal:true});
else {
var ref = window.open(url, '_system', 'location=no');
ref.addEventListener('loadstart', function(event) {
});
ref.addEventListener('loadstop', function(event) {
console.log(event.type + ' - ' + event.url);
});
ref.addEventListener('exit', function(event) {
});
}
}
on iOS still opens inside the app. For Android I think it's working fine.
I fixed my issue.
I was adding cordova.js in my html. Apparently this broke things.

Categories

Resources