-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestPage.xaml.cs
executable file
·428 lines (361 loc) · 12.1 KB
/
TestPage.xaml.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
using System;
using System.Collections.Generic;
using Xamarin.Forms;
using System.Collections.ObjectModel;
using Motion.Mobile.Core.BLE;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using Motion.Misc.Utilities;
using Motion.Core.WSHandler;
using Motion.Core.SyncHandler;
using Newtonsoft.Json;
using PCLStorage;
using System.IO;
namespace XamTest
{
public partial class TestPage : ContentPage
{
string codeGenerated;
IAdapter adapter;
IDevice device;
IService service;
ICharacteristic ff07Char;
ICharacteristic ff08Char;
ObservableCollection<IDevice> devices;
ObservableCollection<IService> services;
ObservableCollection<ICharacteristic> characteristics;
public TestPage (IAdapter adapter)
{
InitializeComponent ();
this.adapter = adapter;
this.devices = new ObservableCollection<IDevice> ();
this.services = new ObservableCollection<IService> ();
this.characteristics = new ObservableCollection<ICharacteristic> ();
adapter.BluetoothStateUpdated += Adapter_BluetoothStateUpdated;
adapter.ScanCompleted += Adapter_ScanCompleted;
adapter.DeviceDiscovered += Adapter_DeviceDiscovered;
adapter.DeviceConnected += Adapter_DeviceConnected;
adapter.DeviceDisconnected += Adapter_DeviceDisconnected;
adapter.DeviceFailedToConnect += Adapter_DeviceFailedToConnect;
adapter.CommandResponse += Adapter_CommandResponse;
btnValidate.Clicked += OnStartClicked;
btnGenerateCode.Clicked += OnGenerateCodeClicked;
btnDisconnect.Clicked += OnDisconnectDevice;
btnGenSerial.Clicked += OnGenSerialClicked;
btnValidateUser.Clicked += BtnValidateUser_Clicked;
btnChangeUser.Clicked += BtnChangeUser_Clicked;
//file.CreateFileAsync ("testFile.txt");
}
async void BtnChangeUser_Clicked(object sender, EventArgs e)
{
IFolder rootFolder = FileSystem.Current.LocalStorage;
IFolder folder = await rootFolder.CreateFolderAsync("Cache",
CreationCollisionOption.OpenIfExists);
IFile file = await folder.CreateFileAsync("Test3" + ".txt",
CreationCollisionOption.OpenIfExists);
Stream fileStream = await file.OpenAsync(FileAccess.ReadAndWrite);
if (fileStream.CanSeek)
{
Debug.WriteLine("Can Seek");
fileStream.Seek(0, SeekOrigin.End);
}
string str;
using (StreamWriter writer = new StreamWriter(fileStream, Encoding.UTF8))
{
await writer.WriteLineAsync("Berkssss");
writer.Flush();
}
fileStream = await file.OpenAsync(FileAccess.Read);
using (StreamReader reader = new StreamReader(fileStream, Encoding.UTF8))
{
str = reader.ReadToEnd();
}
Debug.WriteLine(str);
IList<IFile> filenames = await folder.GetFilesAsync(System.Threading.CancellationToken.None);
foreach(IFile f in filenames)
{
Debug.WriteLine(f.Name);
}
}
void BtnValidateUser_Clicked(object sender, EventArgs e)
{
//Post
WebService ws = new WebService();
string credentials = txtEmail.Text + "|" + txtPassword.Text;
Dictionary<string, object> data = new Dictionary<string, object> { };
data.Add("ident", credentials);
Task<Dictionary<string,object>> response = ws.PostData("https://test.savvysherpa.com/DeviceServices/api/Pedometer/ValidateUser", data);
string dataStr = JsonConvert.SerializeObject(response);
Debug.WriteLine(dataStr);
}
async void OnGenSerialClicked(object sender, EventArgs e)
{
WebService ws = new WebService();
//Get
Task<Dictionary<string,object>> task1 = ws.GetData("https://test.savvysherpa.com/DeviceServices/api/Production/GenerateSerial");
Dictionary<string, object> response = await task1;
string dataStr = JsonConvert.SerializeObject(response);
Debug.WriteLine(dataStr);
}
void Adapter_BluetoothStateUpdated (object sender, BluetoothStateEventArgs e)
{
Debug.WriteLine ("BT state: " + e.BTState);
}
void Adapter_CommandResponse (object sender, CommandResponseEventArgs e)
{
Debug.WriteLine ("Response value " + System.Text.Encoding.UTF8.GetString(e.Data,0,e.Data.Length));
Device.BeginInvokeOnMainThread(() => {
this.lblCodeCaption.IsVisible = true;
this.txtCode.IsVisible = true;
this.btnValidate.IsVisible = true;
btnValidate.IsEnabled = true;
btnValidate.TextColor = Color.Blue;
});
}
void Device_ServicesDiscovered (object sender, EventArgs e)
{
Debug.WriteLine("device.ServicesDiscovered");
//this.services = (List<IService>)device.Services;
if (this.services.Count == 0)
{
Debug.WriteLine ("Service Count: " + device.Services.Count);
foreach (var service in device.Services)
{
service.CharacteristicsDiscovered += (object s, EventArgs ce) => {
Debug.WriteLine("service.CharacteristicsDiscovered");
if (this.characteristics.Count == 0)
{
Debug.WriteLine("Characteristics count: "+service.Characteristics.Count);
foreach (var characteristic in service.Characteristics)
{
if(characteristic !=null)
{
if(characteristic.Uuid.ToUpper().Contains("2A24") ||
characteristic.Uuid.ToUpper().Contains("2A25") ||
characteristic.Uuid.ToUpper().Contains("2A26") ||
characteristic.Uuid.ToUpper().Contains("2A19") )
{
if(characteristic.CanRead)
{
characteristic.ValueUpdated += (cs, chre) => {
Debug.WriteLine("characteristic.ValueUpdated");
};
characteristic.ReadAsync();
}
}
else if(characteristic.Uuid.ToUpper().Contains("FF07"))
{
Debug.WriteLine("FF07");
if(characteristic.CanUpdate)
{
this.ff07Char = characteristic;
characteristic.StartUpdates();
}
}
else if(characteristic.Uuid.ToUpper().Contains("FF08"))
{
Debug.WriteLine("FF08");
if(characteristic.CanUpdate)
{
this.ff08Char = characteristic;
characteristic.StartUpdates();
}
}
}
}
}
};
service.DiscoverCharacteristics ();
}
}
}
void Adapter_DeviceFailedToConnect (object sender, DeviceConnectionEventArgs e)
{
StartScanning ();
}
void Adapter_DeviceDisconnected (object sender, DeviceConnectionEventArgs e)
{
Device.BeginInvokeOnMainThread(() => {
this.lblConnectedDevice.IsVisible = false;
this.lblCodeCaption.IsVisible = false;
this.txtCode.IsVisible = false;
this.btnValidate.IsVisible = false;
this.btnGenerateCode.IsVisible = false;
this.btnValidate.IsEnabled = false;
this.btnDisconnect.IsVisible = false;
this.lblConnectedDevice.Text = "";
this.txtCode.Text = "";
});
//this.StartScanning();
this.StartScanning (0x180F.UuidFromPartial ());
}
void Adapter_DeviceConnected (object sender, DeviceConnectionEventArgs e)
{
Debug.WriteLine("Device connected");
//adapter.StopScanningForDevices();
StopScanning();
Device.BeginInvokeOnMainThread(() => {
this.lblConnectedDevice.IsVisible = true;
this.btnGenerateCode.IsVisible = true;
this.btnDisconnect.IsVisible = true;
this.lblConnectedDevice.Text = "Device Connected: " + e.Device.Name;
});
Debug.WriteLine("Connected Devices Count: "+ adapter.ConnectedDevices.Count);
device = e.Device;
device.ServicesDiscovered += Device_ServicesDiscovered;
device.DiscoverServices ();
}
void Adapter_DeviceDiscovered (object sender, DeviceDiscoveredEventArgs e)
{
Debug.WriteLine("Device discovered");
Device.BeginInvokeOnMainThread(() => {
//devices.Add (e.Device);
//Debug.WriteLine ("Found device: "+e.Device.Name+")");
int numLines = this.lblDevice.Text.Split('\n').Length;
if(numLines > 8)
this.lblDevice.Text = "";
String c = this.lblDevice.Text;
c += "\nName: "+ e.Device.Name + " RSSI:" + e.RSSI;
this.lblDevice.Text = c;
});
//this.ConnectDevice(e.Device,e.RSSI);
}
void Adapter_ScanCompleted(object sender, EventArgs e)
{
//StopScanning();
//this.StartScanning (0x180F.UuidFromPartial ());
Device.BeginInvokeOnMainThread ( () => {
//DisplayAlert("Timeout", "Bluetooth scan timeout elapsed", "OK");
});
bool foundValidDevice = false;
foreach (var device in adapter.DiscoveredDevices) {
if (device.Name.Contains ("961") ||
device.Name.Contains ("932") ||
device.Name.Contains ("936") ||
device.Name.Contains ("939")) {
this.adapter.ConnectToDevice (device);
foundValidDevice = true;
break;
}
}
if (!foundValidDevice)
StartScanning (0x180F.UuidFromPartial ());
}
void OnDisconnectDevice(object sender, EventArgs e)
{
adapter.DisconnectDevice(this.device);
}
void OnGenerateCodeClicked(object sender, EventArgs e)
{
Debug.WriteLine ("Entering getCodeCommand method");
// byte[] command = this.getCodeCommand ();
// ff08Char.Write(command);
adapter.SendCommand (ff08Char, this.getCodeCommand ());
}
void OnStartClicked(object sender, EventArgs e)
{
//this.ff07Char.Write (this.getCodeCommand());
if (this.codeGenerated.Equals (txtCode.Text)) {
DisplayAlert ("Alert", "That's correct!", "OK");
} else {
DisplayAlert ("Alert", "Invalid code", "OK");
}
}
void ConnectDevice(IDevice device, int RSSI)
{
if (device.Name == null) {
Debug.WriteLine ("Device Name is NULL");
return;
}
if ( device.Name!=null && (device.Name.Contains ("961") || device.Name.Contains ("932") ||
device.Name.Contains ("939") || device.Name.Contains ("936") ||
device.Name.Contains ("900")) && RSSI >= -75 )
{
this.adapter.ConnectToDevice (device);
}
}
async void StartScanning () {
await Task.Delay (500);
StartScanning (Guid.Empty);
}
public async void StartScanning (Guid forService) {
await Task.Delay (500);
if (adapter.IsScanning) {
//adapter.StopScanningForDevices();
StopScanning();
Debug.WriteLine ("adapter.StopScanningForDevices()");
} else {
devices.Clear();
List<Guid> serviceUuids = new List<Guid>();
serviceUuids.Add(forService);
adapter.StartScanningForDevices(serviceUuids);
Debug.WriteLine ("adapter.StartScanningForDevices("+forService+")");
}
}
void StopScanning () {
// stop scanning
new Task( () => {
if(adapter.IsScanning) {
Debug.WriteLine ("Still scanning, stopping the scan");
adapter.StopScanningForDevices();
}
}).Start();
}
int CalculateChecksum(byte[] dataToCalculate)
{
int checksum = 0;
foreach (byte chData in dataToCalculate)
{
checksum += chData;
}
checksum &= 0xffff;
return checksum;
}
byte[] getCodeCommand()
{
this.codeGenerated = this.generate4DigitRandomNumberString ();
string codeHexStr = this.convertToHexString (this.codeGenerated);
string messageCodeStr = String.Format ("072d000000"+codeHexStr+"FF");
int messageSize = messageCodeStr.Length / 2;
int checkSum = this.CalculateChecksum (this.StringToByteArray(messageCodeStr));
string commandStr = "1B540000" + this.convertToHexString(messageSize.ToString()) + this.convertToHexString(checkSum.ToString()) + messageCodeStr;
byte[] command = this.StringToByteArray(commandStr);
Debug.WriteLine ("Get code command str: " +commandStr);
return command;
}
string generate4DigitRandomNumberString()
{
string random = String.Empty;
Random rnd = new Random();
for (int i = 0; i < 4; i++)
{
int rand = rnd.Next (0, 9);
random += rand.ToString();
}
return random;
}
string convertToHexString(string param)
{
char[] charValues = param.ToCharArray();
string hexOutput="";
foreach (char _eachChar in charValues )
{
// Get the integral value of the character.
int value = Convert.ToInt32(_eachChar);
// Convert the decimal value to a hexadecimal value in string form.
hexOutput += String.Format("{0:X}", value);
// to make output as your eg
// hexOutput +=" "+ String.Format("{0:X}", value);
}
return hexOutput;
}
byte[] StringToByteArray(string hex) {
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
}
}