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
No related branches found
No related tags found
1 merge request!1App now has a basic structure and BLE support
......@@ -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}");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment