-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBluetoothHelperApple.cs
112 lines (96 loc) · 4.01 KB
/
BluetoothHelperApple.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#if IOS
using CoreBluetooth;
using CoreLocation;
using Foundation;
using Microsoft.Maui.ApplicationModel;
using System.Threading.Tasks;
using UIKit;
namespace IndoorCO2App_Multiplatform
{
public class BluetoothHelperApple : IBluetoothHelper
{
CBCentralManager bluetoothManager;
CLLocationManager locationManager;
public BluetoothHelperApple()
{
bluetoothManager = new CBCentralManager();
locationManager = new CLLocationManager();
}
public bool CheckStatus()
{
var status = CBManager.Authorization == CBManagerAuthorization.AllowedAlways
? PermissionStatus.Granted
: PermissionStatus.Denied;
if (status == PermissionStatus.Granted) return true;
else return false;
}
public async Task<PermissionStatus> RequestAsync()
{
// Check if Bluetooth and Location permissions are already granted
if (CheckStatus())
return PermissionStatus.Granted;
// Request Location permission
locationManager.RequestWhenInUseAuthorization();
// Wait for authorization
var tcs = new TaskCompletionSource<PermissionStatus>();
locationManager.AuthorizationChanged += (sender, args) =>
{
if (args.Status == CLAuthorizationStatus.AuthorizedWhenInUse || args.Status == CLAuthorizationStatus.AuthorizedAlways)
{
tcs.SetResult(PermissionStatus.Granted);
}
else
{
tcs.SetResult(PermissionStatus.Denied);
}
};
return await tcs.Task;
}
public void EnsureDeclared()
{
// Ensure Bluetooth and Location permissions are declared in Info.plist
bool hasBluetoothUsageDescription = NSBundle.MainBundle.InfoDictionary.ContainsKey(new NSString("NSBluetoothAlwaysUsageDescription"));
bool hasLocationWhenInUseUsageDescription = NSBundle.MainBundle.InfoDictionary.ContainsKey(new NSString("NSLocationWhenInUseUsageDescription"));
if (!hasBluetoothUsageDescription || !hasLocationWhenInUseUsageDescription)
{
throw new PermissionException("Bluetooth and/or Location permissions are not set in Info.plist.");
}
}
public async void RequestBluetoothEnable()
{
if (MainPage.bluetoothHelper.CheckIfBTEnabled()) return;
// Show the dialog asking if the user wants to enable Bluetooth
bool result = await App.Current.MainPage.DisplayAlert(
"Enable Bluetooth",
"Bluetooth is currently disabled. Would you like to enable it?",
"Yes",
"No");
// If the user clicks "Yes", redirect them to the Bluetooth settings
if (result)
{
var url = new NSUrl("App-Prefs:root=Bluetooth");
if (UIApplication.SharedApplication.CanOpenUrl(url))
{
UIApplication.SharedApplication.OpenUrl(url);
}
}
}
public bool CheckIfBTEnabled()
{
// Check if Bluetooth is enabled
return bluetoothManager.State == CBManagerState.PoweredOn;
}
public bool HasPermissionInManifest(string permission)
{
// Ensure Bluetooth and Location permissions are declared in Info.plist
bool hasBluetoothUsageDescription = NSBundle.MainBundle.InfoDictionary.ContainsKey(new NSString("NSBluetoothAlwaysUsageDescription"));
bool hasLocationWhenInUseUsageDescription = NSBundle.MainBundle.InfoDictionary.ContainsKey(new NSString("NSLocationWhenInUseUsageDescription"));
if (!hasBluetoothUsageDescription || !hasLocationWhenInUseUsageDescription)
{
return false;
}
else return true;
}
}
}
#endif