Skip to content

Commit 259b6f2

Browse files
authored
feat(IBluetoothService): add BluetoothFilterOption parameter (#4496)
* feat: 增加 BluetoothRequestOptions 参数 * refactor: 更新脚本 * doc: 更新示例 * doc: 更新示例 * test: 更新单元测试 * chore: bump version 8.10.4 * test: 更新单元测试
1 parent 697fdea commit 259b6f2

File tree

11 files changed

+222
-14
lines changed

11 files changed

+222
-14
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ private IBluetoothService? BluetoothService { get; set; }</Pre>
4646
private IBluetoothService? BluetoothService { get; set; }</Pre>
4747

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

51-
<Pre>_serialPort = await BluetoothService.RequestDevice(["battery_service"]);
51+
<Pre>_serialPort = await BluetoothService.RequestDevice();
5252
if (BluetoothService.IsSupport == false)
5353
{
5454
await ToastService.Error(Localizer["NotSupportBluetoothTitle"], Localizer["NotSupportBluetoothContent"]);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public partial class Bluetooth
2525

2626
private async Task RequestDevice()
2727
{
28-
_blueDevice = await BluetoothService.RequestDevice(["battery_service"]);
28+
_blueDevice = await BluetoothService.RequestDevice();
2929
if (BluetoothService.IsSupport == false)
3030
{
3131
await ToastService.Error(Localizer["NotSupportBluetoothTitle"], Localizer["NotSupportBluetoothContent"]);

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>8.10.4-beta03</Version>
4+
<Version>8.10.4</Version>
55
</PropertyGroup>
66

77
<ItemGroup>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using System.Text.Json.Serialization;
6+
7+
namespace BootstrapBlazor.Components;
8+
9+
/// <summary>
10+
/// BluetoothFilter 类
11+
/// </summary>
12+
public class BluetoothFilter
13+
{
14+
/// <summary>
15+
/// An array of values indicating the Bluetooth GATT (Generic Attribute Profile) services that a Bluetooth device must support. Each value can be a valid name from the GATT assigned services list, such as 'battery_service' or 'blood_pressure'. You can also pass a full service UUID such as '0000180F-0000-1000-8000-00805f9b34fb' or the short 16-bit (0x180F) or 32-bit alias. Note that these are the same values that can be passed to BluetoothUUID.getService().
16+
/// </summary>
17+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
18+
public List<string>? Services { get; set; }
19+
20+
/// <summary>
21+
/// A string containing the precise name of the device to match against.
22+
/// </summary>
23+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
24+
public string? Name { get; set; }
25+
26+
/// <summary>
27+
/// A string containing the name prefix to match against. All devices that have a name starting with this string will be matched.
28+
/// </summary>
29+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
30+
public string? NamePrefix { get; set; }
31+
32+
/// <summary>
33+
/// An array of objects matching against manufacturer data in the Bluetooth Low Energy (BLE) advertising packets.
34+
/// </summary>
35+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
36+
public List<BluetoothManufacturerDataFilter>? ManufacturerData { get; set; }
37+
38+
/// <summary>
39+
/// An array of objects matching against service data in the Bluetooth Low Energy (BLE) advertising packets.
40+
/// </summary>
41+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
42+
public List<BluetoothServiceDataFilter>? ServiceData { get; set; }
43+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using System.Text.Json.Serialization;
6+
7+
namespace BootstrapBlazor.Components;
8+
9+
/// <summary>
10+
/// BluetoothManufacturerDataFilter 配置类
11+
/// </summary>
12+
public class BluetoothManufacturerDataFilter
13+
{
14+
/// <summary>
15+
/// A mandatory number identifying the manufacturer of the device. Company identifiers are listed in the Bluetooth specification Assigned numbers, Section 7. For example, to match against devices manufactured by "Digianswer A/S", with assigned hex number 0x000C, you would specify 12.
16+
/// </summary>
17+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
18+
public int? CompanyIdentifier { get; set; }
19+
20+
/// <summary>
21+
/// The data prefix. A buffer containing values to match against the values at the start of the advertising manufacturer data.
22+
/// </summary>
23+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
24+
public string? DataPrefix { get; set; }
25+
26+
/// <summary>
27+
/// This allows you to match against bytes within the manufacturer data, by masking some bytes of the service data dataPrefix.
28+
/// </summary>
29+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
30+
public string? Mask { get; set; }
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using System.Text.Json.Serialization;
6+
7+
namespace BootstrapBlazor.Components;
8+
9+
/// <summary>
10+
/// BluetoothRequestOptions 参数类
11+
/// </summary>
12+
public class BluetoothRequestOptions
13+
{
14+
/// <summary>
15+
/// An array of filter objects indicating the properties of devices that will be matched. To match a filter object, a device must match all the values of the filter: all its specified services, name, namePrefix, and so on
16+
/// </summary>
17+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
18+
public List<BluetoothFilter>? Filters { get; set; }
19+
20+
/// <summary>
21+
/// An array of filter objects indicating the characteristics of devices that will be excluded from matching. The properties of the array elements are the same as for <see cref="Filters"/>.
22+
/// </summary>
23+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
24+
public List<BluetoothFilter>? ExclusionFilters { get; set; }
25+
26+
/// <summary>
27+
/// An array of optional service identifiers.
28+
/// </summary>
29+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
30+
public List<string>? OptionalServices { get; set; }
31+
32+
/// <summary>
33+
/// An optional array of integer manufacturer codes. This takes the same values as companyIdentifier.
34+
/// </summary>
35+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
36+
public List<string>? OptionalManufacturerData { get; set; }
37+
38+
/// <summary>
39+
/// A boolean value indicating that the requesting script can accept all Bluetooth devices. The default is false.
40+
/// </summary>
41+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
42+
public bool AcceptAllDevices { get; set; }
43+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using System.Text.Json.Serialization;
6+
7+
namespace BootstrapBlazor.Components;
8+
9+
/// <summary>
10+
/// BluetoothServiceDataFilter 配置类
11+
/// </summary>
12+
public class BluetoothServiceDataFilter
13+
{
14+
/// <summary>
15+
/// The GATT service name, the service UUID, or the UUID 16-bit or 32-bit form. This takes the same values as the elements of the services array.
16+
/// </summary>
17+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
18+
public string? Service { get; set; }
19+
20+
/// <summary>
21+
/// The data prefix. A buffer containing values to match against the values at the start of the advertising service data.
22+
/// </summary>
23+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
24+
public string? DataPrefix { get; set; }
25+
26+
/// <summary>
27+
/// This allows you to match against bytes within the manufacturer data, by masking some bytes of the service data dataPrefix.
28+
/// </summary>
29+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
30+
public string? Mask { get; set; }
31+
}

src/BootstrapBlazor/Services/Bluetooth/DefaultBluetoothService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ public async Task<bool> GetAvailability(CancellationToken token = default)
7070
/// <summary>
7171
/// <inheritdoc />
7272
/// </summary>
73-
public async Task<IBluetoothDevice?> RequestDevice(string[] optionalServices, CancellationToken token = default)
73+
public async Task<IBluetoothDevice?> RequestDevice(BluetoothRequestOptions? options = null, CancellationToken token = default)
7474
{
7575
_module ??= await LoadModule();
7676

7777
BluetoothDevice? device = null;
7878
if (IsSupport)
7979
{
8080
ErrorMessage = null;
81-
var parameters = await _module.InvokeAsync<string[]?>("requestDevice", token, _deviceId, optionalServices, _interop, nameof(OnError));
81+
var parameters = await _module.InvokeAsync<string[]?>("requestDevice", token, _deviceId, options, _interop, nameof(OnError));
8282
if (parameters != null)
8383
{
8484
device = new BluetoothDevice(_module, _deviceId, parameters);

src/BootstrapBlazor/Services/Bluetooth/IBluetoothService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public interface IBluetoothService
3333
/// <summary>
3434
/// 请求蓝牙配对方法
3535
/// </summary>
36-
/// <param name="optionalServices">请求服务列表 请参考 https://github.com/WebBluetoothCG/registries/blob/master/gatt_assigned_services.txt</param>
36+
/// <param name="options"><see cref="BluetoothRequestOptions"/> 实例</param>
3737
/// <param name="token"></param>
3838
/// <returns></returns>
39-
Task<IBluetoothDevice?> RequestDevice(string[] optionalServices, CancellationToken token = default);
39+
Task<IBluetoothDevice?> RequestDevice(BluetoothRequestOptions? options = null, CancellationToken token = default);
4040
}

src/BootstrapBlazor/wwwroot/modules/bt.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export async function getAvailability() {
1212
return ret;
1313
}
1414

15-
export async function requestDevice(id, optionalServices, invoke, method) {
15+
export async function requestDevice(id, options, invoke, method) {
1616
let ret = await getAvailability();
1717
if (ret === false) {
1818
return null;
@@ -22,9 +22,8 @@ export async function requestDevice(id, optionalServices, invoke, method) {
2222
const bt = { device: null };
2323
Data.set(id, bt);
2424
try {
25-
const ret = await navigator.bluetooth.requestDevice({
26-
acceptAllDevices: true,
27-
optionalServices: optionalServices
25+
const ret = await navigator.bluetooth.requestDevice(options ?? {
26+
acceptAllDevices: true
2827
});
2928
bt.device = ret;
3029
device = [ret.name, ret.id];

0 commit comments

Comments
 (0)