-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCubeBluetoothLEAdvertisementWatcher.cs
More file actions
65 lines (60 loc) · 2.17 KB
/
CubeBluetoothLEAdvertisementWatcher.cs
File metadata and controls
65 lines (60 loc) · 2.17 KB
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
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Security.Cryptography;
using Windows.Storage.Streams;
namespace virtual_cube
{
public class CubeBluetoothLEAdvertisementWatcher
{
private readonly BluetoothLEAdvertisementWatcher watcher;
public delegate void DeviceFoundEventHandler(Cube rubiksConnected);
public event DeviceFoundEventHandler DeviceFoundEvent;
protected virtual void RaiseDeviceFoundEvent(Cube cube)
{
// Raise the event in a thread-safe manner using the ?. operator.
DeviceFoundEvent?.Invoke(cube);
}
public CubeBluetoothLEAdvertisementWatcher()
{
watcher = new BluetoothLEAdvertisementWatcher
{
ScanningMode = BluetoothLEScanningMode.Active
};
watcher.Received += WatcherAdvertisementReceived;
}
public void Start()
{
watcher.Start();
}
public void Stop()
{
watcher.Stop();
}
private async void WatcherAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs args)
{
if (args.Advertisement.LocalName.StartsWith("R"))
{
foreach (BluetoothLEManufacturerData data in args.Advertisement.ManufacturerData)
{
Debug.WriteLine("Company" + data.CompanyId);
}
foreach (var data in args.Advertisement.ServiceUuids)
{
Debug.WriteLine("Service:" + data);
}
Debug.WriteLine($"Found {args.Advertisement.LocalName} | {Cube.MAC802DOT3(args.BluetoothAddress)} | {args.AdvertisementType}");
var cube = CubeFactory.CreateCube(args);
if (cube != null)
{
RaiseDeviceFoundEvent(cube);
}
}
}
}
}