webBluetoothとPythonのBluetoothライブラリについて
解決したいこと
webBluetoothを利用してMicroPythonでコーディングしたbleデバイスと接続をしたいのですが、ペイロードに埋め込んだ値をうまく読み込めず、ペアリング画面では不明またはサポートされていないデバイスと表示され、ペアリングを行っても値が入っていません。
スマホアプリのBLEScannerで確認したところMicroPythonで設定した値はうまく入っているように見えるのですが、JS側だと入っていないように見えます。
どのように修正したら良いのでしょうか?
発生している問題・エラー
js
接続成功: null Test2.js:21
Py
MPY: soft reboot
接続
ソースコード
ペリフェラル
import bluetooth
import time
import machine
from ble_advertising import advertising_payload
from micropython import const
from machine import Pin
flg1 = 0
flg2 = 0
char1 = "a"
_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
_IRQ_GATTS_WRITE = const(3)
_IRQ_GATTS_INDICATE_DONE = const(20)
_FLAG_READ = const(0x0002)
_FLAG_WRITE = const(0x0008)
_FLAG_NOTIFY = const(0x0010)
_FLAG_INDICATE = const(0x0020)
_ENV_SENSE_UUID = bluetooth.UUID(0x000a)
_SERVICE_CHAR = (
bluetooth.UUID(0x000b),
_FLAG_READ| _FLAG_WRITE | _FLAG_NOTIFY | _FLAG_INDICATE,
)
_ENV_SENSE_SERVICE = (
_ENV_SENSE_UUID,
(_SERVICE_CHAR,),
)
_ADV_APPEARANCE_GENERIC_THERMOMETER = const(512)
class BLEclass:
def __init__(self, ble, name=""):
self._ble = ble
self._ble.active(True)
self._ble.irq(self._irq)
((self._handle,),) = self._ble.gatts_register_services((_ENV_SENSE_SERVICE,))
self._connections = set()
self._conn_handle = None
self._payload = advertising_payload(
name=name, services=[_ENV_SENSE_UUID]
)
self._advertise()
def _irq(self, event, data):
global flg1, flg2, char1
if event == _IRQ_GATTS_WRITE:
self._conn_handle, attr_handle = data
value = self._ble.gatts_read(attr_handle)
value = value.decode('utf-8')
print("Received data: ", value )
flg1 = 1
char1 = value
if flg2 == 1:
flg2 = 0
print("LED OFF")
else:
flg2 = 1
print("LED ON")
if event == _IRQ_CENTRAL_CONNECT:
self._conn_handle, _, _ = data
self._connections.add(self._conn_handle)
print("接続")
elif event == _IRQ_CENTRAL_DISCONNECT:
self._conn_handle, _, _ = data
print(self._conn_handle)
self._connections.remove(self._conn_handle)
self._advertise()
print("切断")
elif event == _IRQ_GATTS_INDICATE_DONE:
self._conn_handle, value_handle, status = data
print("指示完了")
def update_service(self, v):
if self._conn_handle is not None:
self._ble.gatts_write(self._handle, v)
def _advertise(self, interval_us=500000):
self._ble.gap_advertise(interval_us, adv_data=self._payload)
ble = bluetooth.BLE()
service = BLEclass(ble)
led = Pin('LED', Pin.OUT)
while (1):
if flg1 == 1:
service.update_service(char1)
flg1 = 0
led.value(flg2)
ble_advertising
https://github.com/micropython/micropython/blob/0fff2e03fe07471997a6df6f92c6960cfd225dc0/examples/bluetooth/ble_advertising.py
セントラル
let picoid = 0x000a;
let setdevice;
let setservice;
let setserver;
let setchara;
document.addEventListener('DOMContentLoaded', (event) => {
const scanButton = document.getElementById('scanBtn');
scanButton.addEventListener('click', () => {
navigator.bluetooth.requestDevice({
filters: [{
services: [picoid]
}]
})
.then(device => {
setdevice = device;
setdevice.addEventListener('gattserverdisconnected', disconnect);
console.log("接続成功: "+setdevice.name);
return setdevice.gatt.connect();
})
/*
.then(server => {
setserver = server;
return setserver.getPrimaryService(picoid);
})
.then(service => {
setservice = service;
return setservice.getCharacteristic(CharacteristicUUID_Notification);
})
.then(characteristic => {
setchara = characteristic;
})
*/
.catch(error => {
console.log("接続に失敗しました")
console.log(error);
});
});
});
const disconnect = () => {
console.log("接続終了");
};
自分で試したこと
edge://flags/#enable-experimental-web-platform-featuresの有効化
Chrome及びEdgeでの動作確認
windows10及びwindows11環境での動作確認
0