Skip to content

Commit 6a66786

Browse files
committed
fix: user @nativescript-community/perms
1 parent ee25c37 commit 6a66786

File tree

5 files changed

+21
-59
lines changed

5 files changed

+21
-59
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"dependencies": {
5858
"@nativescript-community/arraybuffers": "^1.1.1",
5959
"@nativescript-community/observable": "^2.0.11",
60+
"@nativescript-community/perms": "^2.3.0",
6061
"@nativescript-community/plugin-seed-tools": "file:tools",
6162
"make-error": "1.3.6",
6263
"p-queue": "~7.3.0"

packages/ble/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"dependencies": {
6969
"@nativescript-community/arraybuffers": "^1.1.1",
7070
"@nativescript-community/observable": "^2.0.11",
71+
"@nativescript-community/perms": "^2.3.0",
7172
"make-error": "1.3.6",
7273
"p-queue": "~7.3.0"
7374
}

packages/ble/platforms/android/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
<uses-permission android:name="android.permission.BLUETOOTH"/>
55
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
6+
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
7+
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
68

79
<!-- Required for Android 6+ when scanning for peripherals in the background -->
810
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

src/ble/index.android.ts

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
prepareArgs
2929
} from './index.common';
3030
import PQueue from 'p-queue';
31-
import { Device, Trace, Utils } from '@nativescript/core';
31+
import { Application, Device, Trace, Utils } from '@nativescript/core';
3232
import { arrayToNativeArray } from '@nativescript-community/arraybuffers';
3333
let _bluetoothInstance: Bluetooth;
3434
export 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);

src/ble/index.common.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Observable from '@nativescript-community/observable';
22
import { Trace } from '@nativescript/core';
33
import { BaseError } from 'make-error';
4+
import { check, request } from '@nativescript-community/perms';
45

56
export const BleTraceCategory = 'NativescriptBle';
67
export enum CLogTypes {
@@ -140,12 +141,13 @@ export abstract class BluetoothCommon extends Observable {
140141
public enableGPS(): Promise<void> {
141142
return Promise.resolve(); // we dont need to check for GPS in the bluetooth iOS module
142143
}
143-
requestLocationPermission() {
144-
return Promise.resolve(true);
144+
145+
public hasLocationPermission() {
146+
return check('location').then((r) => r[1]);
145147
}
146148

147-
hasLocationPermission() {
148-
return Promise.resolve(true);
149+
public async requestLocationPermission() {
150+
return request('location').then((r) => r[1]);
149151
}
150152

151153
/**

0 commit comments

Comments
 (0)