Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
61c8386
feat: 增加蓝牙接口
ArgoZhang Oct 19, 2024
11a628c
refactor: 移除可见性修饰符
ArgoZhang Oct 19, 2024
00bd1d6
chore: 增加蓝牙服务
ArgoZhang Oct 19, 2024
4037b65
doc: 更新蓝牙示例
ArgoZhang Oct 19, 2024
fb2184d
refactor: 增加 getAvaliablity 方法
ArgoZhang Oct 19, 2024
bab6ad9
feat: 增加 Request 方法
ArgoZhang Oct 19, 2024
535f85c
doc: 更新示例
ArgoZhang Oct 19, 2024
d1dc92b
feat: 更新蓝牙设备
ArgoZhang Oct 19, 2024
b04f7f5
doc: 更新示例
ArgoZhang Oct 19, 2024
a15593e
feat: 增加电量读取方法
ArgoZhang Oct 19, 2024
83bb0e5
doc: 更新示例
ArgoZhang Oct 19, 2024
07f678e
refactor: 移除 GetDevices 方法
ArgoZhang Oct 20, 2024
d5957b7
doc: 更新示例
ArgoZhang Oct 20, 2024
fdbc4e7
feat: 增加 IBluetoothDevice 接口
ArgoZhang Oct 20, 2024
5d82606
doc: 更新示例
ArgoZhang Oct 20, 2024
e0b391c
refactor: 更改为通用设计
ArgoZhang Oct 20, 2024
489ef3b
doc: 更新示例
ArgoZhang Oct 20, 2024
e892dfa
refactor: 精简代码
ArgoZhang Oct 20, 2024
f98261e
refactor: 更新读取方法
ArgoZhang Oct 20, 2024
3dc79ad
refactor: 更新读取数据方法
ArgoZhang Oct 20, 2024
8ea6c04
refactor: 更新接口
ArgoZhang Oct 20, 2024
a6d94ec
refactor: 更新代码
ArgoZhang Oct 20, 2024
d01255a
feat: 增加扩展方法
ArgoZhang Oct 20, 2024
0aecfa5
doc: 精简代码
ArgoZhang Oct 20, 2024
5f159f2
feat: 增加 ErrorMessage 参数
ArgoZhang Oct 20, 2024
8c34131
feat: 增加异常回调方法
ArgoZhang Oct 20, 2024
ff45392
feat: 增加 ErrorMessage 参数
ArgoZhang Oct 20, 2024
56deb5f
feat: 增加方法返回值
ArgoZhang Oct 20, 2024
ab56737
refactor: 增加 IAsyncDispose 接口
ArgoZhang Oct 20, 2024
1002d18
refactor: 更新脚本
ArgoZhang Oct 20, 2024
9f55ed3
doc: 增加异常逻辑
ArgoZhang Oct 20, 2024
fed770b
refactor: 代码重构
ArgoZhang Oct 20, 2024
66c0e71
refactor: 更新脚本使用 byte 通讯
ArgoZhang Oct 20, 2024
288db37
refactor: 更改为 byte 通讯
ArgoZhang Oct 20, 2024
3f3ff34
refactor: 重构蓝牙模块检查
ArgoZhang Oct 20, 2024
e21ea02
doc: 更新文档
ArgoZhang Oct 20, 2024
f4617d1
doc: 更新文档
ArgoZhang Oct 20, 2024
fb376cf
doc: 更新文档
ArgoZhang Oct 20, 2024
b814635
doc: 更新示例
ArgoZhang Oct 20, 2024
bb3c09d
doc: 更新文档
ArgoZhang Oct 20, 2024
aa27ccc
doc: 更新文档
ArgoZhang Oct 20, 2024
9ccdc0a
doc: 更新示例文档
ArgoZhang Oct 20, 2024
fbf8488
test: 更新单元测试
ArgoZhang Oct 20, 2024
4c101c3
test: 更新单元测试
ArgoZhang Oct 20, 2024
d38bf4c
test: 更新单元测试
ArgoZhang Oct 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions exclusion.dic
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ Validatable
noselect
Urls
Pharmacode
bluetooth
1 change: 0 additions & 1 deletion src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
<PackageReference Include="BootstrapBlazor.BaiduOcr" Version="8.2.0" />
<PackageReference Include="BootstrapBlazor.BarCode" Version="8.2.0" />
<PackageReference Include="BootstrapBlazor.BarcodeGenerator" Version="8.2.0" />
<PackageReference Include="BootstrapBlazor.Bluetooth" Version="8.0.2" />
<PackageReference Include="BootstrapBlazor.BootstrapIcon" Version="8.0.3" />
<PackageReference Include="BootstrapBlazor.BootstrapIcon.Extensions" Version="8.2.0" />
<PackageReference Include="BootstrapBlazor.Chart" Version="8.4.0" />
Expand Down
141 changes: 88 additions & 53 deletions src/BootstrapBlazor.Server/Components/Samples/Bluetooth.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,109 @@

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

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

<Tips class="mt-2">
<p>@((MarkupString)Localizer["Tips"].Value)</p>
<p>@((MarkupString)Localizer["BluetoothDescription"].Value)</p>

<Pre>[Inject, NotNull]
private IBluetoothService? BluetoothService { get; set; }</Pre>

<Tips>
<ul class="ul-demo">
<li>@((MarkupString)Localizer["BluetoothTipsLi1"].Value)</li>
<li>@((MarkupString)Localizer["BluetoothTipsLi2"].Value)</li>
</ul>
<div>@((MarkupString)Localizer["BluetoothTipsTitle"].Value)</div>
</Tips>

