@@ -28,7 +28,7 @@ import {
2828 prepareArgs
2929} from './index.common' ;
3030import PQueue from 'p-queue' ;
31- import { Device , Trace , Utils } from '@nativescript/core' ;
31+ import { Application , Device , Trace , Utils } from '@nativescript/core' ;
3232import { arrayToNativeArray } from '@nativescript-community/arraybuffers' ;
3333let _bluetoothInstance : Bluetooth ;
3434export function getBluetoothInstance ( ) {
@@ -1213,54 +1213,8 @@ export class Bluetooth extends BluetoothCommon {
12131213 this . unregisterBroadcast ( ) ;
12141214 }
12151215
1216- public locationPermissionGranted ( ) {
1217- let hasPermission = sdkVersion < MARSHMALLOW ;
1218- if ( ! hasPermission ) {
1219- const ctx = Utils . android . getApplicationContext ( ) ;
1220- // CLog(CLogTypes.info, 'app context', ctx);
1221- const neededPermission = sdkVersion < ANDROID10 ? android . Manifest . permission . ACCESS_COARSE_LOCATION : android . Manifest . permission . ACCESS_FINE_LOCATION ;
1222-
1223- hasPermission = android . content . pm . PackageManager . PERMISSION_GRANTED === androidx . core . content . ContextCompat . checkSelfPermission ( ctx , neededPermission ) ;
1224- if ( Trace . isEnabled ( ) ) {
1225- CLog ( CLogTypes . info , `coarseLocationPermissionGranted ---- ${ neededPermission } permission granted?` , hasPermission ) ;
1226- }
1227- }
1228- return hasPermission ;
1229- }
1230-
1231- public hasLocationPermission ( ) {
1232- return Promise . resolve ( this . locationPermissionGranted ( ) ) ;
1233- }
1234-
1235- public requestLocationPermission ( callback ?: ( ) => void ) : Promise < boolean > {
1236- return new Promise ( ( resolve , reject ) => {
1237- let permissionCb = ( args : AndroidActivityRequestPermissionsEventData ) => {
1238- if ( args . requestCode === ACCESS_LOCATION_PERMISSION_REQUEST_CODE ) {
1239- andApp . off ( AndroidApplication . activityRequestPermissionsEvent , permissionCb ) ;
1240- permissionCb = null ;
1241- for ( let i = 0 ; i < args . permissions . length ; i ++ ) {
1242- if ( args . grantResults [ i ] === android . content . pm . PackageManager . PERMISSION_DENIED ) {
1243- reject ( 'Permission denied' ) ;
1244- return ;
1245- }
1246- }
1247-
1248- if ( callback ) {
1249- callback ( ) ;
1250- }
1251- resolve ( true ) ;
1252- }
1253- } ;
1254-
1255- // grab the permission dialog result
1256- andApp . on ( AndroidApplication . activityRequestPermissionsEvent , permissionCb ) ;
1257- const neededPermission = sdkVersion < ANDROID10 ? android . Manifest . permission . ACCESS_COARSE_LOCATION : android . Manifest . permission . ACCESS_FINE_LOCATION ;
1258- // invoke the permission dialog
1259- androidx . core . app . ActivityCompat . requestPermissions ( this . _getActivity ( ) , [ neededPermission ] , ACCESS_LOCATION_PERMISSION_REQUEST_CODE ) ;
1260- } ) ;
1261- }
12621216 getAndroidLocationManager ( ) : android . location . LocationManager {
1263- return ( andApp . context as android . content . Context ) . getSystemService ( android . content . Context . LOCATION_SERVICE ) ;
1217+ return ( Utils . android . getApplicationContext ( ) as android . content . Context ) . getSystemService ( android . content . Context . LOCATION_SERVICE ) ;
12641218 }
12651219 public isGPSEnabled ( ) {
12661220 if ( ! this . hasLocationPermission ( ) ) {
@@ -1470,7 +1424,7 @@ export class Bluetooth extends BluetoothCommon {
14701424 @bluetoothEnabled
14711425 public startScanning ( args : StartScanningOptions ) {
14721426 const methodName = 'startScanning' ;
1473- return new Promise < void > ( ( resolve , reject ) => {
1427+ return new Promise < void > ( async ( resolve , reject ) => {
14741428 try {
14751429 const onPermissionGranted = ( ) => {
14761430 // for (const key in this.connections) {
@@ -1588,14 +1542,16 @@ export class Bluetooth extends BluetoothCommon {
15881542 }
15891543 } ;
15901544
1591- if ( args . skipPermissionCheck !== true && ! this . locationPermissionGranted ( ) ) {
1592- if ( Trace . isEnabled ( ) ) {
1593- CLog ( CLogTypes . info , methodName , '---- Coarse Location Permission not granted on Android device, will request permission.' ) ;
1545+ if ( args . skipPermissionCheck !== true ) {
1546+ const hasPermission = await this . hasLocationPermission ( ) ;
1547+ if ( ! hasPermission ) {
1548+ if ( Trace . isEnabled ( ) ) {
1549+ CLog ( CLogTypes . info , methodName , '---- Coarse Location Permission not granted on Android device, will request permission.' ) ;
1550+ }
1551+ await this . requestLocationPermission ( ) ;
15941552 }
1595- this . requestLocationPermission ( onPermissionGranted ) ;
1596- } else {
1597- onPermissionGranted ( ) ;
15981553 }
1554+ onPermissionGranted ( ) ;
15991555 } catch ( ex ) {
16001556 if ( Trace . isEnabled ( ) ) {
16011557 CLog ( CLogTypes . error , methodName , '---- error:' , ex ) ;
0 commit comments