From 8a848bbb567edda2cc73a4ee5c3a74cf3bed93c6 Mon Sep 17 00:00:00 2001 From: Paul-Winpenny <92634321+Paul-Winpenny@users.noreply.github.com> Date: Wed, 23 Oct 2024 18:21:47 +0100 Subject: [PATCH] Names are now used to find characteristics instead of UUIDs --- .../ViewModels/ConnectionPageViewModel.cs | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs b/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs index 14acad7d..9a4c2b1f 100644 --- a/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs +++ b/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs @@ -19,6 +19,7 @@ using System.Reflection.PortableExecutable; using Plugin.BLE.Windows; using System.Text; using System.Windows.Documents; +using Robobin.Interfaces; namespace RobobinApp.ViewModels { @@ -43,6 +44,10 @@ namespace RobobinApp.ViewModels public const string SendReceiveServiceUUID = "00000001-710e-4a5b-8d75-3e5b444bc3cf"; public const string rxUUID = "00000002-710e-4a5b-8d75-3e5b444bc3cf"; public const string wxUUID = "00000003-710e-4a5b-8d75-3e5b444bc3cf"; + public const string readCharacteristicName = "CPU Temperature"; + public const string writeCharacteristicName = "Temperature Units (F or C)"; + + public string tempUnit = "C"; public bool IsConnected @@ -52,7 +57,7 @@ namespace RobobinApp.ViewModels { _isConnected = value; OnPropertyChanged(); - OnPropertyChanged(nameof(ConnectButtonText)); // Update button text when connection state changes + OnPropertyChanged(nameof(ConnectButtonText)); } } private ICharacteristic _readCharacteristic; @@ -109,6 +114,11 @@ namespace RobobinApp.ViewModels public ConnectionPageViewModel() { + + _bluetoothLE = CrossBluetoothLE.Current; + _adapter = CrossBluetoothLE.Current.Adapter; + + BluetoothDevices = new ObservableCollection<BluetoothDevice>(); WifiNetworks = new ObservableCollection<WifiNetwork>(); ConnectCommand = new Command(OnConnect); @@ -119,8 +129,8 @@ namespace RobobinApp.ViewModels SendWifiInfoCommand = new Command(OnSendWifiInfo); TestWriteOperation = new Command(async() => await PingPiASync()); - _bluetoothLE = CrossBluetoothLE.Current; - _adapter = _bluetoothLE.Adapter; + + _adapter.DeviceDiscovered += OnDeviceDiscovered; IsWifiNetworkSelectionVisible = false; @@ -154,7 +164,7 @@ namespace RobobinApp.ViewModels Debug.WriteLine("Retrieving Wifi networks from readCharacteristic"); //Delay 5 seconds - await Task.Delay(5000); + await Task.Delay(2500); //var networks = await ReadOperationAsync(); var networks = "Network1\nNetwork2\nNetwork3"; // For testing @@ -347,15 +357,22 @@ namespace RobobinApp.ViewModels foreach (var characteristic in characteristics) { - var descriptor = await characteristic.GetDescriptorsAsync(); - Debug.WriteLine($"{characteristic.Uuid} | {characteristic.Name} | {descriptor}"); - if (characteristic.Uuid.Equals(rxUUID)) + var descriptors = await characteristic.GetDescriptorsAsync(); + foreach (var descriptor in descriptors) + { + var descriptorValue = await descriptor.ReadAsync(); + var descriptorValueString = Encoding.UTF8.GetString(descriptorValue); + Debug.WriteLine($"Descriptor: {descriptor.Id} | Value: {descriptorValueString}"); + } + + Debug.WriteLine($"{characteristic.Uuid} |{characteristic.Name}"); + if (characteristic.Name.Equals(readCharacteristicName)) { ReadCharacteristic = characteristic; var result = await ReadOperationAsync(); } - else if (characteristic.Uuid.Equals(wxUUID)) + else if (characteristic.Name.Equals(writeCharacteristicName)) { WriteCharacteristic = characteristic; const string command = "SCAN"; @@ -485,8 +502,8 @@ namespace RobobinApp.ViewModels IsWifiNetworkSelectionVisible = false; // Dispose of the device reference if (_connectedDevice != null) - _connectedDevice.Dispose(); - _connectedDevice = null; + // _connectedDevice.Dispose(); + _connectedDevice = null; IsConnected = false; ScanDevices(); await Application.Current.MainPage.DisplayAlert("Disconnected", "Device disconnected successfully.", "OK"); -- GitLab