我有一个BLE设备,我希望通过网络蓝牙连接到。
BLE设备的特点是只在通过安全连接请求时才返回值,蓝牙配对类型必须是“才能工作”。
我正在使用Blazor和一个名为Blazm.Bluetooth的nuget,但是对此的任何纯javascript解决方案也是非常感谢的。https://github.com/EngstromJimmy/Blazm.Bluetooth
我能够让requestDevice模式显示在浏览器中,成功地配对到设备上,并设置一个通知处理程序来侦听特征值的变化。
当我试图从设备读取一个值时,我会得到一个响应代码,它告诉我我没有访问数据的权限,因为设备的设置只允许通过安全连接进行访问。
这使我得出结论,即尚未建立安全连接。
我能够使用Windows.Devices.Bluetooth库与设备(在另一个项目中)进行通信,因此我确信,一旦建立了安全连接,设备就可以正常工作。
请求代码:
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.');
});
}编写特征代码:
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设备和打印机中使用“添加设备”功能:
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)发布于 2022-03-23 20:13:23
这个问题似乎与我们在https://bugs.chromium.org/p/chromium/issues/detail?id=1271239上看到的有点相似。作为一项试验,您能试着在使用Windows系统配对之前对设备进行配对吗?然后写到特征,看看它是否有效。
https://stackoverflow.com/questions/71590482
复制相似问题