Skip to content

Commit b1a06cb

Browse files
committed
Added JSBluetoothRemoteGATTService
1 parent 7eb4587 commit b1a06cb

File tree

3 files changed

+93
-26
lines changed

3 files changed

+93
-26
lines changed

Example/JavaScriptKitExample/Sources/JavaScriptKitExample/main.swift

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,50 @@ import JavaScriptKit
22

33
let alert = JSObjectRef.global.alert.function!
44
let document = JSObjectRef.global.document.object!
5-
let bluetooth = JSBluetooth.shared!
65

76
let divElement = document.createElement!("div").object!
87
divElement.innerText = "Swift Bluetooth Web App"
98
let body = document.body.object!
109
_ = body.appendChild!(divElement)
1110

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")
3114
}
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)
3451
}
35-
36-
_ = body.appendChild!(buttonElement)

Sources/JavaScriptKit/JS Types/JSBluetoothRemoteGATTServer.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public final class JSBluetoothRemoteGATTServer: JSType {
1515

1616
// MARK: - Initialization
1717

18-
public init(_ jsObject: JSObjectRef) {
18+
public init?(_ jsObject: JSObjectRef) {
1919
self.jsObject = jsObject
2020
}
2121

@@ -27,6 +27,7 @@ public final class JSBluetoothRemoteGATTServer: JSType {
2727

2828
// MARK: - Methods
2929

30+
/// Causes the script execution environment to connect to this device.
3031
public func connect() -> JSPromise<JSBluetoothRemoteGATTServer> {
3132
guard let function = jsObject.connect.function
3233
else { fatalError("Missing function \(#function)") }
@@ -36,7 +37,22 @@ public final class JSBluetoothRemoteGATTServer: JSType {
3637
return promise
3738
}
3839

40+
/// Causes the script execution environment to disconnect from this device.
3941
public func disconnect() {
40-
let _ = jsObject.disconnect.function?.apply(this: jsObject)
42+
guard let function = jsObject.disconnect.function
43+
else { fatalError("Missing function \(#function)") }
44+
function.apply(this: jsObject)
45+
}
46+
47+
/// Returns a promise to the primary BluetoothGATTService offered by the bluetooth device for a specified BluetoothServiceUUID.
48+
///
49+
/// - Parameter uuid: A Bluetooth service universally unique identifier for a specified device.
50+
public func getPrimaryService(_ uuid: String) -> JSPromise<JSBluetoothRemoteGATTService> {
51+
guard let function = jsObject.getPrimaryService.function
52+
else { fatalError("Missing function \(#function)") }
53+
let result = function.apply(this: jsObject, arguments: uuid)
54+
guard let value = result.object.flatMap({ JSPromise<JSBluetoothRemoteGATTService>($0) })
55+
else { fatalError("Invalid object \(result)") }
56+
return value
4157
}
4258
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// JSBluetoothRemoteGATTService.swift
3+
//
4+
//
5+
// Created by Alsey Coleman Miller on 6/4/20.
6+
//
7+
8+
/**
9+
JavaScript Bluetooth GATT Service
10+
11+
The [`BluetoothRemoteGATTService`](https://developer.mozilla.org/en-US/docs/Web/API/BluetoothRemoteGATTService) interface of the [Web Bluetooth API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Bluetooth_API) represents a service provided by a GATT server, including a device, a list of referenced services, and a list of the characteristics of this service.
12+
*/
13+
public final class JSBluetoothRemoteGATTService: JSType {
14+
15+
// MARK: - Properties
16+
17+
public let jsObject: JSObjectRef
18+
19+
// MARK: - Initialization
20+
21+
public init?(_ jsObject: JSObjectRef) {
22+
self.jsObject = jsObject
23+
}
24+
25+
// MARK: - Accessors
26+
27+
public lazy var isPrimary: Bool = jsObject.isPrimary.boolean ?? false
28+
29+
public lazy var uuid: String = jsObject.uuid.string ?? ""
30+
31+
// MARK: - Methods
32+
33+
34+
}
35+
36+

0 commit comments

Comments
 (0)