Skip to content

Commit

Permalink
feat(android): add new methods isLocationEnabled, openLocationSetting…
Browse files Browse the repository at this point in the history
…s and openBluetoothSettings
  • Loading branch information
pwespi committed Oct 2, 2021
1 parent 8b01adb commit 4c1cc60
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ Below is an index of all the methods available.
- [`isEnabled()`](#isenabled)
- [`startEnabledNotifications(...)`](#startenablednotifications)
- [`stopEnabledNotifications()`](#stopenablednotifications)
- [`isLocationEnabled()`](#islocationenabled)
- [`openLocationSettings()`](#openlocationsettings)
- [`openBluetoothSettings()`](#openbluetoothsettings)
- [`setDisplayStrings(...)`](#setdisplaystrings)
- [`requestDevice(...)`](#requestdevice)
- [`requestLEScan(...)`](#requestlescan)
Expand Down Expand Up @@ -310,6 +313,41 @@ Stop the enabled notifications registered with `startEnabledNotifications`.

---

### isLocationEnabled()

```typescript
isLocationEnabled() => Promise<boolean>
```

Reports whether Location Services are enabled on this device.
Only available on **Android**.

**Returns:** <code>Promise&lt;boolean&gt;</code>

---

### openLocationSettings()

```typescript
openLocationSettings() => Promise<void>
```

Open Location settings.
Only available on **Android**.

---

### openBluetoothSettings()

```typescript
openBluetoothSettings() => Promise<void>
```

Open Bluetooth settings.
Only available on **Android**.

---

### setDisplayStrings(...)

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import com.getcapacitor.annotation.Permission
import com.getcapacitor.annotation.PermissionCallback
import java.util.*
import kotlin.collections.ArrayList
import android.location.LocationManager
import android.provider.Settings.ACTION_BLUETOOTH_SETTINGS
import android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS
import androidx.core.location.LocationManagerCompat


@CapacitorPlugin(
name = "BluetoothLe",
Expand Down Expand Up @@ -146,6 +151,30 @@ class BluetoothLe : Plugin() {
call.resolve()
}

@PluginMethod
fun isLocationEnabled(call: PluginCall) {
val lm = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val enabled = LocationManagerCompat.isLocationEnabled(lm)
Log.d(TAG, "location $enabled")
val result = JSObject()
result.put("value", enabled)
call.resolve(result)
}

@PluginMethod
fun openLocationSettings(call: PluginCall) {
val intent = Intent(ACTION_LOCATION_SOURCE_SETTINGS)
getActivity().startActivity(intent);
call.resolve()
}

@PluginMethod
fun openBluetoothSettings(call: PluginCall) {
val intent = Intent(ACTION_BLUETOOTH_SETTINGS)
getActivity().startActivity(intent);
call.resolve()
}

@PluginMethod
fun setDisplayStrings(call: PluginCall) {
displayStrings = DisplayStrings(
Expand Down
3 changes: 3 additions & 0 deletions ios/Plugin/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
CAP_PLUGIN_METHOD(isEnabled, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(startEnabledNotifications, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(stopEnabledNotifications, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(isLocationEnabled, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(openLocationSettings, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(openBluetoothSettings, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(setDisplayStrings, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(requestDevice, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(requestLEScan, CAPPluginReturnPromise);
Expand Down
13 changes: 13 additions & 0 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ public class BluetoothLe: CAPPlugin {
deviceManager.unregisterStateReceiver()
call.resolve()
}

@objc func isLocationEnabled(_ call: CAPPluginCall) {
call.unavailable("isLocationEnabled is not available on iOS.")
}

@objc func openLocationSettings(_ call: CAPPluginCall) {
call.unavailable("openLocationSettings is not available on iOS.")
}

@objc func openBluetoothSettings(_ call: CAPPluginCall) {
call.unavailable("openBluetoothSettings is not available on iOS.")
}


@objc func setDisplayStrings(_ call: CAPPluginCall) {
for key in ["noDeviceFound", "availableDevices", "scanning", "cancel"] {
Expand Down
38 changes: 38 additions & 0 deletions src/bleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ export interface BleClientInterface {
*/
stopEnabledNotifications(): Promise<void>;

/**
* Reports whether Location Services are enabled on this device.
* Only available on **Android**.
*/
isLocationEnabled(): Promise<boolean>;

/**
* Open Location settings.
* Only available on **Android**.
*/
openLocationSettings(): Promise<void>;

/**
* Open Bluetooth settings.
* Only available on **Android**.
*/
openBluetoothSettings(): Promise<void>;

/**
* Set the strings that are displayed in the `requestDevice` dialog.
* @param displayStrings
Expand Down Expand Up @@ -233,6 +251,26 @@ class BleClientClass implements BleClientInterface {
});
}

async isLocationEnabled(): Promise<boolean> {
const enabled = await this.queue(async () => {
const result = await BluetoothLe.isLocationEnabled();
return result.value;
});
return enabled;
}

async openLocationSettings(): Promise<void> {
await this.queue(async () => {
await BluetoothLe.openLocationSettings();
});
}

async openBluetoothSettings(): Promise<void> {
await this.queue(async () => {
await BluetoothLe.openBluetoothSettings();
});
}

async setDisplayStrings(displayStrings: DisplayStrings): Promise<void> {
await this.queue(async () => {
await BluetoothLe.setDisplayStrings(displayStrings);
Expand Down
3 changes: 3 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ export interface BluetoothLePlugin {
isEnabled(): Promise<BooleanResult>;
startEnabledNotifications(): Promise<void>;
stopEnabledNotifications(): Promise<void>;
isLocationEnabled(): Promise<BooleanResult>;
openLocationSettings(): Promise<void>;
openBluetoothSettings(): Promise<void>;
setDisplayStrings(displayStrings: DisplayStrings): Promise<void>;
requestDevice(options?: RequestBleDeviceOptions): Promise<BleDevice>;
requestLEScan(options?: RequestBleDeviceOptions): Promise<void>;
Expand Down
12 changes: 12 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ export class BluetoothLeWeb extends WebPlugin implements BluetoothLePlugin {
// not available on web
}

async isLocationEnabled(): Promise<BooleanResult> {
throw this.unavailable('isLocationEnabled is not available on web.');
}

async openLocationSettings(): Promise<void> {
throw this.unavailable('openLocationSettings is not available on web.');
}

async openBluetoothSettings(): Promise<void> {
throw this.unavailable('openBluetoothSettings is not available on web.');
}

async setDisplayStrings(): Promise<void> {
// not available on web
}
Expand Down

0 comments on commit 4c1cc60

Please sign in to comment.