Skip to content

Commit 62b4a5a

Browse files
committed
fix build iOS
1 parent 2728daa commit 62b4a5a

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

android/src/main/java/it/iotinga/blelibrary/RNBleManager.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import android.bluetooth.BluetoothGattService
77
import android.content.Context
88
import android.util.Base64
99
import android.util.Log
10+
import androidx.core.graphics.component1
11+
import androidx.core.graphics.component2
12+
import androidx.core.graphics.component3
13+
import androidx.core.graphics.component4
1014
import expo.modules.kotlin.Promise
1115
import no.nordicsemi.android.ble.BleManager
1216
import no.nordicsemi.android.ble.TimeoutableRequest
@@ -102,12 +106,16 @@ class RNBleManager(
102106
)
103107
}
104108

109+
// Function is called after connect to perform initialization before service discovery
105110
override fun initialize() {
106111
requestById.clear()
107112

108113
val mtu = mtu
109-
if (mtu != null) {
114+
if (mtu != null && mtu != 0) {
110115
requestMtu(mtu)
116+
.before {
117+
emitConnectionStateChange(ConnectionState.REQUESTING_MTU, 0)
118+
}
111119
.fail { device, status ->
112120
log(Log.WARN, "Error requesting MTU: $status")
113121
}
@@ -118,17 +126,22 @@ class RNBleManager(
118126
}
119127
}
120128

129+
// This function is called when services are invalidate, for example when the device
130+
// disconnects.
121131
override fun onServicesInvalidated() {
122132
gatt = null
123133
}
124134

135+
// This function is after service discovery is done. Shall return true if all required
136+
// services are available.
137+
// Since I don't know the services to be used in advance I just return true
125138
override fun isRequiredServiceSupported(gatt: BluetoothGatt): Boolean {
126139
this.gatt = gatt
127140

128141
return true
129142
}
130143

131-
fun enqueue(transactionId: String, request: TimeoutableRequest) {
144+
private fun enqueue(transactionId: String, request: TimeoutableRequest) {
132145
requestById[transactionId] = request
133146

134147
request
@@ -146,8 +159,9 @@ class RNBleManager(
146159
this.mtu = mtu
147160

148161
if (isConnected) {
162+
log(Log.WARN, "Device already connected. Disconnecting it first.")
149163
disconnect()
150-
.done { log(Log.INFO, "device disconnected") }
164+
.done { log(Log.INFO, "Device disconnected") }
151165
.fail { device, status ->
152166
Log.w(
153167
LOG_TAG,

ios/ReactNativeBleLibraryModule.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ private final class BleLibraryImpl: NSObject, CBCentralManagerDelegate,
256256
// MARK: Scan
257257

258258
func scanStart(serviceUuids: [String]?, promise: Promise) {
259-
print("[BleLibrary] scanStart(\(serviceUuids, default: "[]")")
259+
print("[BleLibrary] scanStart(\(serviceUuids ?? [])")
260260
guard self.isModuleInitialized else {
261261
print("[BleLibrary] manager is not initialized")
262262
return promise.reject(
@@ -310,7 +310,7 @@ private final class BleLibraryImpl: NSObject, CBCentralManagerDelegate,
310310
promise: Promise
311311
) {
312312
print(
313-
"[BleLibrary] connect(\(deviceId), \(mtu), \(options, default: "null"))"
313+
"[BleLibrary] connect(\(deviceId), \(mtu), \(options ?? [:])"
314314
)
315315
guard self.isModuleInitialized else {
316316
print("[BleLibrary] module is not initialized")
@@ -799,7 +799,7 @@ private final class BleLibraryImpl: NSObject, CBCentralManagerDelegate,
799799
error: Error?
800800
) {
801801
print(
802-
"[BleLibrary] error connecting to peripheral \(peripheral) (error: \(error, default: "nil")"
802+
"[BleLibrary] error connecting to peripheral \(peripheral) (error: \(String(describing: error))"
803803
)
804804

805805
// Cancel connection promise if present
@@ -913,7 +913,7 @@ private final class BleLibraryImpl: NSObject, CBCentralManagerDelegate,
913913
) {
914914
if let error = error {
915915
print(
916-
"[BleLibrary] error discovering characteristics for service \(service.uuid) (error: \(error, default: "null")"
916+
"[BleLibrary] error discovering characteristics for service \(service.uuid) (error: \(String(describing: error))"
917917
)
918918
// Cancel connection promise if present
919919
if let p = connectionPromise {
@@ -1030,7 +1030,7 @@ private final class BleLibraryImpl: NSObject, CBCentralManagerDelegate,
10301030
)
10311031
} else {
10321032
print(
1033-
"[BleLibrary] disconnected from peripheral \(peripheral) failed (error: \(error, default: "null"). Trigger new connection"
1033+
"[BleLibrary] disconnected from peripheral \(peripheral) failed (error: \(String(describing: error)). Trigger new connection"
10341034
)
10351035

10361036
module.sendEvent(
@@ -1090,7 +1090,7 @@ private final class BleLibraryImpl: NSObject, CBCentralManagerDelegate,
10901090
print("[BleLibrary] read RSSI success, RSSI = \(RSSI)")
10911091
t.succeed(RSSI)
10921092
} else {
1093-
print("[BleLibrary] read RSSI error (error: \(error, default: "null")")
1093+
print("[BleLibrary] read RSSI error (error: \(String(describing: error))")
10941094
t.fail(ERROR_GATT, (error! as NSError).description)
10951095
}
10961096
transactionById.removeValue(forKey: t.transactionId)
@@ -1145,7 +1145,7 @@ private final class BleLibraryImpl: NSObject, CBCentralManagerDelegate,
11451145
}
11461146
} else {
11471147
print(
1148-
"[BleLibrary] write value failure (error: \(error, default: "null")"
1148+
"[BleLibrary] write value failure (error: \(String(describing: error))"
11491149
)
11501150
write.fail(ERROR_GATT, error?.localizedDescription ?? "")
11511151
transactionById.removeValue(forKey: write.transactionId)
@@ -1200,7 +1200,7 @@ private final class BleLibraryImpl: NSObject, CBCentralManagerDelegate,
12001200
}
12011201
} else {
12021202
print(
1203-
"[BleLibrary] read value failure (error: \(error, default: "null")"
1203+
"[BleLibrary] read value failure (error: \(String(describing: error))"
12041204
)
12051205
read.fail(ERROR_GATT, "error reading characteristic")
12061206
transactionById.removeValue(forKey: read.transactionId)
@@ -1253,7 +1253,7 @@ private final class BleLibraryImpl: NSObject, CBCentralManagerDelegate,
12531253
transaction.succeed(nil)
12541254
} else {
12551255
print(
1256-
"[BleLibrary] characteristic \(characteristic.uuid) notification state update (error: \(error, default: "null")"
1256+
"[BleLibrary] characteristic \(characteristic.uuid) notification state update (error: \(String(describing: error))"
12571257
)
12581258
transaction.fail(ERROR_GATT, "error setting notification")
12591259
}
@@ -1338,6 +1338,7 @@ public final class ReactNativeBleLibraryModule: Module {
13381338
OnStartObserving {
13391339
print("[BleLibrary] NativeEventListener registered")
13401340
}
1341+
13411342
OnStopObserving {
13421343
print("[BleLibrary] NativeEventListener removed")
13431344
}

0 commit comments

Comments
 (0)