|
3 | 3 |
|
4 | 4 | <h3>@Localizer["BluetoothTitle"]</h3> |
5 | 5 |
|
6 | | -<PackageTips Name="BootstrapBlazor.Bluetooth" /> |
| 6 | +<h4>@Localizer["BluetoothIntro"]</h4> |
7 | 7 |
|
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> |
10 | 19 | </Tips> |
11 | 20 |
|
12 | 21 | <DemoBlock Title="@Localizer["BaseUsageTitle"]" |
13 | 22 | Introduction="@Localizer["BaseUsageIntro"]" |
14 | 23 | 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> |
18 | 56 |
|
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> |
20 | 59 |
|
| 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 | + } |
21 | 69 | } |
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) |
23 | 79 | { |
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 | + } |
31 | 85 | } |
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> |
39 | 87 |
|
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> |
53 | 90 |
|
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> |
67 | 96 |
|
68 | | -<AttributeTable Title="@Localizer["PrinterComponent"]" Items="@GetAttributes()" /> |
| 97 | +<p>原生方法 <code>getDevices</code> 暂未封装,因为需要设置浏览器才能开启</p> |
69 | 98 |
|
70 | | -<AttributeTable Title="@Localizer["BatteryLevelComponent"]" Items="@GetAttributesBatteryLevel()" /> |
| 99 | +<p>可根据自己的业务需求自定义扩展方法,内置扩展方法列表如下:</p> |
71 | 100 |
|
72 | | -<AttributeTable Title="@Localizer["HeartrateComponent"]" Items="@GetAttributesHeartrate()" /> |
| 101 | +<ul class="ul-demo"> |
| 102 | + <li><code>GetBatteryValue</code> 读取电量方法</li> |
| 103 | +</ul> |
73 | 104 |
|
74 | | -<AttributeTable Title="@Localizer["BluetoothDeviceClass"]" Items="@GetBluetoothDeviceAttributes()" /> |
| 105 | +<p class="code-label">相关文档</p> |
75 | 106 |
|
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