Skip to content

Commit 697fdea

Browse files
authored
feat(IBluetoothService): add IBluetoothService service (#4492)
* feat: 增加蓝牙接口 * refactor: 移除可见性修饰符 * chore: 增加蓝牙服务 * doc: 更新蓝牙示例 * refactor: 增加 getAvaliablity 方法 * feat: 增加 Request 方法 * doc: 更新示例 * feat: 更新蓝牙设备 * doc: 更新示例 * feat: 增加电量读取方法 * doc: 更新示例 * refactor: 移除 GetDevices 方法 * doc: 更新示例 * feat: 增加 IBluetoothDevice 接口 * doc: 更新示例 * refactor: 更改为通用设计 * doc: 更新示例 * refactor: 精简代码 * refactor: 更新读取方法 * refactor: 更新读取数据方法 * refactor: 更新接口 * refactor: 更新代码 * feat: 增加扩展方法 * doc: 精简代码 * feat: 增加 ErrorMessage 参数 * feat: 增加异常回调方法 * feat: 增加 ErrorMessage 参数 * feat: 增加方法返回值 * refactor: 增加 IAsyncDispose 接口 * refactor: 更新脚本 * doc: 增加异常逻辑 * refactor: 代码重构 * refactor: 更新脚本使用 byte 通讯 * refactor: 更改为 byte 通讯 * refactor: 重构蓝牙模块检查 * doc: 更新文档 * doc: 更新文档 * doc: 更新文档 * doc: 更新示例 * doc: 更新文档 * doc: 更新文档 * doc: 更新示例文档 * test: 更新单元测试 * test: 更新单元测试 * test: 更新单元测试
1 parent ac3c3ca commit 697fdea

File tree

19 files changed

+717
-492
lines changed

19 files changed

+717
-492
lines changed

exclusion.dic

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,4 @@ Validatable
106106
noselect
107107
Urls
108108
Pharmacode
109+
bluetooth

src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
<PackageReference Include="BootstrapBlazor.BaiduOcr" Version="8.2.0" />
3030
<PackageReference Include="BootstrapBlazor.BarCode" Version="8.2.0" />
3131
<PackageReference Include="BootstrapBlazor.BarcodeGenerator" Version="8.2.0" />
32-
<PackageReference Include="BootstrapBlazor.Bluetooth" Version="8.0.2" />
3332
<PackageReference Include="BootstrapBlazor.BootstrapIcon" Version="8.0.3" />
3433
<PackageReference Include="BootstrapBlazor.BootstrapIcon.Extensions" Version="8.2.0" />
3534
<PackageReference Include="BootstrapBlazor.Chart" Version="8.4.0" />

src/BootstrapBlazor.Server/Components/Samples/Bluetooth.razor

Lines changed: 88 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,109 @@
33

44
<h3>@Localizer["BluetoothTitle"]</h3>
55

6-
<PackageTips Name="BootstrapBlazor.Bluetooth" />
6+
<h4>@Localizer["BluetoothIntro"]</h4>
77

8-
<Tips class="mt-2">
9-
<p>@((MarkupString)Localizer["Tips"].Value)</p>
8+
<p>@((MarkupString)Localizer["BluetoothDescription"].Value)</p>
9+
10+
<Pre>[Inject, NotNull]
11+
private IBluetoothService? BluetoothService { get; set; }</Pre>
12+
13+
<Tips>
14+
<ul class="ul-demo">
15+
<li>@((MarkupString)Localizer["BluetoothTipsLi1"].Value)</li>
16+
<li>@((MarkupString)Localizer["BluetoothTipsLi2"].Value)</li>
17+
</ul>
18+
<div>@((MarkupString)Localizer["BluetoothTipsTitle"].Value)</div>
1019
</Tips>
1120

1221
<DemoBlock Title="@Localizer["BaseUsageTitle"]"
1322
Introduction="@Localizer["BaseUsageIntro"]"
1423
Name="Normal">
15-
@if (ShowUI)
16-
{
17-
<span>@Localizer["InnerUI"]</span>
24+
<div class="row form-inline g-3">
25+
<div class="col-12">
26+
<Button Text="@Localizer["BluetoothRequestText"]" Icon="fa-brands fa-bluetooth" OnClick="RequestDevice"></Button>
27+
<Button Text="@Localizer["BluetoothConnectText"]" Icon="fa-solid fa-circle-play" IsAsync="true" IsDisabled="@(_blueDevice is not {Connected: false})" IsKeepDisabled="true" OnClick="Connect" class="ms-3"></Button>
28+
<Button Text="@Localizer["BluetoothDisconnectText"]" Icon="fa-solid fa-circle-stop" IsDisabled="@(_blueDevice is not {Connected: true})" OnClick="Disconnect" class="ms-3"></Button>
29+
</div>
30+
<div class="col-12">
31+
<Display Value="@BluetoothDeviceName" ShowLabel="true" DisplayText="Device Name"></Display>
32+
</div>
33+
<div class="col-12">
34+
<div class="d-flex align-items-center">
35+
<Button Text="@Localizer["BluetoothGetBatteryText"]" Icon="fa-solid fa-battery-half" IsDisabled="@(_blueDevice is not { Connected: true })" OnClick="GetBatteryValue"></Button>
36+
<label class="d-flex align-items-center"><progress value="@_batteryValue" max="100" class="ms-3"></progress><span class="ms-3">@_batteryValueString</span></label>
37+
</div>
38+
</div>
39+
</div>
40+
</DemoBlock>
41+
42+
<p class="code-label mt-3">1. 服务注入</p>
43+
44+
<Pre>[Inject]
45+
[NotNull]
46+
private IBluetoothService? BluetoothService { get; set; }</Pre>
47+
48+
<p class="code-label">2. 列出蓝牙设备</p>
49+
<p>调用 <code>BluetoothService</code> 实例方法 <code>RequestDevice</code> 即可,通过 <code>IsSupport</code> 进行浏览器是否支持蓝牙</p>
50+
51+
<Pre>_serialPort = await BluetoothService.RequestDevice(["battery_service"]);
52+
if (BluetoothService.IsSupport == false)
53+
{
54+
await ToastService.Error(Localizer["NotSupportBluetoothTitle"], Localizer["NotSupportBluetoothContent"]);
55+
}</Pre>
1856

19-
<Printer OnResult="OnResult" ShowUI="true" OnError="OnError" OnUpdateStatus="OnUpdateStatus" OnUpdateError="OnError" OnGetDevices="OnGetDevices" />
57+
<p class="code-label">3. 连接设备</p>
58+
<p>调用 <code>IBluetoothDevice</code> 实例方法 <code>Connect</code> 即可</p>
2059

60+
<Pre>private async Task Connect()
61+
{
62+
if (_blueDevice != null)
63+
{
64+
var ret = await _blueDevice.Connect();
65+
if (ret == false && !string.IsNullOrEmpty(_blueDevice.ErrorMessage))
66+
{
67+
await ToastService.Error("Connect", _blueDevice.ErrorMessage);
68+
}
2169
}
22-
else
70+
}</Pre>
71+
72+
<p class="code-label">4. 断开设备</p>
73+
74+
<p>调用 <code>IBluetoothDevice</code> 实例方法 <code>Disconnect</code> 断开连接,请注意路由切换时,请调用其 <code>DisposeAsync</code> 方法释放资源</p>
75+
76+
<Pre>private async Task Disconnect()
77+
{
78+
if (_blueDevice != null)
2379
{
24-
<p>@Localizer["BasicUsage"]</p>
25-
<Printer @ref="printer" OnResult="OnResult" OnError="OnError" OnUpdateStatus="OnUpdateStatus" OnUpdateError="OnError" OnGetDevices="OnGetDevices" />
26-
<div>
27-
<button data-action="btnConnect" class="btn btn-outline-primary">@Localizer["ConnectButtonText"]</button>
28-
<button data-action="btnDisconnect" class="btn btn-outline-danger">@Localizer["DisconnectButtonText"]</button>
29-
<button data-action="tools" class="btn btn-outline-primary" @onclick="printer.Print">@Localizer["PrintButtonText"]</button>
30-
</div>
80+
var ret = await _blueDevice.Disconnect();
81+
if (ret == false && !string.IsNullOrEmpty(_blueDevice.ErrorMessage))
82+
{
83+
await ToastService.Error("Disconnect", _blueDevice.ErrorMessage);
84+
}
3185
}
32-
<hr />
33-
<pre>@message</pre>
34-
<pre style="color:green">@statusMessage</pre>
35-
<pre style="color:red">@errorMessage</pre>
36-
<p />
37-
<button class="btn btn-link" @onclick="SwitchUI ">@Localizer["SwitchUI"]</button>
38-
</DemoBlock>
86+
}</Pre>
3987

40-
<h4>@Localizer["BluetoothTitle"]</h4>
41-
42-
<DemoBlock Title="@Localizer["BluetoothBatteryLevelTitle"]"
43-
Introduction="@Localizer["BluetoothBatteryLevelIntro"]"
44-
Name="BatteryLevel">
45-
<button class="btn btn-outline-primary" @onclick="GetBatteryLevel ">@Localizer["GetBatteryLevelButtonText"]</button>
46-
<BatteryLevel @ref="batteryLevel" OnUpdateValue="OnUpdateValue" OnUpdateStatus="OnUpdateStatus" OnUpdateError="OnError" />
47-
<br />
48-
<progress max="100" value="@value"> @value % </progress>
49-
<pre>@message</pre>
50-
<pre style="color:green">@statusMessage</pre>
51-
<pre style="color:red">@errorMessage</pre>
52-
</DemoBlock>
88+
<p class="code-label">注意事项</p>
89+
<p>可以通过调用 <code>IBluetoothService</code> 实例方法 <code>GetAvailability</code> 方法后,判断其实例属性 <code>IsAvailable</code> 检查当前终端是否有蓝牙模块</p>
5390

54-
<h4>@Localizer["BluetoothHeartRate"]</h4>
55-
56-
<DemoBlock Title="@Localizer["BatteryLevelTitle"]"
57-
Introduction="@Localizer["BatteryLevelIntro"]"
58-
Name="HeartRate">
59-
<button class="btn btn-outline-primary" @onclick="GetHeartrate ">@Localizer["GetHeartrateButtonText"]</button>
60-
<button class="btn btn-outline-danger" @onclick="StopHeartrate ">@Localizer["StopHeartrateButtonText"]</button>
61-
<Heartrate @ref="heartrate" OnUpdateValue="OnUpdateValue" OnUpdateStatus="OnUpdateStatus" OnUpdateError="OnError" />
62-
<h3 style="color:red" data-action="heartrate"></h3>
63-
<pre>@message</pre>
64-
<pre style="color:green">@statusMessage</pre>
65-
<pre style="color:red">@errorMessage</pre>
66-
</DemoBlock>
91+
<p><code>IBluetoothService</code> 实例属性 <code>IsSupport</code> 是表示当前浏览器是否支持蓝牙功能</p>
92+
93+
<p><code>IBluetoothService</code> 与 <code>IBluetoothDevice</code> 所有实例方法均有返回值,可通过查看其实例属性 <code>ErrorMessage</code> 获得上一次执行的错误描述信息</p>
94+
95+
<p><code>IBluetoothDevice</code> 实例方法 <code>ReadValue</code> 是通用方法,通过参数指定<code>Services</code> 与 <code>Characteristics</code></p>
6796

68-
<AttributeTable Title="@Localizer["PrinterComponent"]" Items="@GetAttributes()" />
97+
<p>原生方法 <code>getDevices</code> 暂未封装,因为需要设置浏览器才能开启</p>
6998

70-
<AttributeTable Title="@Localizer["BatteryLevelComponent"]" Items="@GetAttributesBatteryLevel()" />
99+
<p>可根据自己的业务需求自定义扩展方法,内置扩展方法列表如下:</p>
71100

72-
<AttributeTable Title="@Localizer["HeartrateComponent"]" Items="@GetAttributesHeartrate()" />
101+
<ul class="ul-demo">
102+
<li><code>GetBatteryValue</code> 读取电量方法</li>
103+
</ul>
73104

74-
<AttributeTable Title="@Localizer["BluetoothDeviceClass"]" Items="@GetBluetoothDeviceAttributes()" />
105+
<p class="code-label">相关文档</p>
75106

76-
<AttributeTable Title="@Localizer["PrinterOptionClass"]" Items="@GetPrinterOptionAttributes()" />
107+
<ul class="ul-demo">
108+
<li>Service List:<a href="https://github.com/WebBluetoothCG/registries/blob/master/gatt_assigned_services.txt" target="_blank">[传送门]</a></li>
109+
<li>Characteristics List:<a href="https://github.com/WebBluetoothCG/registries/blob/master/gatt_assigned_characteristics.txt" target="_blank">[传送门]</a></li>
110+
<li>Descriptors List:<a href="https://github.com/WebBluetoothCG/registries/blob/master/gatt_assigned_descriptors.txt" target="_blank">[传送门]</a></li>
111+
</ul>

0 commit comments

Comments
 (0)