Skip to content

Commit f1be2de

Browse files
committed
Added JSBluetooth.isAvailable
1 parent 195b5e2 commit f1be2de

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

Sources/JavaScriptKit/JS Types/JSBluetooth.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ public final class JSBluetooth: JSType {
2222

2323
public static var shared: JSBluetooth? { return JSNavigator.shared?.bluetooth }
2424

25+
// MARK: - Accessors
26+
27+
public var isAvailable: JSPromise<Bool> {
28+
guard let function = jsObject.getAvailability.function
29+
else { fatalError("Invalid function \(#function)") }
30+
let result = function.apply(this: jsObject)
31+
guard let promise = result.object.flatMap({ JSPromise<Bool>($0) })
32+
else { fatalError("Invalid object \(result)") }
33+
return promise
34+
}
35+
2536
// MARK: - Methods
2637

2738
/// Returns a Promise to a BluetoothDevice object with the specified options.
@@ -46,7 +57,8 @@ public final class JSBluetooth: JSType {
4657

4758
// FIXME: Improve, support all options
4859
let options = JSObject()
49-
options[Option.acceptAllDevices.rawValue] = JSBoolean(acceptAllDevices).jsValue()
60+
options[Option.acceptAllDevices.rawValue] = acceptAllDevices.jsValue()
61+
options[Option.optionalServices.rawValue] = ["device_information"].jsValue()
5062

5163
let result = function.apply(this: jsObject, arguments: options)
5264

@@ -56,8 +68,3 @@ public final class JSBluetooth: JSType {
5668
return promise
5769
}
5870
}
59-
60-
internal extension JSBluetooth {
61-
62-
static let classObject = JSObjectRef.global.Bluetooth.function!
63-
}

0 commit comments

Comments
 (0)