@@ -2,35 +2,50 @@ import JavaScriptKit
2
2
3
3
let alert = JSObjectRef . global. alert. function!
4
4
let document = JSObjectRef . global. document. object!
5
- let bluetooth = JSBluetooth . shared!
6
5
7
6
let divElement = document. createElement!( " div " ) . object!
8
7
divElement. innerText = " Swift Bluetooth Web App "
9
8
let body = document. body. object!
10
9
_ = body. appendChild!( divElement)
11
10
12
- let buttonElement = document. createElement!( " button " ) . object!
13
- buttonElement. innerText = " Scan for Bluetooth devices "
14
- buttonElement. onclick = . function { _ in
15
- JSConsole . info ( " Swift is running on browser! " )
16
- JSConsole . debug ( " \( #file) \( #function) \( #line) " )
17
- alert ( " Swift is running on browser! " )
18
- JSConsole . log ( " Requesting any Bluetooth Device... " )
19
- bluetooth. requestDevice ( ) . then { ( device: JSBluetoothDevice ) -> ( JSPromise < JSBluetoothRemoteGATTServer > ) in
20
- JSConsole . info ( device)
21
- JSConsole . debug ( " \( #file) \( #function) \( #line) \( device) " )
22
- alert ( " Got device \( device) " )
23
- JSConsole . log ( " Connecting to GATT Server... " )
24
- return device. gatt. connect ( )
25
- } . then { ( server: JSBluetoothRemoteGATTServer ) -> ( ) in
26
- JSConsole . info ( server)
27
- JSConsole . debug ( " \( #file) \( #function) \( #line) \( server) " )
28
- alert ( " Connected " )
29
- } . catch { ( error: JSError ) in
30
- alert ( " Error: \( error) " )
11
+ if let bluetooth = JSBluetooth . shared {
12
+ bluetooth. isAvailable. then {
13
+ JSConsole . assert ( $0, " Bluetooth not available " )
31
14
}
32
- JSConsole . debug ( " \( #file) \( #function) \( #line) " )
33
- return . undefined
15
+ let buttonElement = document. createElement!( " button " ) . object!
16
+ buttonElement. innerText = " Scan for Bluetooth devices "
17
+ buttonElement. onclick = . function { _ in
18
+ JSConsole . info ( " Swift is running on browser! " )
19
+ JSConsole . debug ( " \( #file) \( #function) \( #line) " )
20
+ alert ( " Swift is running on browser! " )
21
+ JSConsole . log ( " Requesting any Bluetooth Device... " )
22
+ bluetooth. requestDevice ( ) . then { ( device: JSBluetoothDevice ) -> ( JSPromise < JSBluetoothRemoteGATTServer > ) in
23
+ JSConsole . info ( device)
24
+ JSConsole . debug ( " \( #file) \( #function) \( #line) \( device) " )
25
+ alert ( " Got device \( device) " )
26
+ JSConsole . log ( " Connecting to GATT Server... " )
27
+ return device. gatt. connect ( )
28
+ } . then { ( server: JSBluetoothRemoteGATTServer ) -> ( JSPromise < JSBluetoothRemoteGATTService > ) in
29
+ JSConsole . info ( server)
30
+ JSConsole . debug ( " \( #file) \( #function) \( #line) \( server) " )
31
+ alert ( " Connected " )
32
+ JSConsole . log ( " Getting Device Information Service... " )
33
+ return server. getPrimaryService ( " device_information " )
34
+ } . then { ( service: JSBluetoothRemoteGATTService ) -> ( ) in
35
+ JSConsole . info ( service)
36
+ JSConsole . debug ( " \( #file) \( #function) \( #line) isPrimary \( service. isPrimary) uuid \( service. uuid) " )
37
+ } . catch { ( error: JSError ) in
38
+ JSConsole . debug ( #file, #function, #line)
39
+ JSConsole . error ( error)
40
+ alert ( " Error: \( error. message) " )
41
+ }
42
+ JSConsole . debug ( " \( #file) \( #function) \( #line) " )
43
+ return . undefined
44
+ }
45
+ _ = body. appendChild!( buttonElement)
46
+ } else {
47
+ JSConsole . error ( " Cannot access Bluetooth API " )
48
+ let divElement = document. createElement!( " div " ) . object!
49
+ divElement. innerText = " Bluetooth Web API not enabled "
50
+ _ = body. appendChild!( divElement)
34
51
}
35
-
36
- _ = body. appendChild!( buttonElement)
0 commit comments