[英]Can I send a stream of images from a raspberry pi to android using chrome web bluetooth low energy API?
Currently I have a HTML/Javascript web application working on my Android that can pair with a raspberry pi using the Chrome web bluetooth api. 我知道通過 GATT 特性最多可以發送 512 個字節,這對於我的目的來說太小了。 Is there a way to send a stream of images (or even just one image for now) from the pi to my android over BLE with this web bluetooth api?
下面是請求樹莓派的 javascript 代碼。 我在 pi 上運行 python 腳本來宣傳自己及其服務/特征。 目前我有一個自定義的 GATT 特性,但由於 512 字節的限制,我無法通過它發送圖像。 使用 API 查找/連接到 pi 后,是否有另一種方法可以將圖像從 pi 發送回 android?
//device and characteristic variables
var bluetoothDevice;
var imageCharacteristic;
//decoder for array buffer -> string
var enc = new TextDecoder("utf-8");
//UUIDs
//service uuid
const serviceUUID = '00000001-710e-4a5b-8d75-3e5b444bc3cf'
//write characteristic for taking an image on the pi
const imageCharacteristicUUID = '00000007-710e-4a5b-8d75-3e5b444bc3cf'
//button 1: change connect/disconnect button when clicked
connectButton.addEventListener('click', function handleClick() {
const initialText = 'Connect';
if (connectButton.innerText.includes(initialText)) { //user clicks 'connect'
//check if see if bluetooth device has already been requested
return (bluetoothDevice ? Promise.resolve() : requestDevice())
.then(connectDevice)
.then(_ => {
connectButton.innerText = 'Disconnect';
document.getElementById("status").innerHTML = 'BT Connected';
})
.catch(error => {
console.log('error' + error);
});
} else { //user clicks 'disconnect'
if (bluetoothDevice.gatt.connected) { //error handling to ensure device is actually connected
console.log('disconnecting...')
bluetoothDevice.gatt.disconnect();
} else {
console.log('Bluetooth Device is already disconnected');
}
}
})
//request device using web bluetooth api
function requestDevice() {
return navigator.bluetooth.requestDevice({
//filter to search for one device
filters: [
{name: 'rasPi'},
{services: [serviceUUID]}
]
})
.then(device => {
bluetoothDevice = device;
})
}
//connect to device and register services and characteristics
function connectDevice() {
if (bluetoothDevice.gatt.connected) {
return Promise.resolve();
}
console.log("connected");
return bluetoothDevice.gatt.connect()
.then(server => server.getPrimaryService(serviceUUID))
.then(service => service.getCharacteristics())
.then(characteristics => {
//start a queue to gather each characteristic
let queue = Promise.resolve();
characteristics.forEach(characteristic => {
switch (characteristic.uuid) {
case BluetoothUUID.getCharacteristic(imageCharacteristicUUID) :
queue = queue.then(_ => {
imageCharacteristic = characteristic;
})
break;
default : console.log('Unknown Characteristic: ' + characteristic.uuid);
}
});
return queue;
})
}
已解決:我最終將圖像轉換為字符串並通過一個循環發送它。 我的 web 頁面啟動特征通知並一次獲取 512 個字節,然后串在一起並顯示圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.