diff --git a/README.md b/README.md index 07b3f5e..f05d643 100644 --- a/README.md +++ b/README.md @@ -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) @@ -310,6 +313,41 @@ Stop the enabled notifications registered with `startEnabledNotifications`. --- +### isLocationEnabled() + +```typescript +isLocationEnabled() => Promise +``` + +Reports whether Location Services are enabled on this device. +Only available on **Android**. + +**Returns:** Promise<boolean> + +--- + +### openLocationSettings() + +```typescript +openLocationSettings() => Promise +``` + +Open Location settings. +Only available on **Android**. + +--- + +### openBluetoothSettings() + +```typescript +openBluetoothSettings() => Promise +``` + +Open Bluetooth settings. +Only available on **Android**. + +--- + ### setDisplayStrings(...) ```typescript diff --git a/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/BluetoothLe.kt b/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/BluetoothLe.kt index 386b06e..c511ffa 100644 --- a/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/BluetoothLe.kt +++ b/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/BluetoothLe.kt @@ -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", @@ -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( diff --git a/ios/Plugin/Plugin.m b/ios/Plugin/Plugin.m index 836bbc3..495f2bc 100644 --- a/ios/Plugin/Plugin.m +++ b/ios/Plugin/Plugin.m @@ -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); diff --git a/ios/Plugin/Plugin.swift b/ios/Plugin/Plugin.swift index 2c0eefa..e2fda3f 100644 --- a/ios/Plugin/Plugin.swift +++ b/ios/Plugin/Plugin.swift @@ -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"] { diff --git a/src/bleClient.ts b/src/bleClient.ts index 4be1f38..9b74503 100644 --- a/src/bleClient.ts +++ b/src/bleClient.ts @@ -42,6 +42,24 @@ export interface BleClientInterface { */ stopEnabledNotifications(): Promise; + /** + * Reports whether Location Services are enabled on this device. + * Only available on **Android**. + */ + isLocationEnabled(): Promise; + + /** + * Open Location settings. + * Only available on **Android**. + */ + openLocationSettings(): Promise; + + /** + * Open Bluetooth settings. + * Only available on **Android**. + */ + openBluetoothSettings(): Promise; + /** * Set the strings that are displayed in the `requestDevice` dialog. * @param displayStrings @@ -233,6 +251,26 @@ class BleClientClass implements BleClientInterface { }); } + async isLocationEnabled(): Promise { + const enabled = await this.queue(async () => { + const result = await BluetoothLe.isLocationEnabled(); + return result.value; + }); + return enabled; + } + + async openLocationSettings(): Promise { + await this.queue(async () => { + await BluetoothLe.openLocationSettings(); + }); + } + + async openBluetoothSettings(): Promise { + await this.queue(async () => { + await BluetoothLe.openBluetoothSettings(); + }); + } + async setDisplayStrings(displayStrings: DisplayStrings): Promise { await this.queue(async () => { await BluetoothLe.setDisplayStrings(displayStrings); diff --git a/src/definitions.ts b/src/definitions.ts index afce54b..9c1964c 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -204,6 +204,9 @@ export interface BluetoothLePlugin { isEnabled(): Promise; startEnabledNotifications(): Promise; stopEnabledNotifications(): Promise; + isLocationEnabled(): Promise; + openLocationSettings(): Promise; + openBluetoothSettings(): Promise; setDisplayStrings(displayStrings: DisplayStrings): Promise; requestDevice(options?: RequestBleDeviceOptions): Promise; requestLEScan(options?: RequestBleDeviceOptions): Promise; diff --git a/src/web.ts b/src/web.ts index 1e79928..27500f4 100644 --- a/src/web.ts +++ b/src/web.ts @@ -50,6 +50,18 @@ export class BluetoothLeWeb extends WebPlugin implements BluetoothLePlugin { // not available on web } + async isLocationEnabled(): Promise { + throw this.unavailable('isLocationEnabled is not available on web.'); + } + + async openLocationSettings(): Promise { + throw this.unavailable('openLocationSettings is not available on web.'); + } + + async openBluetoothSettings(): Promise { + throw this.unavailable('openBluetoothSettings is not available on web.'); + } + async setDisplayStrings(): Promise { // not available on web }