Skip to content
Snippets Groups Projects
Commit d5a636f0 authored by Paul-Winpenny's avatar Paul-Winpenny
Browse files

Read and write values of the characteristics are now gotten.

parent 6bf364f0
Branches
No related tags found
1 merge request!1App now has a basic structure and BLE support
...@@ -15,6 +15,9 @@ using System.Collections.Generic; ...@@ -15,6 +15,9 @@ using System.Collections.Generic;
using Windows.Networking.Connectivity; using Windows.Networking.Connectivity;
using Windows.Devices.WiFi; using Windows.Devices.WiFi;
using System.Reflection.PortableExecutable;
using Plugin.BLE.Windows;
using System.Text;
namespace RobobinApp.ViewModels namespace RobobinApp.ViewModels
{ {
...@@ -33,6 +36,9 @@ namespace RobobinApp.ViewModels ...@@ -33,6 +36,9 @@ namespace RobobinApp.ViewModels
private string ssid; private string ssid;
private string password; private string password;
public bool IsBluetoothDeviceSelectionVisible => !IsWifiNetworkSelectionVisible; 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 public bool IsConnected
{ {
...@@ -98,7 +104,7 @@ namespace RobobinApp.ViewModels ...@@ -98,7 +104,7 @@ namespace RobobinApp.ViewModels
Debug.WriteLine("Checking and requesting Bluetooth permissions."); Debug.WriteLine("Checking and requesting Bluetooth permissions.");
CheckAndRequestBluetoothPermissions(); CheckAndRequestBluetoothPermissions();
ScanForWifiNetworks(); //ScanForWifiNetworks();
} }
private async void ScanForWifiNetworks() private async void ScanForWifiNetworks()
{ {
...@@ -130,7 +136,7 @@ namespace RobobinApp.ViewModels ...@@ -130,7 +136,7 @@ namespace RobobinApp.ViewModels
SignalStrength = network.NetworkRssiInDecibelMilliwatts 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"); Debug.WriteLine($"Found Wi-Fi network: {wifiNetwork.SSID}, Signal Strength: {wifiNetwork.SignalStrength} dBm");
} }
} }
...@@ -172,7 +178,7 @@ namespace RobobinApp.ViewModels ...@@ -172,7 +178,7 @@ namespace RobobinApp.ViewModels
} }
var characteristics = await uartService.GetCharacteristicsAsync(); 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) if (rxCharacteristic == null)
{ {
...@@ -296,7 +302,7 @@ namespace RobobinApp.ViewModels ...@@ -296,7 +302,7 @@ namespace RobobinApp.ViewModels
if (!BluetoothDevices.Any(d => d.MacAddress == bluetoothDevice.MacAddress)) if (!BluetoothDevices.Any(d => d.MacAddress == bluetoothDevice.MacAddress))
{ {
Device.BeginInvokeOnMainThread(() => BluetoothDevices.Add(bluetoothDevice)); Microsoft.Maui.Controls.Device.BeginInvokeOnMainThread(() => BluetoothDevices.Add(bluetoothDevice));
} }
} }
...@@ -353,29 +359,47 @@ namespace RobobinApp.ViewModels ...@@ -353,29 +359,47 @@ namespace RobobinApp.ViewModels
{ {
Debug.WriteLine($"Service: ID={service.Id}, Name={service.Name}"); 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"))
{
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) 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)
{ {
// Read the device name characteristic Debug.WriteLine("Transmit service wasn't found");
Debug.WriteLine($"Device Name: {deviceNameCharacteristic}");
await Application.Current.MainPage.DisplayAlert("Device Name", $"Device Name: Paul", "OK");
} }
else var characteristics = await transmitService.GetCharacteristicsAsync();
foreach (var characteristic in characteristics)
{
Debug.WriteLine($"{characteristic.Uuid} | {characteristic.Name}");
if (characteristic.Uuid.Equals(rxUUID))
{ {
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) catch (DeviceConnectionException ex)
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment