Reputation: 23
I'm working with a Flutter app, using a Dart package called flutter_blue to connect to a Bluetooth device. The Bluetooth device connects successfully, but only if the user has Bluetooth and Location enabled on their device. I'm trying to find a way to detect whether Location and Bluetooth is enabled on the device before trying to connect so that I can prompt the user to turn them on. Currently, I'm also using a Dart package that works with native Android intents to bring up the settings menus for each (also working). I just need a way to check if they're enabled so that I know whether to show the settings menus for users to enable them.
Upvotes: 2
Views: 9877
Reputation: 929
I hope this still would be useful
You can check if bluetooth is enabled with:
Future<bool> _checkDeviceBluetoothIsOn() async {
return await flutterBlue.isOn;
}
and check if location is enabled using PermissionHandler package with:
Future<bool> _checkDeviceLocationIsOn() async {
return await Permission.locationWhenInUse.serviceStatus.isEnabled;
}
Upvotes: 7
Reputation: 345
I'm working on the same case as you actually, and the way i check for it is to use the following:
For checking bluetooth activation, you can listen to the state stream provided by the flutter blue library, you can access it like the following :
FlutterBlue.instance.state
For location, i did not found a "native fluttery way" to do it, so i'm using the geolocator package which gives you the ability to check if the locaiton is active.
It provides you with a future that resolve to a boolean saying true for active, and false for disabled.
Geolocator().isLocationServiceEnabled()
Upvotes: 1