File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 ">
5
+ < title > Arduino Light Control</ title >
6
+ </ head >
7
+ < body >
8
+ < button id ="connect "> Connect</ button >
9
+ < button id ="toggleLight "> Toggle Light</ button >
10
+ < script >
11
+ let device ;
12
+ let characteristic ;
13
+
14
+ document . getElementById ( 'connect' ) . addEventListener ( 'click' , async ( ) => {
15
+ try {
16
+ device = await navigator . bluetooth . requestDevice ( { acceptAllDevices : true } ) ;
17
+ const server = await device . gatt . connect ( ) ;
18
+ console . log ( "server is connected" )
19
+ const service = await server . getPrimaryService ( 'cae55316-6575-413c-9858-0f76c95f6590' ) ;
20
+ console . log ( "service is connected" )
21
+ characteristic = await service . getCharacteristic ( 'bd13c171-c34c-46ef-9078-70fd20719d31' ) ;
22
+ alert ( 'Connected!' ) ;
23
+ } catch ( error ) {
24
+ console . error ( 'Error:' , error ) ;
25
+ }
26
+ } ) ;
27
+
28
+ document . getElementById ( 'toggleLight' ) . addEventListener ( 'click' , async ( ) => {
29
+ if ( characteristic ) {
30
+ const value = await characteristic . readValue ( ) ;
31
+ const newValue = value . getUint8 ( 0 ) === 0 ? 1 : 0 ;
32
+ await characteristic . writeValue ( new Uint8Array ( [ newValue ] ) ) ;
33
+ }
34
+ } ) ;
35
+ </ script >
36
+ </ body >
37
+ </ html >
You can’t perform that action at this time.
0 commit comments