@@ -2,6 +2,8 @@ package uk.co.playerdata.reactnativemcumanager
2
2
3
3
import android.bluetooth.BluetoothAdapter
4
4
import android.bluetooth.BluetoothDevice
5
+ import android.bluetooth.BluetoothManager
6
+ import android.content.Context
5
7
import android.net.Uri
6
8
import android.util.Log
7
9
import expo.modules.kotlin.Promise
@@ -27,22 +29,24 @@ class UpdateOptions : Record {
27
29
@Field val upgradeMode: Int? = null
28
30
}
29
31
30
- class ReactNativeMcuManagerModule : Module () {
31
- private val bluetoothAdapter: BluetoothAdapter ? = BluetoothAdapter .getDefaultAdapter()
32
+ class ReactNativeMcuManagerModule () : Module() {
32
33
private val upgrades: MutableMap <String , DeviceUpgrade > = mutableMapOf ()
33
34
private val context
34
35
get() = requireNotNull(appContext.reactContext) { " React Application Context is null" }
35
36
37
+ private fun getBluetoothDevice (macAddress : String? ): BluetoothDevice {
38
+ val bluetoothManager = context.getSystemService(Context .BLUETOOTH_SERVICE ) as BluetoothManager
39
+ val adapter = bluetoothManager?.adapter ? : throw Exception (" No bluetooth adapter" )
40
+
41
+ return adapter.getRemoteDevice(macAddress)
42
+ }
43
+
36
44
override fun definition () = ModuleDefinition {
37
45
Name (MODULE_NAME )
38
46
39
47
AsyncFunction (" eraseImage" ) { macAddress: String? , promise: Promise ->
40
- if (this @ReactNativeMcuManagerModule.bluetoothAdapter == null ) {
41
- throw Exception (" No bluetooth adapter" )
42
- }
43
-
44
48
try {
45
- val device: BluetoothDevice = bluetoothAdapter.getRemoteDevice (macAddress)
49
+ val device: BluetoothDevice = getBluetoothDevice (macAddress)
46
50
47
51
val transport = McuMgrBleTransport (context, device)
48
52
transport.connect(device).timeout(60000 ).await()
@@ -63,15 +67,11 @@ class ReactNativeMcuManagerModule : Module() {
63
67
updateOptions: UpdateOptions ,
64
68
progressCallback: JavaScriptFunction <Unit >,
65
69
stateCallback: JavaScriptFunction <Unit > ->
66
- if (this @ReactNativeMcuManagerModule.bluetoothAdapter == null ) {
67
- throw Exception (" No bluetooth adapter" )
68
- }
69
-
70
70
if (upgrades.contains(id)) {
71
71
throw Exception (" Update ID already present" )
72
72
}
73
73
74
- val device: BluetoothDevice = bluetoothAdapter.getRemoteDevice (macAddress)
74
+ val device: BluetoothDevice = getBluetoothDevice (macAddress)
75
75
val updateFileUri = Uri .parse(updateFileUriString)
76
76
77
77
val upgrade = DeviceUpgrade (
@@ -128,11 +128,7 @@ class ReactNativeMcuManagerModule : Module() {
128
128
}
129
129
130
130
AsyncFunction (" resetDevice" ) { macAddress: String , promise: Promise ->
131
- if (this @ReactNativeMcuManagerModule.bluetoothAdapter == null ) {
132
- throw Exception (" No bluetooth adapter" )
133
- }
134
-
135
- val device: BluetoothDevice = bluetoothAdapter.getRemoteDevice(macAddress)
131
+ val device: BluetoothDevice = getBluetoothDevice(macAddress)
136
132
137
133
val transport = McuMgrBleTransport (context, device)
138
134
transport.connect(device).timeout(60000 ).await()
0 commit comments