-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbluetooth-strip.tsx
More file actions
42 lines (39 loc) · 1.23 KB
/
bluetooth-strip.tsx
File metadata and controls
42 lines (39 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Component, Method } from '@stencil/core';
@Component({
tag: 'bluetooth-strip'
})
export class BluetoothStrip {
private acceptEverything = false;
private serviceUUID = '0000ffe5-0000-1000-8000-00805f9b34fb';
ch: BluetoothRemoteGATTCharacteristic;
@Method()
async connect() {
const connectionOptions: RequestDeviceOptions = this.acceptEverything
? {
optionalServices: [this.serviceUUID],
acceptAllDevices: true
}
: {
optionalServices: [this.serviceUUID],
filters: [{ namePrefix: 'LED' }]
};
const device = await navigator.bluetooth.requestDevice(connectionOptions);
const server = await device.gatt.connect();
const service = await server.getPrimaryService(0xffe5);
const ch = await service.getCharacteristic(0xffe9);
this.ch = ch;
return;
}
@Method()
async setColor(red: number, green: number, blue: number) {
const r = new Uint8Array([0x56, green, red, blue, 0x00, 0xaa]);
await this.ch.writeValue(r);
}
@Method()
async runCommand(commandNumber: number) {
const speed = 0x10;
const command = commandNumber + 0x26;
const r = new Uint8Array([0xbb, command, speed, 0x44]);
await this.ch.writeValue(r);
}
}