forked from CarterAppleton/Win10Win32Bluetooth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
56 lines (45 loc) · 1.93 KB
/
Program.cs
File metadata and controls
56 lines (45 loc) · 1.93 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.System;
using Windows.Devices.Bluetooth.Advertisement;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Start the program
var program = new Program();
// Close on key press
Console.ReadLine();
}
public Program()
{
// Create Bluetooth Listener
var watcher = new BluetoothLEAdvertisementWatcher();
watcher.ScanningMode = BluetoothLEScanningMode.Active;
// Only activate the watcher when we're recieving values >= -80
watcher.SignalStrengthFilter.InRangeThresholdInDBm = -80;
// Stop watching if the value drops below -90 (user walked away)
watcher.SignalStrengthFilter.OutOfRangeThresholdInDBm = -90;
// Register callback for when we see an advertisements
watcher.Received += OnAdvertisementReceived;
// Wait 5 seconds to make sure the device is really out of range
watcher.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(5000);
watcher.SignalStrengthFilter.SamplingInterval = TimeSpan.FromMilliseconds(2000);
// Starting watching for advertisements
watcher.Start();
}
private void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
// Tell the user we see an advertisement and print some properties
Console.WriteLine(String.Format("Advertisement:"));
Console.WriteLine(String.Format(" BT_ADDR: {0}", eventArgs.BluetoothAddress));
Console.WriteLine(String.Format(" FR_NAME: {0}", eventArgs.Advertisement.LocalName));
Console.WriteLine();
}
}
}