I have written a VERY simple Ionic package (as part of a test) to fire up the barcode scanner
I have installed cordova-plugin-barcodescanner and am using the following
$scope.scanBarcode = function() {
$cordovaBarcodeScanner
.scan()
.then(function(barcodeData) {
alert(barcodeData);
}, function(error) {
alert(error);
});
}
I then have a very simple
<button ng-click="scanBarcode()">Scan</button>
I have compiled the app, generated my apk, installed on a device, confirmed that the application has permissions enabled for camera (and storage).
When I click the scan button, an error is firing which is
write settings: false
I am at a loss where to start as there is no specific error docs for barcodescanner.
Just to advise I have been able to use the cordova-plugin-camera ok with ionic creator after compiling the app.
Actually I also encountered this issue, it will probably happen for Android 6.0 and above.
Inside the plugin, these following codes are the one triggering it.
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP + 1) {
Class systemClass = Settings.System.class;
try {
Method canWriteMethod = systemClass.getDeclaredMethod("canWrite", Context.class);
boolean retVal = (Boolean) canWriteMethod.invoke(null, this.cordova.getActivity());
Log.d(LOG_TAG, "Can Write Settings: " + retVal);
if (!retVal && !action.equals("requestWriteSettings") && !action.equals("getWriteSettings")) {
//can't write Settings
this.callbackContext.error("write settings: false");
return false;
}
this.writeSettings = retVal;
} catch (Exception ignored) {
Log.e(LOG_TAG, "Could not perform permission check");
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION));
}
}
So I think we need to inform the plugin creators or submit a fix for this one.
Temporarily what you can do is either you by pass it, by commenting the whole block I shared. Seems like it's working from my side, haven't checked if this is actually used somewhere in the codes. Why it must be called.
Another proper way, which is to enhance the code by implementing the following:
A. Include in the AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
B. The allow or ask the user to allow the write settings, by calling the intent needed:
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + this.cordova.getActivity().getPackageName()));
this.cordova.getActivity().startActivity(intent);
UPDATE
C. This is suggested by MoleDesign, as mentioned in one of the answer below. Another solution which is a workaround is to lower the sdk version, in the project.properties change it it to:
target=android-23