Created
April 13, 2022 20:49
-
-
Save ctacke/00ddfc3d6a617e862406dfbd12fcbf86 to your computer and use it in GitHub Desktop.
Meadow BLE Console Sample (feat. 32-Feet.NET)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using InTheHand.Bluetooth; | |
| using System; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var discoveryTask = TestDeviceDiscovery(); | |
| discoveryTask.Wait(); | |
| } | |
| private static async Task<bool> TestDeviceDiscovery() | |
| { | |
| Console.WriteLine($"Discovering BLE devices (this may take a while)..."); | |
| var discoveredDevices = await Bluetooth.ScanForDevicesAsync(); | |
| if (discoveredDevices != null) | |
| { | |
| Console.WriteLine($"found {discoveredDevices?.Count} devices"); | |
| BluetoothDevice meadow = null; | |
| foreach (var device in discoveredDevices) | |
| { | |
| if (device.Name == "PROJECT LAB") //"94B97E910E92" | |
| { | |
| meadow = device; | |
| Console.WriteLine($" {device.Id} <-- {device.Name}"); | |
| } | |
| else | |
| { | |
| Console.WriteLine($" {device.Id}"); | |
| } | |
| } | |
| if (meadow != null) | |
| { | |
| Console.WriteLine($"Project Lab was found! Connecting..."); | |
| await meadow.Gatt.ConnectAsync(); | |
| Console.WriteLine($"Getting GATT service..."); | |
| var service = await meadow.Gatt.GetPrimaryServiceAsync(BluetoothUuid.FromShortId(253)); | |
| Console.WriteLine($"Getting characteristics..."); | |
| var characteristics = await service.GetCharacteristicsAsync(); | |
| Console.WriteLine($"{characteristics.Count} characteristics found."); | |
| var lastTemp = string.Empty; | |
| var lastClicks = -1; | |
| while (meadow.Gatt.IsConnected) | |
| { | |
| foreach (var c in characteristics) | |
| { | |
| var value = await c.ReadValueAsync(); | |
| // we know the UUIDs of our characteristics | |
| if (c.Uuid.ToString().EndsWith("aa")) | |
| { // temp | |
| var temp = Encoding.ASCII.GetString(value).Trim(); | |
| if (temp != lastTemp) | |
| { | |
| Console.WriteLine($"Temp is now {temp}"); | |
| lastTemp = temp; | |
| } | |
| } | |
| else if (c.Uuid.ToString().EndsWith("bb")) | |
| { // clicks | |
| var clicks = BitConverter.ToInt16(value); | |
| if (clicks != lastClicks) | |
| { | |
| Console.WriteLine($"Clicks is now {clicks}"); | |
| lastClicks = clicks; | |
| } | |
| } | |
| else | |
| { | |
| Console.WriteLine($" {c.Uuid}: {BitConverter.ToString(value)}"); | |
| } | |
| } | |
| await Task.Delay(1000); | |
| } | |
| } | |
| } | |
| return discoveredDevices?.Count > 0; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment