Using Bluetooth on mobile
This commit is contained in:
parent
7d0db309eb
commit
d44c23f815
7 changed files with 2200 additions and 6382 deletions
111
frontend/src/Bluetooth.tsx
Normal file
111
frontend/src/Bluetooth.tsx
Normal file
|
@ -0,0 +1,111 @@
|
|||
function Bluetooth() {
|
||||
const connectToDevice = async () => {
|
||||
if (!navigator.bluetooth)
|
||||
console.log('Web Bluetooth is not available!');
|
||||
navigator.bluetooth
|
||||
.requestDevice({
|
||||
filters: [
|
||||
{ namePrefix: "Chromecast Remote" }
|
||||
],
|
||||
optionalServices: ["battery_service"],
|
||||
})
|
||||
.then(device => {
|
||||
console.log(device);
|
||||
// console.log(device.id, device.name, device.gatt);
|
||||
// Set up event listener for when device gets disconnected.
|
||||
device.addEventListener('gattserverdisconnected', onDisconnected);
|
||||
|
||||
console.log(1)
|
||||
// Attempts to connect to remote GATT Server.
|
||||
const gatt = device.gatt
|
||||
if (!gatt)
|
||||
throw new Error('no gatt');
|
||||
return gatt.connect();
|
||||
})
|
||||
.then(server => {
|
||||
console.log(2)
|
||||
console.log(server)
|
||||
// Getting Battery Service…
|
||||
return server.getPrimaryService('battery_service');
|
||||
})
|
||||
.then(service => {
|
||||
console.log(3)
|
||||
// Getting Battery Level Characteristic…
|
||||
return service.getCharacteristic('battery_level');
|
||||
// return service.getCharacteristic(0x2a19);
|
||||
})
|
||||
.then(characteristic => {
|
||||
console.log(4)
|
||||
// Reading Battery Level…
|
||||
return characteristic.readValue();
|
||||
})
|
||||
.then(value => {
|
||||
console.log(5)
|
||||
console.log(`Battery percentage is ${value.getUint8(0)}`);
|
||||
})
|
||||
.catch(error => { console.log(error); });
|
||||
}
|
||||
const connectToDevice2 = async () => {
|
||||
if (!navigator.bluetooth)
|
||||
console.log('Web Bluetooth is not available!');
|
||||
let device = await navigator.bluetooth
|
||||
navigator.bluetooth
|
||||
.requestDevice({
|
||||
filters: [
|
||||
{ namePrefix: "Chromecast Remote" }
|
||||
],
|
||||
optionalServices: ["device_information"],
|
||||
})
|
||||
.then(device => {
|
||||
// Set up event listener for when device gets disconnected.
|
||||
device.addEventListener('gattserverdisconnected', onDisconnected);
|
||||
|
||||
console.log(1)
|
||||
// Attempts to connect to remote GATT Server.
|
||||
const gatt = device.gatt
|
||||
if (!gatt)
|
||||
throw new Error('no gatt');
|
||||
return gatt.connect();
|
||||
})
|
||||
.then(server => {
|
||||
console.log(2)
|
||||
console.log(server)
|
||||
// Getting Battery Service…
|
||||
return server.getPrimaryService('device_information');
|
||||
})
|
||||
.then(service => {
|
||||
console.log(3)
|
||||
// Getting Battery Level Characteristic…
|
||||
return service.getCharacteristic('manufacturer_name_string');
|
||||
})
|
||||
.then(characteristic => {
|
||||
console.log(4)
|
||||
// Reading Battery Level…
|
||||
return characteristic.readValue();
|
||||
})
|
||||
.then(value => {
|
||||
console.log(5)
|
||||
let decoder = new TextDecoder('utf-8');
|
||||
console.log(decoder.decode(value));
|
||||
})
|
||||
.catch(error => { console.log(error); });
|
||||
}
|
||||
|
||||
const onDisconnected = (event: any) => {
|
||||
// alert("Device Disconnected");
|
||||
// console.log(event);
|
||||
const device = event.target;
|
||||
console.log(`Device "${device.name}" is disconnected.`);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
<button className="bluetooth" onClick={connectToDevice2}>CONNECT</button>
|
||||
</>
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
export default Bluetooth
|
Loading…
Add table
Add a link
Reference in a new issue