mirror of
https://github.com/usatiuk/door-thing.git
synced 2025-10-26 19:17:49 +01:00
does kind of work
This commit is contained in:
@@ -1,8 +1,16 @@
|
|||||||
import { useState } from "preact/hooks";
|
import { useState } from "preact/hooks";
|
||||||
|
|
||||||
|
const doorServiceUUID: BluetoothServiceUUID = 0x1811;
|
||||||
|
const doorSwitchUUID: BluetoothCharacteristicUUID = 0x2ae2;
|
||||||
|
|
||||||
export function App() {
|
export function App() {
|
||||||
const [connecting, setConnecting] = useState(false);
|
const [connecting, setConnecting] = useState(false);
|
||||||
const [device, setDevice] = useState<BluetoothDevice>(null);
|
const [device, setDevice] = useState<{
|
||||||
|
device: BluetoothDevice;
|
||||||
|
service: BluetoothRemoteGATTService;
|
||||||
|
characteristic: BluetoothRemoteGATTCharacteristic;
|
||||||
|
} | null>(null);
|
||||||
|
const [doorOpen, setDoorOpen] = useState<boolean | null>(null);
|
||||||
|
|
||||||
const onConnectClick = async () => {
|
const onConnectClick = async () => {
|
||||||
try {
|
try {
|
||||||
@@ -13,12 +21,30 @@ export function App() {
|
|||||||
namePrefix: "Nano",
|
namePrefix: "Nano",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
optionalServices: [doorServiceUUID],
|
||||||
});
|
});
|
||||||
console.log("Found device:");
|
console.log("Found device:");
|
||||||
console.log(foundDevice);
|
console.log(foundDevice);
|
||||||
setDevice(foundDevice);
|
|
||||||
await foundDevice.gatt.connect();
|
await foundDevice.gatt.connect();
|
||||||
foundDevice.ongattserverdisconnected = onDisconnect;
|
foundDevice.ongattserverdisconnected = onDisconnect;
|
||||||
|
|
||||||
|
const doorService = await foundDevice.gatt.getPrimaryService(
|
||||||
|
doorServiceUUID,
|
||||||
|
);
|
||||||
|
|
||||||
|
const doorSwitch = await doorService.getCharacteristic(doorSwitchUUID);
|
||||||
|
setDevice({
|
||||||
|
device: foundDevice,
|
||||||
|
service: doorService,
|
||||||
|
characteristic: doorSwitch,
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await (await doorSwitch.readValue()).getUint8(0);
|
||||||
|
|
||||||
|
doorSwitch.oncharacteristicvaluechanged = onDoorUpdate;
|
||||||
|
doorSwitch.startNotifications();
|
||||||
|
|
||||||
|
setDoorOpen(Boolean(data));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("Error connecting:");
|
console.log("Error connecting:");
|
||||||
console.log(error);
|
console.log(error);
|
||||||
@@ -27,13 +53,21 @@ export function App() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const onDoorUpdate = async (ev: Event) => {
|
||||||
|
const characteristic = ev.target as BluetoothRemoteGATTCharacteristic;
|
||||||
|
const parsed = Boolean(await characteristic.value.getUint8(0));
|
||||||
|
setDoorOpen(parsed);
|
||||||
|
new Notification("Door update", { body: parsed ? "Open" : "Closed" });
|
||||||
|
};
|
||||||
|
|
||||||
const onDisconnect = async () => {
|
const onDisconnect = async () => {
|
||||||
setDevice(null);
|
setDevice(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onDisconnectClick = async () => {
|
const onDisconnectClick = async () => {
|
||||||
try {
|
try {
|
||||||
device.gatt.disconnect();
|
await device.characteristic.stopNotifications();
|
||||||
|
device.device.gatt.disconnect();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("Error disconnecting:");
|
console.log("Error disconnecting:");
|
||||||
console.log(error);
|
console.log(error);
|
||||||
@@ -50,6 +84,7 @@ export function App() {
|
|||||||
<>
|
<>
|
||||||
<span>connected</span>
|
<span>connected</span>
|
||||||
<button onClick={onDisconnectClick}>Disconnect</button>
|
<button onClick={onDisconnectClick}>Disconnect</button>
|
||||||
|
<span>{doorOpen ? "open" : "closed"}</span>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -4,10 +4,10 @@
|
|||||||
// Door switch pin
|
// Door switch pin
|
||||||
#define SWITCH_PIN 2
|
#define SWITCH_PIN 2
|
||||||
|
|
||||||
BLEService switchService("180A"); // BLE Switch Service
|
BLEService switchService("1811"); // BLE Switch Service
|
||||||
|
|
||||||
// BLE Switch Characteristic - custom 128-bit UUID, read by central
|
// BLE Switch Characteristic - custom 128-bit UUID, read by central
|
||||||
BLEByteCharacteristic switchCharacteristic("1234", BLERead | BLEIndicate);
|
BLEByteCharacteristic switchCharacteristic("2AE2", BLERead | BLEIndicate);
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user