-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBluetoothScanActivity.cs
229 lines (192 loc) · 5.97 KB
/
BluetoothScanActivity.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using Android;
using Android.Bluetooth;
using Android.Content;
using Android.Content.PM;
using AndroidX.Core.App;
using AndroidX.Core.Content;
using Nauti_Control_Wear.Adapters;
using Nauti_Control_Wear.ViewModels;
namespace Nauti_Control_Wear;
[Activity(Label = "@string/app_name" +
"", MainLauncher = true)]
public class BluetoothScanActivity : Activity
{
/// <summary>
/// Request Enable BT
/// </summary>
private static int REQUEST_ENABLE_BT = 1;
/// <summary>
/// Bluetooth Manager
/// </summary>
private BluetoothManagerVM? _bluetoothManager;
/// <summary>
/// Scan Button
/// </summary>
private Button? _scanButton;
/// <summary>
/// BT List Adapter
/// </summary>
private BluetoothListAdapter? _btAdapter;
/// <summary>
/// On Create
/// </summary>
/// <param name="savedInstanceState"></param>
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.bluetooth_scan);
_bluetoothManager = new BluetoothManagerVM();
_bluetoothManager.OnScanStatusChanged += _bluetoothManager_OnScanStatusChanged;
ListView? mainList = FindViewById<ListView>(Resource.Id.idbtdevice);
_scanButton = FindViewById<Button>(Resource.Id.idbtstartscan);
if (_scanButton != null)
{
_scanButton.Click += ScanButton_Click;
}
if (mainList != null)
{
_btAdapter = new BluetoothListAdapter(_bluetoothManager);
mainList.Adapter = _btAdapter;
mainList.ItemClick += MainList_ItemClick;
}
}
private void _bluetoothManager_OnScanStatusChanged(object? sender, EventArgs e)
{
if (_bluetoothManager.IsScanning)
{
if (_scanButton != null)
{
_scanButton.Text = "Stop Scan";
}
}
else
{
if (_scanButton != null)
{
_scanButton.Text = "Start Scan";
}
}
}
/// <summary>
/// Main List Item Click
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">event</param>
private void MainList_ItemClick(object? sender, AdapterView.ItemClickEventArgs e)
{
if (e.View != null && e.View.Tag != null)
{
BluetoothViewHolder? view = e.View.Tag as BluetoothViewHolder;
if (view != null)
{
// Save Battery Stop Scanning
_bluetoothManager.StopScanning();
view.Device.OnConnected += Device_OnConnected;
view.Device.Connect();
}
}
}
/// <summary>
/// Device On Connected
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Device_OnConnected(object? sender, EventArgs e)
{
BluetoothDeviceVM? deviceVM = sender as BluetoothDeviceVM;
if (deviceVM != null)
{
Intent intent = new Intent(this, typeof(MainMenuActivity));
this.StartActivity(intent);
}
}
/// <summary>
/// Scan Button Click
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">Event</param>
private void ScanButton_Click(object? sender, EventArgs e)
{
if (_bluetoothManager != null)
{
if (!_bluetoothManager.IsScanning)
{
if (CheckPermissions())
{
if (_bluetoothManager != null)
{
_bluetoothManager.StartScanning();
}
}
else
{
DisplayPermissionsDialog();
}
}
else
{
_bluetoothManager.StopScanning();
}
}
}
/// <summary>
/// Check Permissions
/// </summary>
/// <returns>boolean response</returns>
private bool CheckPermissions()
{
return (ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) == Permission.Granted);
}
/// <summary>
/// Display Permissions Dialog
/// </summary>
private void DisplayPermissionsDialog()
{
AlertDialog.Builder alertDiag = new AlertDialog.Builder(this);
alertDiag.SetTitle("Location Permission");
alertDiag.SetMessage("BT Scan requires this");
alertDiag.SetPositiveButton("Ok", RequestPermissions);
alertDiag.SetNegativeButton("Cancel", (senderAlert, args) =>
{
alertDiag.Dispose();
});
Dialog? diag = alertDiag.Create();
if (diag != null)
{
diag.Show();
}
}
/// <summary>
/// Request Permissions
/// </summary>
/// <param name="senderAlert">Sender Alert</param>
/// <param name="args">Args</param>
private void RequestPermissions(object? senderAlert, DialogClickEventArgs args)
{
ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.AccessFineLocation }, 1);
}
/// <summary>
/// On Request Permissions Result
/// </summary>
/// <param name="requestCode">Request Code</param>
/// <param name="permissions">Permissions</param>
/// <param name="grantResults">Grand Results</param>
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
if ((requestCode == 1) && (grantResults.Length > 0) && (grantResults[0] == Permission.Granted))
{
if (_bluetoothManager != null)
{
_bluetoothManager.StartScanning();
}
}
else
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
if (_scanButton != null)
{
_scanButton.Enabled = false;
}
}
}
}