[英]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. 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. I understand that up to 512 bytes can be sent through a GATT characteristic which is too small for my purposes.
我知道通过 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?
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?
Here is the javascript code to request the raspberry pi below.下面是请求树莓派的 javascript 代码。 I have python scripts running on the pi to advertise itself and its services/characteristics.
我在 pi 上运行 python 脚本来宣传自己及其服务/特征。 Currently I have a custom GATT characteristic that I have built but can't send an image through because of the 512 byte limit.
目前我有一个自定义的 GATT 特性,但由于 512 字节的限制,我无法通过它发送图像。 After using the API to find/connect to the pi, is there another way to send images from the pi back to the android?
使用 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;
})
}
Resolved: I ended up converting the image to a string and sending it through one characteristic with a loop.已解决:我最终将图像转换为字符串并通过一个循环发送它。 My web page starts characteristic notifications and gets 512 bytes at a time to then string together and display an image.
我的 web 页面启动特征通知并一次获取 512 个字节,然后串在一起并显示图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.