这个问题可能有很多人问过,但我没有运气通过研究得到答案。
我的最终计划是在智能手机中运行带有 Web 蓝牙 API 的 Web 应用程序,并使用 FLIC 按钮来控制音频播放。一键点击,播放一首音频。
我首先在我的 MAC 笔记本电脑和 iPhoneX 上测试该程序,因为我在想是否可以将它们连接起来,然后当我在智能手机中运行网络应用程序时,我就可以连接到 FLIC 按钮.
但是,我收到了这个错误。
出了点问题。 NotSupportedError:GATT 错误:不支持。
我错过了什么吗?我看到有人提到iPhone cannot connect Latop ,希望这不是真的
下面是代码:
$("#bluetooth").on("click", function(){
const controlServiceUUID = '00001805-0000-1000-8000-00805f9b34fb'; // Full UUID
const commandCharacteristicUUID = '00002a0f-0000-1000-8000-00805f9b34fb'; //
var myCharacteristic;
navigator.bluetooth.requestDevice({
acceptAllDevices: true,
optionalServices: [controlServiceUUID]
})
.then(device => {
console.log("Got device name: ", device.name);
console.log("id: ", device.id);
return device.gatt.connect();
console.log("Here");
})
.then(server => {
serverInstance = server;
console.log("Getting PrimaryService");
return server.getPrimaryService(controlServiceUUID);
})
.then(service => {
console.log("Getting Characteristic");
return service.getCharacteristic(commandCharacteristicUUID);
})
.then(characteristic => {
// 0x01,3,0x02,0x03,0x01
myCharacteristic = characteristic;
return myCharacteristic.startNotifications().then(_ => {
log('Notifications started');
myCharacteristic.addEventListener('characteristicvaluechanged', test);
});
})
.catch(function(error) {
console.log("Something went wrong. " + error);
});
function test(event) {
if (myCharacteristic) {
myCharacteristic.startNotifications()
.then(_ => {
console.log("Notification stopped!");
})
.catch(error => {
console.log("Argh!" + error);
});
}
}
});
最佳答案
网络蓝牙 API 仅适用于 ChromeOS 和 Android 6 或更高版本(带标志选项)。 (https://developer.mozilla.org/en-US/docs/Web/API/Web_Bluetooth_API)
关于Javascript-Web 蓝牙 API GATT 错误 : Not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49991644/