Skip to content

Commit bb3c09d

Browse files
committed
doc: 更新文档
1 parent b814635 commit bb3c09d

File tree

3 files changed

+46
-127
lines changed

3 files changed

+46
-127
lines changed

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

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,59 +33,60 @@ private IBluetoothService? BluetoothService { get; set; }</Pre>
3333
<label class="d-flex align-items-center"><progress value="@_batteryValue" max="100" class="ms-3"></progress><span class="ms-3">@_batteryValueString</span></label>
3434
</div>
3535
</div>
36-
<div class="col-12">
37-
<Button Text="@Localizer["BluetoothGetHeartRateText"]" Icon="fa-solid fa-heart-pulse" IsDisabled="@(_blueDevice is not { Connected: true })" OnClick="GetHeartRateValue"></Button>
38-
</div>
3936
</div>
4037
</DemoBlock>
4138

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

4441
<Pre>[Inject]
4542
[NotNull]
46-
private ISerialService? SerialService { get; set; }</Pre>
43+
private IBluetoothService? BluetoothService { get; set; }</Pre>
4744

48-
<p class="code-label">2. 申请串口权限</p>
49-
<p>调用 <code>SerialService</code> 实例方法 <code>GetPort</code> 即可,通过 <code>IsSupport</code> 进行浏览器是否支持判断</p>
45+
<p class="code-label">2. 列出蓝牙设备</p>
46+
<p>调用 <code>BluetoothService</code> 实例方法 <code>RequestDevice</code> 即可,通过 <code>IsSupport</code> 进行浏览器是否支持蓝牙</p>
5047

51-
<Pre>_serialPort = await SerialService.GetPort();
52-
if (SerialService.IsSupport == false)
48+
<Pre>_serialPort = await BluetoothService.RequestDevice(["battery_service"]);
49+
if (BluetoothService.IsSupport == false)
5350
{
54-
await ToastService.Error(Localizer["NotSupportSerialTitle"], Localizer["NotSupportSerialContent"]);
51+
await ToastService.Error(Localizer["NotSupportBluetoothTitle"], Localizer["NotSupportBluetoothContent"]);
5552
}</Pre>
5653

57-
<p class="code-label">3. 打开串口</p>
54+
<p class="code-label">3. 连接设备</p>
55+
<p>调用 <code>IBluetoothDevice</code> 实例方法 <code>Connect</code> 即可</p>
5856

59-
<ul class="ul-demo">
60-
<li>如果需要读取数据,请先设置 <code>ISerialPort</code> 实例 <code>DataReceive</code> 参数</li>
61-
<li>调用 <code>ISerialPort</code> 实例方法 <code>Open</code> 打开串口,可通过 <code>SerialPortOptions</code> 参数设置 <code>波特率</code> 等信息</li>
62-
</ul>
63-
64-
<Pre>private async Task OpenPort()
57+
<Pre>private async Task Connect()
6558
{
66-
if (_serialPort != null)
59+
if (_blueDevice != null)
6760
{
68-
_serialPort.DataReceive = async data =>
61+
var ret = await _blueDevice.Connect();
62+
if (ret == false && !string.IsNullOrEmpty(_blueDevice.ErrorMessage))
6963
{
70-
_messages.Add(new ConsoleMessageItem()
71-
{
72-
IsHtml = true,
73-
Message = $"{DateTime.Now}: --> Text: {Encoding.ASCII.GetString(data)} HEX: {Convert.ToHexString(data)}"
74-
});
75-
await InvokeAsync(StateHasChanged);
76-
};
77-
await _serialPort.Open(_serialOptions);
64+
await ToastService.Error("Connect", _blueDevice.ErrorMessage);
65+
}
7866
}
7967
}</Pre>
8068

81-
<p class="code-label">4. 关闭串口</p>
69+
<p class="code-label">4. 断开设备</p>
8270

83-
<p>调用 <code>ISerialPort</code> 实例方法 <code>Close</code> 关闭串口,请注意路由切换时,请调用其 <code>DisposeAsync</code> 方法释放资源</p>
71+
<p>调用 <code>IBluetoothDevice</code> 实例方法 <code>Disconnect</code> 断开连接,请注意路由切换时,请调用其 <code>DisposeAsync</code> 方法释放资源</p>
8472

