- Web Bluetooth を実装しているブラウザであれば、次のような非常に簡単なコードで Bluetooth デバイスをスキャンすることができます。
- 注意点として、ユーザのアクション(ボタンのクリック等)により、コードを実行する必要があります。(セキュリティの観点から、そのように仕様が定義されています。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebBleTest</title>
</head>
<body>
<button onclick="scan()">scan</button>
<script type="text/javascript">
function scan() {
navigator.bluetooth.requestDevice({
acceptAllDevices: true
})
.catch(error => console.log(error))
}
</script>
</body>
</html>