【发布时间】:2021-01-02 00:14:00
【问题描述】:
在我的 ESP32 设备上使用“Web 蓝牙 API”连接到 BLE 服务器时,我遇到了一个奇怪的行为。
我可以在桌面上使用 chrome 浏览器连接和发送/接收数据,但在我的 android 手机上使用 chrome 浏览器时,我无法将数据发送到设备上的 BLE 服务器。
我不知道是什么原因。桌面和移动浏览器上的编码/解码是否不同,一些权限问题或 json 字符串以不同的方式发送,或者 移动浏览器发送的数据长度是否存在一些限制,因为我能够发送简单的“hello world” " 来自移动浏览器的字符串,但我无法发送“hello world hello world hello world hello world”?
我的代码如下:
btnconnect.addEventListener('click', submitted);
var wifissid;
var wifipw;
var myjson;
var decoder = new TextDecoder('utf-8');
var encoder = new TextEncoder('utf-8');
var characteristic1;
async function submitted() {
wifissid = document.getElementById("ssid").value;
wifipw = document.getElementById("pw").value;
xdid = document.getElementById("xdid").value;
var obj = {
ssid: wifissid,
pw: wifipw,
did: xdid,
};
myjson = JSON.stringify(obj);
try {
const device = await navigator.bluetooth.requestDevice({
filters: [{
name: 'DEVICENAME'
}],
optionalServices: [‘xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx’]
});
const server = await device.gatt.connect();
const service = await server.getPrimaryService(‘xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx’);
characteristic1 = await service.getCharacteristic(‘yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy’);
await characteristic1.startNotifications();
characteristic1.addEventListener('characteristicvaluechanged',
handleNotifications);
let valuesent = encoder.encode(myjson);
await characteristic1.writeValue(valuesent);
} catch (error) {
console.log( error);
}
}
async function handleNotifications(event) {
let value = await event.target.value;
let valDecoded = decoder.decode(value);
if (valDecoded.includes("success")) {
// Do something
}
if (valDecoded.includes("failed")) {
// Do something
}}
【问题讨论】:
-
您能否提供如chromium.org/developers/how-tos/file-web-bluetooth-bugs 中所述的 Android 日志以帮助诊断此问题?
标签: bluetooth-lowenergy web-bluetooth android-chrome