diff --git a/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs b/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs index 7630059918cc5caf5256cb9e657471a8a754beb8..0b639ce45e3816359a6771d2edb6c43a0ffff011 100644 --- a/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs +++ b/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs @@ -15,6 +15,9 @@ using System.Collections.Generic; using Windows.Networking.Connectivity; using Windows.Devices.WiFi; +using System.Reflection.PortableExecutable; +using Plugin.BLE.Windows; +using System.Text; namespace RobobinApp.ViewModels { @@ -33,6 +36,9 @@ namespace RobobinApp.ViewModels private string ssid; private string password; public bool IsBluetoothDeviceSelectionVisible => !IsWifiNetworkSelectionVisible; + 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 bool IsConnected { @@ -98,7 +104,7 @@ namespace RobobinApp.ViewModels Debug.WriteLine("Checking and requesting Bluetooth permissions."); CheckAndRequestBluetoothPermissions(); - ScanForWifiNetworks(); + //ScanForWifiNetworks(); } private async void ScanForWifiNetworks() { @@ -130,7 +136,7 @@ namespace RobobinApp.ViewModels SignalStrength = network.NetworkRssiInDecibelMilliwatts }; - Device.BeginInvokeOnMainThread(() => WifiNetworks.Add(wifiNetwork)); + Microsoft.Maui.Controls.Device.BeginInvokeOnMainThread(() => WifiNetworks.Add(wifiNetwork)); Debug.WriteLine($"Found Wi-Fi network: {wifiNetwork.SSID}, Signal Strength: {wifiNetwork.SignalStrength} dBm"); } } @@ -172,7 +178,7 @@ namespace RobobinApp.ViewModels } var characteristics = await uartService.GetCharacteristicsAsync(); - var rxCharacteristic = characteristics.FirstOrDefault(c => c.Id == Guid.Parse("6e400002-b5a3-f393-e0a9-e50e24dcca9e")); + var rxCharacteristic = characteristics.FirstOrDefault(c => c.Id == Guid.Parse(rxUUID)); if (rxCharacteristic == null) { @@ -296,7 +302,7 @@ namespace RobobinApp.ViewModels if (!BluetoothDevices.Any(d => d.MacAddress == bluetoothDevice.MacAddress)) { - Device.BeginInvokeOnMainThread(() => BluetoothDevices.Add(bluetoothDevice)); + Microsoft.Maui.Controls.Device.BeginInvokeOnMainThread(() => BluetoothDevices.Add(bluetoothDevice)); } } @@ -353,30 +359,48 @@ namespace RobobinApp.ViewModels { Debug.WriteLine($"Service: ID={service.Id}, Name={service.Name}"); - // Check if this is the Generic Access service - if (service.Id == Guid.Parse("00001800-0000-1000-8000-00805f9b34fb")) + + } + + + var transmitService = services.FirstOrDefault(s => s.Id == Guid.Parse(SendReceiveServiceUUID)); + Debug.WriteLine("Transmit service was accessed"); + Debug.WriteLine($"{transmitService.Name} : {transmitService.Id}"); + if (transmitService == null) + { + Debug.WriteLine("Transmit service wasn't found"); + } + var characteristics = await transmitService.GetCharacteristicsAsync(); + foreach (var characteristic in characteristics) + { + Debug.WriteLine($"{characteristic.Uuid} | {characteristic.Name}"); + if (characteristic.Uuid.Equals(rxUUID)) { - Debug.WriteLine("Found Generic Access service!"); - - // Get the characteristic for the device name - var deviceNameCharacteristic = service.GetCharacteristicAsync(Guid.Parse("00002a00-0000-1000-8000-00805f9b34fb")); - - if (deviceNameCharacteristic != null) - { - // Read the device name characteristic - - Debug.WriteLine($"Device Name: {deviceNameCharacteristic}"); - await Application.Current.MainPage.DisplayAlert("Device Name", $"Device Name: Paul", "OK"); - } - else - { - Debug.WriteLine("Device name characteristic not found."); - } + Debug.WriteLine("Found Read"); + + // Read the value from the characteristic + var temperatureData = await characteristic.ReadAsync(); + + // Convert the byte array to a string + var temperatureValue = System.Text.Encoding.UTF8.GetString(temperatureData.data); + + Debug.WriteLine($"Temperature Value: {temperatureValue}"); + } + else if (characteristic.Uuid.Equals(wxUUID)) + { + Debug.WriteLine("Found Write"); + + // Read the current value (if needed) + var writeValueData = await characteristic.ReadAsync(); + var writeValue = System.Text.Encoding.UTF8.GetString(writeValueData.data); + + Debug.WriteLine($"Current Write Value: {writeValue}"); + } } - // Optional: Handle other services or characteristics if needed - } + + } catch (DeviceConnectionException ex) { Debug.WriteLine($"Connection error: {ex.Message}");