-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththe_last_bluetooth.dart
67 lines (55 loc) · 2.1 KB
/
the_last_bluetooth.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import 'dart:async';
import 'package:flutter_broadcasts/flutter_broadcasts.dart' as fb;
import 'package:jni/jni.dart';
import 'package:rxdart/rxdart.dart';
import 'package:the_last_bluetooth/src/android_bluetooth.g.dart' as android;
import 'src/bluetooth_device.dart';
class TheLastBluetooth {
static final TheLastBluetooth _instance = TheLastBluetooth._();
static TheLastBluetooth get instance => _instance;
late android.BluetoothManager _manager;
late android.BluetoothAdapter _adapter;
// Our streams for user:
final _isEnabledCtrl = BehaviorSubject<bool>();
TheLastBluetooth._() {
// this is some init stuff - maybe move this to manual init() dispose() ?
final ctx = android.Context.fromRef(Jni.getCachedApplicationContext());
_manager = android.BluetoothManager.fromRef(
ctx
.getSystemService(android.Context.BLUETOOTH_SERVICE.toJString())
.reference,
);
_adapter = _manager.getAdapter();
_isEnabledCtrl.add(_adapter.isEnabled());
fb.BroadcastReceiver receiver = fb.BroadcastReceiver(
names: <String>[android.BluetoothAdapter.ACTION_STATE_CHANGED],
);
receiver.messages.listen((event) {
print(event);
switch (event) {
// oh my god, Dart shines already <3
// and we're not fully JNI yet :')
case fb.BroadcastMessage(
name: android.BluetoothAdapter.ACTION_STATE_CHANGED,
data: {
android.BluetoothAdapter.EXTRA_STATE: int state,
android.BluetoothAdapter.EXTRA_PREVIOUS_STATE: int _,
},
):
switch (state) {
case android.BluetoothAdapter.STATE_ON:
_isEnabledCtrl.add(true);
case android.BluetoothAdapter.STATE_OFF:
_isEnabledCtrl.add(false);
}
break;
}
});
receiver.start();
}
// TODO: Make this real
ValueStream<bool> get isAvailable => Stream.value(true).shareValue();
ValueStream<bool> get isEnabled => _isEnabledCtrl.stream;
ValueStream<Set<BluetoothDevice>> get pairedDevices =>
Stream.value(<BluetoothDevice>{}).shareValue();
}