Skip to content

(Android) enableGPS does not attempt to enable GPS due to incorrect isGPSEnabled check #266

Open
@delaneyb

Description

@delaneyb

enableGPS uses the check

if (!this.isGPSEnabled())

to determine whether we need to prompt the user to enable location services on the device. isGPSEnabled however returns a Promise, meaning the non-awaited returned value is always truthy, and therefore enableGPS will never actually actually prompt the user to enable location services, even if the promise returned by isGPSEnabled resolves to false

ble/src/ble/index.android.ts

Lines 1277 to 1300 in 9820235

public enableGPS(): Promise<void> {
if (Trace.isEnabled()) {
CLog(CLogTypes.info, 'Bluetooth.enableGPS');
}
return new Promise((resolve, reject) => {
const activity = andApp.foregroundActivity || andApp.startActivity;
if (!this.isGPSEnabled()) {
const onActivityResultHandler = (data: AndroidActivityResultEventData) => {
andApp.off(AndroidApplication.activityResultEvent, onActivityResultHandler);
if (data.requestCode === 0) {
if (this.isGPSEnabled()) {
resolve();
} else {
reject('GPS not enabled');
}
}
};
andApp.on(AndroidApplication.activityResultEvent, onActivityResultHandler);
activity.startActivityForResult(new android.content.Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
} else {
resolve();
}
});
}

A simple solution would be to make the promise constructor callback async and adjust the condition to:

if (!(await this.isGPSEnabled()))

The check after receiving the request result from the system on line 1287 also needs to be corrected

if (this.isGPSEnabled()) {

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