/
BtNotificationPage.razor
114 lines (94 loc) · 3.51 KB
/
BtNotificationPage.razor
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
@page "/BtNotification"
@attribute [TabItemOption(Text = "蓝牙特征通知")]
@using System.ComponentModel
<a href="https://www.nuget.org/packages/BootstrapBlazor.Bluetooth#readme-body-tab"><h4>蓝牙特征通知</h4></a>
<hr />
<a href="https://github.com/densen2014/Densen.Extensions/blob/master/Demo/DemoShared/Pages/BtNotificationPage.razor"><h5>页面源码</h5></a>
<BootstrapInput @bind-Value="ServiceUuid" ShowLabel="true" />
<BootstrapInput @bind-Value="CharacteristicUuid" ShowLabel="true" />
<Switch @bind-Value="AutomaticComplement" ShowLabel="true" />
<Switch @bind-Value="AutoConnect" ShowLabel="true" />
<Switch @bind-Value="AdvertisementReceived" ShowLabel="true" />
<button class="btn btn-outline-secondary" @onclick="GetNotification ">读取通知</button>
<button class="btn btn-outline-secondary" @onclick="StopNotification ">停止读取</button>
<button class="btn btn-outline-secondary" @onclick="Scan ">扫描设备</button>
<BleNotification @ref="Notification" OnUpdateValue="OnUpdateValue" OnUpdateStatus="OnUpdateStatus" OnUpdateError="OnError" />
<h2 style="color:red" data-action="notificationValue" />
<pre>@message</pre>
<pre style="color:green">@statusmessage</pre>
<pre style="color:red">@errmessage</pre>
@code{
//string? heartrateIcon;// { get => (heartrateIcon == "❤" ? "♥" : "❤"); }
BleNotification Notification { get; set; } = new BleNotification();
/// <summary>
/// 服务UUID / Service UUID
/// </summary>
[DisplayName("服务UUID")]
public string ServiceUuid { get; set; } = "heart_rate";
/// <summary>
/// 特征UUID / Characteristic UUID
/// </summary>
/// <returns></returns>
[DisplayName("特征UUID")]
public string CharacteristicUuid { get; set; } = "heart_rate_measurement";
/// <summary>
/// 自动连接设备
/// </summary>
[DisplayName("自动连接设备")]
public bool AutoConnect { get; set; } = true;
/// <summary>
/// 自动补全, 否则为转数字格式
/// </summary>
[DisplayName("自动补全, 否则为转数字格式")]
public bool AutomaticComplement { get; set; }
/// <summary>
/// 显示扫描结果
/// </summary>
[DisplayName("显示扫描结果")]
public bool AdvertisementReceived { get; set; }
private string? message;
private string? value;
private string? statusmessage;
private string? errmessage;
private Task OnResult(string message)
{
this.message = message;
StateHasChanged();
return Task.CompletedTask;
}
private Task OnUpdateValue(string value)
{
this.value = value;
this.statusmessage = $"{value}";
StateHasChanged();
return Task.CompletedTask;
}
private Task OnUpdateStatus(BluetoothDevice device)
{
this.statusmessage = device.Status;
StateHasChanged();
return Task.CompletedTask;
}
private Task OnError(string message)
{
this.errmessage = message;
StateHasChanged();
return Task.CompletedTask;
}
public async void GetNotification()
{
this.message = null;
this.value = null;
this.statusmessage = null;
this.errmessage = null;
await Notification.GetNotification(ServiceUuid, CharacteristicUuid, AutoConnect, AutomaticComplement, AdvertisementReceived);
}
public async void StopNotification()
{
await Notification.StopNotification();
}
public async void Scan()
{
await Notification.Scan();
}
}