85-
<Pre>private async Task ClosePort()
73+
<Pre>private async Task Disconnect()
8674
{
87-
if (_serialPort != null)
75+
if (_blueDevice != null)
8876
{
89-
await _serialPort.Close();
77+
var ret = await _blueDevice.Disconnect();
78+
if (ret == false && !string.IsNullOrEmpty(_blueDevice.ErrorMessage))
79+
{
80+
await ToastService.Error("Disconnect", _blueDevice.ErrorMessage);
81+
}
9082
}
9183
}</Pre>
84+
85+
<p class="code-label">注意事项</p>
86+
<p>可以通过调用 <code>IBluetoothService</code> 实例方法 <code>GetAvailability</code> 方法后,判断其实例属性 <code>IsAvailable</code> 检查当前终端是否有蓝牙模块</p>
87+
88+
<p><code>IBluetoothService</code> 实例属性 <code>IsSupport</code> 是表示当前浏览器是否支持蓝牙功能</p>
89+
90+
<p><code>IBluetoothService</code> 与 <code>IBluetoothDevice</code> 所有实例方法均有返回值,可通过查看其实例属性 <code>ErrorMessage</code> 获得上一次执行的错误描述信息</p>
91+
92+
<p><code>IBluetoothDevice</code> 实例方法 <code>ReadValue</code> 是通用方法,通过参数指定<code>Services</code> 与 <code>Characteristics</code></p>

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5888,55 +5888,20 @@
58885888
},
58895889
"BootstrapBlazor.Server.Components.Samples.Bluetooth": {
58905890
"BluetoothTitle": "IBluetooth Service ",
5891+
"BluetoothIntro": "Provides methods to query Bluetooth availability and request access to devices",
5892+
"BluetoothDescription": "Allows websites to communicate with Bluetooth devices connected to the user's computer. For example, you can connect to a Bluetooth printer to print.",
5893+
"BluetoothTipsLi1": "This feature is available only in secure contexts (HTTPS)",
5894+
"BluetoothTipsLi2": "This is an experimental technology Check the <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth#browser_compatibility\" target=\"_blank\">Browser compatibility table</a> carefully before using this in production",
5895+
"BluetoothTipsTitle": "Note: The <code>IBluetoothService</code> interface instance inherits <code>IAsyncDisposable</code>. When switching routes, you need to release its resources by calling its <code>DisposeAsync</code>.",
5896+
"NotSupportBluetoothTitle": "Scan Devices",
5897+
"NotSupportBluetoothContent": "The current browser does not support serial port operations. Please change to Edge or Chrome browser.",
5898+
"BluetoothRequestText": "Scan",
5899+
"BluetoothConnectText": "Connect",
5900+
"BluetoothDisconnectText": "Disconnect",
5901+
"BluetoothGetBatteryText": "Battery",
5902+
"BluetoothGetHeartRateText": "HeartRate",
58915903
"BaseUsageTitle": "Basic usage",
5892-
"BaseUsageIntro": "After connecting the device, perform operations such as printing, and write the corresponding commands(ESC/POS/CPCL) according to the characteristics of the printer",
5893-
"BluetoothBatteryLevelTitle": "Bluetooth Battery Level",
5894-
"BluetoothBatteryLevelIntro": "Click to connect device",
5895-
"ConnectButtonText": "Connect",
5896-
"DisconnectButtonText": "Disconnect",
5897-
"ReconnectButtonText": "Reconnect",
5898-
"PrintButtonText": "Print",
5899-
"Url1": "1. Printer, for printable label/barcode/QR code",
5900-
"BluetoothsTitle2": "Bluetooth Heart Rate",
5901-
"GetHeartrateButtonText": "Get heart rate",
5902-
"StopHeartrateButtonText": "Stop heart rate",
5903-
"Url2": "2. Heart Rate",
5904-
"BluetoothHeartRate": "Bluetooth Heart Rate Band",
5905-
"BatteryLevelTitle": "Heart rate monitor",
5906-
"BatteryLevelIntro": "Connect the heart rate band",
5907-
"GetBatteryLevelButtonText": "Get Battery Level",
5908-
"Url3": "3. Battery Level",
5909-
"Tips": "ServiceUUID, Default <code>0xff00</code><br/>Common Printers ServiceUUID:<br/><code>0000ff00-0000-1000-8000-00805f9b34fb</code><br/><code>e7810a71-73ae-499d-8c15-faa9aef0c3f2</code><br/><code>0000fee7-0000-1000-8000-00805f9b34fb</code><br/>Set up component services UUID : <code>printer.Opt.ServiceUuid=?</code>",
5910-
"InnerUI": "Built-in UI",
5911-
"BasicUsage": "Basic usage",
5912-
"PrinterComponent": "Printer Component",
5913-
"BatteryLevelComponent": "BatteryLevel Component",
5914-
"HeartrateComponent": "Heartrate Component",
5915-
"BluetoothDeviceClass": "BluetoothDevice Class",
5916-
"PrinterOptionClass": "PrinterOption Class",
5917-
"DeviceBattery": "Device power {0}%",
5918-
"CommandsAttr": "Print instructions (cpcl/esp/pos code)",
5919-
"PrintAttr": "Print",
5920-
"OnUpdateStatusAttr": "Status update callback method",
5921-
"OnUpdateErrorAttr": "Error update callback method",
5922-
"PrinterElementAttr": "The reference object for UI interface elements, if left blank, the entire page will be used",
5923-
"OptAttr": "Printer Options",
5924-
"ShowUIAttr": "Obtain/Set Display Built-in UI",
5925-
"DebugAttr": "Obtain/Set Display Log",
5926-
"DeviceNameAttr": "Obtain/Set Device Name",
5927-
"NamePrefixAttr": "Initial search for device name prefix, default to null",
5928-
"MaxChunkAttr": "Data slice size, default to 100",
5929-
"NameAttr": "Device Name",
5930-
"ValueAttr": "Device value: such as heart rate/battery level%",
5931-
"StatusAttr": "Status",
5932-
"ErrorAttr": "Error",
5933-
"GetBatteryLevelAttr": "Query battery level",
5934-
"OnUpdateValueAttr": "Numerical update callback method",
5935-
"BatteryLevelElementAttr": "Reference objects for UI interface elements",
5936-
"GetHeartrateAttr": "Connect the heart rate band",
5937-
"StopHeartrateAttr": "Stop monitoring heart rate",
5938-
"HeartrateElementAttr": "Reference objects for UI interface elements",
5939-
"SwitchUI": "Switch UI"
5904+
"BaseUsageIntro": "Request communication with Bluetooth devices through the <code>IBluetoothService</code> service"
59405905
},
59415906
"BootstrapBlazor.Server.Components.Samples.FileIcons": {
59425907
"Title": "File Icon",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5901,54 +5901,7 @@
59015901
"BluetoothGetBatteryText": "读取电量",
59025902
"BluetoothGetHeartRateText": "读取心率",
59035903
"BaseUsageTitle": "基础用法",
5904-
"BaseUsageIntro": "通过 <code>IBluetoothService</code> 服务,请求与蓝牙设备通讯",
5905-
"BluetoothBatteryLevelTitle": "蓝牙设备电量",
5906-
"BluetoothBatteryLevelIntro": "点击连接设备",
5907-
"ConnectButtonText": "连接",
5908-
"DisconnectButtonText": "断开",
5909-
"ReconnectButtonText": "重连",
5910-
"PrintButtonText": "打印",
5911-
"Url1": "1. 蓝牙打印机 Printer,可打印标签/条码/QR码",
5912-
"BluetoothsTitle2": "Bluetooth Heart Rate 蓝牙心率带",
5913-
"GetHeartrateButtonText": "查询心率",
5914-
"StopHeartrateButtonText": "停止读取",
5915-
"Url2": "2. 蓝牙心率带 Heart Rate",
5916-
"BluetoothHeartRate": "蓝牙心率带",
5917-
"BatteryLevelTitle": "心率带",
5918-
"BatteryLevelIntro": "连接心率带",
5919-
"GetBatteryLevelButtonText": "查询电量",
5920-
"Url3": "3. 蓝牙设备电量 Battery Level",
5921-
"Tips": "服务UUID/ServiceUUID, 默认0xff00<br/>常见打印机ServiceUUID:<br/><code>0000ff00-0000-1000-8000-00805f9b34fb</code><br/><code>e7810a71-73ae-499d-8c15-faa9aef0c3f2</code><br/><code>0000fee7-0000-1000-8000-00805f9b34fb</code><br/>设置组件服务UUID : <code>printer.Opt.ServiceUuid=?</code>",
5922-
"InnerUI": "内置UI",
5923-
"BasicUsage": "基本用法",
5924-
"PrinterComponent": "Printer 组件",
5925-
"BatteryLevelComponent": "BatteryLevel 组件",
5926-
"HeartrateComponent": "Heartrate 组件",
5927-
"BluetoothDeviceClass": "BluetoothDevice 类",
5928-
"PrinterOptionClass": "PrinterOption 类",
5929-
"DeviceBattery": "设备电量{0}%",
5930-
"CommandsAttr": "打印指令(cpcl/esp/pos代码)",
5931-
"PrintAttr": "打印",
5932-
"OnUpdateStatusAttr": "状态更新回调方法",
5933-
"OnUpdateErrorAttr": "错误更新回调方法",
5934-
"PrinterElementAttr": "UI界面元素的引用对象,为空则使用整个页面",
5935-
"OptAttr": "打印机选项",
5936-
"ShowUIAttr": "获得/设置 显示内置UI",
5937-
"DebugAttr": "获得/设置 显示Log",
5938-
"DeviceNameAttr": "获得/设置 设备名称",
5939-
"NamePrefixAttr": "初始搜索设备名称前缀,默认 null",
5940-
"MaxChunkAttr": "数据切片大小,默认100",
5941-
"NameAttr": "设备名称",
5942-
"ValueAttr": "设备数值:例如心率/电量%",
5943-
"StatusAttr": "状态",
5944-
"ErrorAttr": "错误",
5945-
"GetBatteryLevelAttr": "查询电量",
5946-
"OnUpdateValueAttr": "数值更新回调方法",
5947-
"BatteryLevelElementAttr": "UI界面元素的引用对象",
5948-
"GetHeartrateAttr": "连接心率带",
5949-
"StopHeartrateAttr": "停止监听心率",
5950-
"HeartrateElementAttr": "UI界面元素的引用对象",
5951-
"SwitchUI": "切换界面"
5904+
"BaseUsageIntro": "通过 <code>IBluetoothService</code> 服务,请求与蓝牙设备通讯"
59525905
},
59535906
"BootstrapBlazor.Server.Components.Samples.FileIcons": {
59545907
"Title": "File Icon 文件图标",

0 commit comments

Comments
 (0)