如何使用网络应用程序连接到蓝牙以播放音频?

时间:2018-03-24 18:45:43

标签: javascript bluetooth

我是JavaScript的蓝牙连接的新手。我想让我的网络应用程序连接到一个物理按钮,用于播放音频,它连接到带有电路板的蓝牙。但我的网络应用程序没有连接到主板。

我现在没有一块电路板,所以我首先尝试连接我的iPhone,所以我仍然无法连接它。我还有什么想念?

即使我可以连接,如何按下物理按钮一次并播放音频,然后再次按下它,它会播放下一个音频?

我的HTML:

<button id="bluetooth" stlye=display:none> Connect to bluetooth</button>

我的JavaScript:

  $("#bluetooth").on("click", function(){

        console.log('Searching Bluetooth Device...');

        navigator.bluetooth.requestDevice({
            acceptAllDevices: true,
            optionalServices: ['battery_service']
        })

        .then(device => { 
            console.log('aa: '+ device.gatt.connect())
            console.log('Name: ' + device.name);
            console.log('id ' + device.id);
            console.log('Connected: ' + device.gatt.connected);

            // Attempts to connect to remote GATT Server.
            return device.gatt.connect();
        })

        .catch(function(error) {
            console.log("Something went wrong. " + error);
        });
   });

这是来自控制台:

index.html:166 Searching Bluetooth Device...
index.html:177 aa: [object Promise]
index.html:178 Name: IPhone of Johnny
index.html:179 id HoC4uQMnDGuMRTWhj0HxKw==
index.html:180 Connected: false

0 个答案:

没有答案