<DemoBlock Title="@Localizer["BaseUsageTitle"]"
Introduction="@Localizer["BaseUsageIntro"]"
Name="Normal">
@if (ShowUI)
{
<span>@Localizer["InnerUI"]</span>
<div class="row form-inline g-3">
<div class="col-12">
<Button Text="@Localizer["BluetoothRequestText"]" Icon="fa-brands fa-bluetooth" OnClick="RequestDevice"></Button>
<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>
<Button Text="@Localizer["BluetoothDisconnectText"]" Icon="fa-solid fa-circle-stop" IsDisabled="@(_blueDevice is not {Connected: true})" OnClick="Disconnect" class="ms-3"></Button>
</div>
<div class="col-12">
<Display Value="@BluetoothDeviceName" ShowLabel="true" DisplayText="Device Name"></Display>
</div>
<div class="col-12">
<div class="d-flex align-items-center">
<Button Text="@Localizer["BluetoothGetBatteryText"]" Icon="fa-solid fa-battery-half" IsDisabled="@(_blueDevice is not { Connected: true })" OnClick="GetBatteryValue"></Button>
<label class="d-flex align-items-center"><progress value="@_batteryValue" max="100" class="ms-3"></progress><span class="ms-3">@_batteryValueString</span></label>
</div>
</div>
</div>
</DemoBlock>

<p class="code-label mt-3">1. 服务注入</p>

<Pre>[Inject]
[NotNull]
private IBluetoothService? BluetoothService { get; set; }</Pre>

<p class="code-label">2. 列出蓝牙设备</p>
<p>调用 <code>BluetoothService</code> 实例方法 <code>RequestDevice</code> 即可,通过 <code>IsSupport</code> 进行浏览器是否支持蓝牙</p>

<Pre>_serialPort = await BluetoothService.RequestDevice(["battery_service"]);
if (BluetoothService.IsSupport == false)
{
await ToastService.Error(Localizer["NotSupportBluetoothTitle"], Localizer["NotSupportBluetoothContent"]);
}</Pre>

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

<Pre>private async Task Connect()
{
if (_blueDevice != null)
{
var ret = await _blueDevice.Connect();
if (ret == false && !string.IsNullOrEmpty(_blueDevice.ErrorMessage))
{
await ToastService.Error("Connect", _blueDevice.ErrorMessage);
}
}
else
}</Pre>

<p class="code-label">4. 断开设备</p>

<p>调用 <code>IBluetoothDevice</code> 实例方法 <code>Disconnect</code> 断开连接,请注意路由切换时,请调用其 <code>DisposeAsync</code> 方法释放资源</p>

<Pre>private async Task Disconnect()
{
if (_blueDevice != null)
{
<p>@Localizer["BasicUsage"]</p>
<Printer @ref="printer" OnResult="OnResult" OnError="OnError" OnUpdateStatus="OnUpdateStatus" OnUpdateError="OnError" OnGetDevices="OnGetDevices" />
<div>
<button data-action="btnConnect" class="btn btn-outline-primary">@Localizer["ConnectButtonText"]</button>
<button data-action="btnDisconnect" class="btn btn-outline-danger">@Localizer["DisconnectButtonText"]</button>
<button data-action="tools" class="btn btn-outline-primary" @onclick="printer.Print">@Localizer["PrintButtonText"]</button>
</div>
var ret = await _blueDevice.Disconnect();
if (ret == false && !string.IsNullOrEmpty(_blueDevice.ErrorMessage))
{
await ToastService.Error("Disconnect", _blueDevice.ErrorMessage);
}
}
<hr />
<pre>@message</pre>
<pre style="color:green">@statusMessage</pre>
<pre style="color:red">@errorMessage</pre>
<p />
<button class="btn btn-link" @onclick="SwitchUI ">@Localizer["SwitchUI"]</button>
</DemoBlock>
}</Pre>

<h4>@Localizer["BluetoothTitle"]</h4>

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

<h4>@Localizer["BluetoothHeartRate"]</h4>

<DemoBlock Title="@Localizer["BatteryLevelTitle"]"
Introduction="@Localizer["BatteryLevelIntro"]"
Name="HeartRate">
<button class="btn btn-outline-primary" @onclick="GetHeartrate ">@Localizer["GetHeartrateButtonText"]</button>
<button class="btn btn-outline-danger" @onclick="StopHeartrate ">@Localizer["StopHeartrateButtonText"]</button>
<Heartrate @ref="heartrate" OnUpdateValue="OnUpdateValue" OnUpdateStatus="OnUpdateStatus" OnUpdateError="OnError" />
<h3 style="color:red" data-action="heartrate"></h3>
<pre>@message</pre>
<pre style="color:green">@statusMessage</pre>
<pre style="color:red">@errorMessage</pre>
</DemoBlock>
<p><code>IBluetoothService</code> 实例属性 <code>IsSupport</code> 是表示当前浏览器是否支持蓝牙功能</p>

<p><code>IBluetoothService</code> 与 <code>IBluetoothDevice</code> 所有实例方法均有返回值,可通过查看其实例属性 <code>ErrorMessage</code> 获得上一次执行的错误描述信息</p>

<p><code>IBluetoothDevice</code> 实例方法 <code>ReadValue</code> 是通用方法,通过参数指定<code>Services</code> 与 <code>Characteristics</code></p>

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

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

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

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

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