-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathDetailsPage.xaml.cs
50 lines (43 loc) · 1.47 KB
/
DetailsPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
namespace MauiBluetooth;
using System.Text;
using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Views;
using Plugin.BLE.Abstractions.Contracts;
public partial class DetailsPage : Popup
{
public IDevice Device { get; }
private readonly IBluetoothService bluetoothService;
public DetailsPage(IDevice device, IBluetoothService bluetoothService)
{
Device = device;
this.bluetoothService = bluetoothService;
InitializeComponent();
BindingContext = this;
Size = new Size(200, 500);
}
private async void GetServices(object sender, EventArgs e)
{
var services = await Device.GetServicesAsync();
var service = services.First();
var characteristics = await service.GetCharacteristicsAsync();
var characteristic = characteristics.First();
//var bytes = await characteristic.ReadAsync();
characteristic.ValueUpdated += (o, args) =>
{
var bytes = args.Characteristic.Value;
};
await characteristic.StartUpdatesAsync();
var descriptors = await characteristic.GetDescriptorsAsync();
var descriptor = descriptors.First();
var bytes = await descriptor.ReadAsync();
await descriptor.WriteAsync(bytes);
}
private async void SendMessage(object sender, EventArgs e)
{
await bluetoothService.Connect(Device.Name);
await bluetoothService.Send(Device.Name, Encoding.Default.GetBytes("test"));
var data = await bluetoothService.Receive(Device.Name);
await Toast.Make(Encoding.Default.GetString(data)).Show();
await bluetoothService.Disconnect();
}
}