Skip to content

Commit 9f27f8a

Browse files
committed
ios: add bluetoothAvailable to state
So the frontend can show a helpful message.
1 parent 234c504 commit 9f27f8a

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

backend/devices/bluetooth/bluetooth.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ type Peripheral struct {
3030

3131
// State contains everything needed to render bluetooth peripherals and other data in the frontend.
3232
type State struct {
33-
Peripherals []*Peripheral `json:"peripherals"`
34-
Connecting bool `json:"connecting"`
33+
// BluetoothAvailable is false if bluetooth is powered off or otherwise unavailable.
34+
BluetoothAvailable bool `json:"bluetoothAvailable"`
35+
Peripherals []*Peripheral `json:"peripherals"`
36+
Connecting bool `json:"connecting"`
3537
}
3638

3739
// Bluetooth manages a list of peripherals.

frontends/ios/BitBoxApp/BitBoxApp/Bluetooth.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import CoreBluetooth
99
import Mobileserver
1010

1111
struct State {
12+
var bluetoothAvailable: Bool
1213
var discoveredPeripherals: [UUID: PeripheralMetadata]
1314
var connecting: Bool
1415
}
@@ -30,7 +31,7 @@ var pairedDeviceIdentifiers: Set<String> {
3031
}
3132

3233
class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeripheralDelegate {
33-
private var state: State = State(discoveredPeripherals: [:], connecting: false)
34+
private var state: State = State(bluetoothAvailable: false, discoveredPeripherals: [:], connecting: false)
3435

3536
var centralManager: CBCentralManager!
3637
var connectedPeripheral: CBPeripheral?
@@ -49,6 +50,8 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
4950
override init() {
5051
super.init()
5152
centralManager = CBCentralManager(delegate: self, queue: nil)
53+
state.bluetoothAvailable = centralManager.state == .poweredOn
54+
updateBackendState()
5255
}
5356

5457
func isConnected() -> Bool {
@@ -77,6 +80,9 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
7780
}
7881

7982
func centralManagerDidUpdateState(_ central: CBCentralManager) {
83+
state.bluetoothAvailable = centralManager.state == .poweredOn
84+
updateBackendState()
85+
8086
switch central.state {
8187
case .poweredOn:
8288
print("BLE: on")
@@ -285,6 +291,7 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
285291
}
286292

287293
struct StateJSON: Codable {
294+
let bluetoothAvailable: Bool
288295
let peripherals: [PeripheralJSON]
289296
let connecting: Bool
290297
}
@@ -298,7 +305,7 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
298305
)
299306
}
300307

301-
let state = StateJSON(peripherals: peripherals, connecting: state.connecting)
308+
let state = StateJSON(bluetoothAvailable: state.bluetoothAvailable, peripherals: peripherals, connecting: state.connecting)
302309

303310
do {
304311
let encoder = JSONEncoder()

frontends/web/src/api/bluetooth.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type TPeripheral = {
2424
};
2525

2626
export type TState = {
27+
bluetoothAvailable: boolean;
2728
peripherals: TPeripheral[];
2829
connecting: boolean;
2930
};

frontends/web/src/components/bluetooth/bluetooth.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const _Bluetooth = () => {
2828
if (!state) {
2929
return null;
3030
}
31+
if (!state.bluetoothAvailable) {
32+
return <>Please turn on Bluetooth</>;
33+
}
3134
return (
3235
<>
3336
<div className={styles.label}>

0 commit comments

Comments
 (0)