Skip to content

(android) startScanning unhandled rejection if user denies location permissions #265

@delaneyb

Description

@delaneyb

requestLocationPermission is passed a callback which is never invoked if the user denies permissions. If the user calls startScanning and this happens, the promise returned by startScanning never resolves as the callback is never invoked and no error is propagated to the user application.

startScanning needs to account for requestLocationPermission potentially rejecting and rethrow this error to the caller.

ble/src/ble/index.android.ts

Lines 1235 to 1261 in 9820235

public requestLocationPermission(callback?: () => void): Promise<boolean> {
return new Promise((resolve, reject) => {
let permissionCb = (args: AndroidActivityRequestPermissionsEventData) => {
if (args.requestCode === ACCESS_LOCATION_PERMISSION_REQUEST_CODE) {
andApp.off(AndroidApplication.activityRequestPermissionsEvent, permissionCb);
permissionCb = null;
for (let i = 0; i < args.permissions.length; i++) {
if (args.grantResults[i] === android.content.pm.PackageManager.PERMISSION_DENIED) {
reject('Permission denied');
return;
}
}
if (callback) {
callback();
}
resolve(true);
}
};
// grab the permission dialog result
andApp.on(AndroidApplication.activityRequestPermissionsEvent, permissionCb);
const neededPermission = sdkVersion < ANDROID10 ? android.Manifest.permission.ACCESS_COARSE_LOCATION : android.Manifest.permission.ACCESS_FINE_LOCATION;
// invoke the permission dialog
androidx.core.app.ActivityCompat.requestPermissions(this._getActivity(), [neededPermission], ACCESS_LOCATION_PERMISSION_REQUEST_CODE);
});
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions