@dewakoding/capacitor-mock-location
v1.1.1
Published
DewaKoding Capacitor plugin to detect mock/fake GPS locations on Android devices
Downloads
359
Maintainers
Readme
@dewakoding/capacitor-mock-location
Capacitor plugin to detect mock or fake GPS locations on Android devices.
Overview
This plugin helps you detect when users are using fake GPS applications to spoof their location. It's useful for apps that require accurate location data, such as attendance systems, location-based services, or tracking applications.
Installation
npm install @dewakoding/capacitor-mock-locationor with yarn:
yarn add @dewakoding/capacitor-mock-locationThen sync Capacitor:
npx cap syncUsage
import { MockLocation } from '@dewakoding/capacitor-mock-location'
const { isMock } = await MockLocation.checkMockLocation()
if (isMock) {
console.log('Mock location detected')
// Handle mock location case
} else {
console.log('Location is valid')
// Proceed with location-based operations
}Example: Check before check-in
import { MockLocation } from '@dewakoding/capacitor-mock-location'
import { Geolocation } from '@capacitor/geolocation'
async function handleCheckIn() {
// Check for mock location first
const { isMock } = await MockLocation.checkMockLocation()
if (isMock) {
alert('Fake GPS detected. Please disable fake GPS apps.')
return
}
// Get actual location
const position = await Geolocation.getCurrentPosition()
// Continue with check-in process
}API
checkMockLocation()
Checks if the device is currently using a mock or fake GPS location.
Returns: Promise<{ isMock: boolean }>
isMock:trueif mock location is detected,falseotherwise
How It Works
The plugin uses multiple detection methods based on standard Android APIs:
- Checks if the current location is from a mock provider using
isFromMockProvider()(Android 6.0+) - Scans for providers containing "mock" or "test" keywords in LocationManager
- Checks if common fake GPS applications are installed on the device
Note: This implementation uses publicly available Android APIs. The code structure and implementation approach are original work by DewaKoding.
Android Setup
Permissions
Make sure your AndroidManifest.xml includes location permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />Auto-Registration
The plugin uses @CapacitorPlugin annotation, so it will be auto-registered by Capacitor. No manual registration in MainActivity is required.
Build & Run
npm run cap:sync
npm run cap:run:androidPlatform Support
- Android: Fully supported
- iOS: Not yet implemented (returns
false) - Web: Not applicable (returns
false)
Testing
Test with Fake GPS App
- Install a fake GPS app (e.g., "Fake GPS Location" from Play Store)
- Enable mock location in the fake GPS app
- Set the desired location
- Open your app
- Call
checkMockLocation() - Should return
{ isMock: true }
Test with Real Location
- Make sure no fake GPS apps are active
- Open your app
- Call
checkMockLocation() - Should return
{ isMock: false }
Troubleshooting
Plugin not detected
- Make sure you've run
npx cap sync - Rebuild the Android app
- Check logcat for errors
- Verify the package name in the Java file is correct
Build errors
- Make sure the Java file uses the correct package:
com.dewakoding.capacitor.mocklocation - Check if there are duplicate plugins with the same name
- Make sure Capacitor version is compatible (^8.0.0)
Mock location not detected
- Some sophisticated fake GPS apps may not be detected
- Make sure the fake GPS app is active and being used
- Try restarting the app after enabling fake GPS
Limitations
Detection is not 100% accurate. Some sophisticated fake GPS applications may not be detected. The plugin checks for common fake GPS apps, but new applications may not be in the detection list.
Development
Build the plugin:
npm run buildWatch mode:
npm run watchLicense
MIT License
