首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Web安全连接与配对类型“Web”

Web安全连接与配对类型“Web”
EN

Stack Overflow用户
提问于 2022-03-23 16:09:20
回答 1查看 440关注 0票数 0

我有一个BLE设备,我希望通过网络蓝牙连接到。

BLE设备的特点是只在通过安全连接请求时才返回值,蓝牙配对类型必须是“才能工作”。

我正在使用Blazor和一个名为Blazm.Bluetooth的nuget,但是对此的任何纯javascript解决方案也是非常感谢的。https://github.com/EngstromJimmy/Blazm.Bluetooth

我能够让requestDevice模式显示在浏览器中,成功地配对到设备上,并设置一个通知处理程序来侦听特征值的变化。

当我试图从设备读取一个值时,我会得到一个响应代码,它告诉我我没有访问数据的权限,因为设备的设置只允许通过安全连接进行访问。

这使我得出结论,即尚未建立安全连接。

我能够使用Windows.Devices.Bluetooth库与设备(在另一个项目中)进行通信,因此我确信,一旦建立了安全连接,设备就可以正常工作。

请求代码:

代码语言:javascript
复制
export async function requestDevice(query)
{
    var objquery = JSON.parse(query);
    console.log(query);

    var device = await navigator.bluetooth.requestDevice(objquery);
    await device.gatt.connect();
    device.addEventListener('gattserverdisconnected', onDisconnected);
    PairedBluetoothDevices.push(device);
    return { "Name": device.name, "Id": device.id };    
}

function connect(bluetoothDevice) {
    exponentialBackoff(3 /* max retries */, 1 /* seconds delay */,
        function toTry() {
            time('Connecting to Bluetooth Device... ');
            
            return bluetoothDevice.gatt.connect();
        },
        function success() {
            console.log('> Bluetooth Device connected. Try disconnect it now.');
        },
        function fail() {
            time('Failed to reconnect.');
        });
}

编写特征代码:

代码语言:javascript
复制
export async function writeValue(deviceId, serviceId, characteristicId, value)
{
    var device = getDevice(deviceId);
    console.log(device);
    if (device.gatt.connected) {
        var service = await device.gatt.getPrimaryService(serviceId);
        var characteristic = await service.getCharacteristic(characteristicId);
        var b = Uint8Array.from(value);
        await characteristic.writeValueWithoutResponse(b);
    }
    else
    {
        await sleep(1000);
        await writeValue(deviceId, serviceId, characteristicId, value);
    }
}

我使用了BTVS和WireShark蓝牙嗅探器来确认所有请求/响应包都是字节相同的字节,从初始连接到写入响应给我“无访问”错误代码。

我最大的问题是,是否真的有可能通过Web建立安全连接?

我也对我可以尝试的代码建议感兴趣。

更新:我尝试在Windows设备和打印机中使用“添加设备”功能:

  • 在通过Web连接之前,将设备添加到windows中,结果如下:
代码语言:javascript
复制
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: GATT operation failed for unknown reason.
      undefined
Microsoft.JSInterop.JSException: GATT operation failed for unknown reason.
undefined
   at Microsoft.JSInterop.JSRuntime.<InvokeAsync>d__16`1[[Microsoft.JSInterop.Infrastructure.IJSVoidResult, Microsoft.JSInterop, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
   at Microsoft.JSInterop.JSObjectReferenceExtensions.InvokeVoidAsync(IJSObjectReference jsObjectReference, String identifier, Object[] args)
   at Blazm.Bluetooth.BluetoothNavigator.SetupNotifyAsync(Device device, String serviceId, String characteristicId)
   at BlazorWebAssemblySample.Pages.Counter.Connect() in C:\project.page.razor:line 82
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
  • 同样的情况下,如果不运行代码,则根本无法通过Web进行配对。
  • 首先通过Web进行配对,然后尝试在windows中“添加设备”,结果导致窗口找不到设备。
  • 一个“有趣的”观察是,如果我首先通过Web配对,这意味着特征值更改事件处理程序已经设置和侦听,然后通过基于Windows.Devices.Bluetooth的项目进行连接,那么该项目读取的所有值也将发送/被Web侦听器捕获,并在浏览器中看到。
EN

回答 1

Stack Overflow用户

发布于 2022-03-23 20:13:23

这个问题似乎与我们在https://bugs.chromium.org/p/chromium/issues/detail?id=1271239上看到的有点相似。作为一项试验,您能试着在使用Windows系统配对之前对设备进行配对吗?然后写到特征,看看它是否有效。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71590482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档