Skip to content

Commit 501a04d

Browse files
committed
Update Central callbacks
1 parent f19e5b5 commit 501a04d

File tree

1 file changed

+70
-58
lines changed

1 file changed

+70
-58
lines changed

Sources/AndroidBluetooth/AndroidCentralCallback.swift

Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ extension AndroidCentral {
3636
extension AndroidCentral.LowEnergyScanCallback {
3737

3838
@JavaMethod
39-
func onScanResultSwift(_ error: Int32, _ result: AndroidBluetooth.ScanResult?) {
39+
func onScanResult(error: Int32, result: AndroidBluetooth.ScanResult?) {
4040
guard let central else {
4141
return
4242
}
@@ -57,7 +57,7 @@ extension AndroidCentral.LowEnergyScanCallback {
5757
}
5858

5959
@JavaMethod
60-
func onBatchScanResultsSwift(results: [AndroidBluetooth.ScanResult?]) {
60+
func onBatchScanResults(results: [AndroidBluetooth.ScanResult?]) {
6161
guard let central else {
6262
return
6363
}
@@ -107,24 +107,26 @@ extension AndroidCentral {
107107
@JavaClass("org.pureswift.bluetooth.BluetoothGattCallback")
108108
internal class GattCallback: AndroidBluetooth.BluetoothGattCallback {
109109

110-
private weak var central: AndroidCentral?
110+
weak var central: AndroidCentral?
111111

112-
/*
113-
convenience init(central: AndroidCentral) {
114-
self.init(javaObject: nil)
115-
bindNewJavaObject()
116-
117-
self.central = central
118-
}
112+
@JavaMethod
113+
@_nonoverride convenience init(environment: JNIEnvironment? = nil)
119114

120-
public required init(javaObject: jobject?) {
121-
super.init(javaObject: javaObject)
115+
convenience init(central: AndroidCentral, environment: JNIEnvironment? = nil) {
116+
self.init(environment: environment)
117+
self.central = central
122118
}
123119

124-
public override func onConnectionStateChange(
125-
gatt: Android.Bluetooth.Gatt,
126-
status: Android.Bluetooth.Gatt.Status,
127-
newState: Android.Bluetooth.Device.State
120+
}
121+
}
122+
@JavaImplementation("org.pureswift.bluetooth.BluetoothGattCallback")
123+
extension AndroidCentral.GattCallback {
124+
125+
@JavaMethod
126+
public func onConnectionStateChange(
127+
gatt: BluetoothGatt?,
128+
status: Int32,
129+
newState: Int32 //Android.Bluetooth.Device.State
128130
) {
129131
let log = central?.log
130132
log?("\(type(of: self)): \(#function)")
@@ -147,16 +149,17 @@ extension AndroidCentral {
147149
state.cache[peripheral] = nil
148150
default:
149151
log?("\(peripheral) Status Error")
150-
state.cache[peripheral]?.continuation.connect?.resume(throwing: status) // throw `status` error
152+
state.cache[peripheral]?.continuation.connect?.resume(throwing: AndroidCentralError.gattStatus(status))
151153
state.cache[peripheral]?.continuation.connect = nil
152154
}
153155
}
154156
}
155157
}
156158

157-
public override func onServicesDiscovered(
158-
gatt: Android.Bluetooth.Gatt,
159-
status: Android.Bluetooth.Gatt.Status
159+
@JavaMethod
160+
public func onServicesDiscovered(
161+
gatt: BluetoothGatt?,
162+
status: Int32
160163
) {
161164
let log = central?.log
162165
let peripheral = Peripheral(gatt)
@@ -173,16 +176,17 @@ extension AndroidCentral {
173176
}
174177
state.cache[peripheral]?.continuation.discoverServices?.resume(returning: services)
175178
default:
176-
state.cache[peripheral]?.continuation.discoverServices?.resume(throwing: status)
179+
state.cache[peripheral]?.continuation.discoverServices?.resume(throwing: AndroidCentralError.gattStatus(status))
177180
}
178181
state.cache[peripheral]?.continuation.discoverServices = nil
179182
}
180183
}
181184
}
182185

183-
public override func onCharacteristicChanged(
184-
gatt: Android.Bluetooth.Gatt,
185-
characteristic: Android.Bluetooth.GattCharacteristic
186+
@JavaMethod
187+
public func onCharacteristicChanged(
188+
gatt: BluetoothGatt?,
189+
characteristic: BluetoothGattCharacteristic?
186190
) {
187191
let log = central?.log
188192
log?("\(type(of: self)): \(#function)")
@@ -192,7 +196,7 @@ extension AndroidCentral {
192196
Task {
193197
await central?.storage.update { state in
194198

195-
guard let uuid = characteristic.getUUID().toString() else {
199+
guard let uuid = characteristic.getUuid().toString() else {
196200
assertionFailure()
197201
return
198202
}
@@ -205,7 +209,7 @@ extension AndroidCentral {
205209
let id = cache.identifier(for: characteristic)
206210

207211
let data = characteristic.getValue()
208-
.map { Data(unsafeBitCast($0, to: [UInt8].self)) } ?? Data()
212+
.map { Data(unsafeBitCast($0, to: [UInt8].self)) } ?? .init()
209213

210214
guard let characteristicCache = cache.characteristics.values[id] else {
211215
assertionFailure("Invalid identifier for \(uuid)")
@@ -222,10 +226,11 @@ extension AndroidCentral {
222226
}
223227
}
224228

225-
public override func onCharacteristicRead(
226-
gatt: Android.Bluetooth.Gatt,
227-
characteristic: Android.Bluetooth.GattCharacteristic,
228-
status: Android.Bluetooth.Gatt.Status
229+
@JavaMethod
230+
public func onCharacteristicRead(
231+
gatt: BluetoothGatt!,
232+
characteristic: BluetoothGattCharacteristic!,
233+
status: Int32
229234
) {
230235
let log = central?.log
231236
let peripheral = Peripheral(gatt)
@@ -240,17 +245,18 @@ extension AndroidCentral {
240245
.map { Data(unsafeBitCast($0, to: [UInt8].self)) } ?? Data()
241246
state.cache[peripheral]?.continuation.readCharacteristic?.resume(returning: data)
242247
default:
243-
state.cache[peripheral]?.continuation.readCharacteristic?.resume(throwing: status)
248+
state.cache[peripheral]?.continuation.readCharacteristic?.resume(throwing: AndroidCentralError.gattStatus(status))
244249
}
245250
state.cache[peripheral]?.continuation.readCharacteristic = nil
246251
}
247252
}
248253
}
249254

250-
public override func onCharacteristicWrite(
251-
gatt: Android.Bluetooth.Gatt,
252-
characteristic: Android.Bluetooth.GattCharacteristic,
253-
status: Android.Bluetooth.Gatt.Status
255+
@JavaMethod
256+
public func onCharacteristicWrite(
257+
gatt: BluetoothGatt!,
258+
characteristic: BluetoothGattCharacteristic!,
259+
status: Int32
254260
) {
255261
central?.log?("\(type(of: self)): \(#function)")
256262

@@ -262,21 +268,22 @@ extension AndroidCentral {
262268
case .success:
263269
state.cache[peripheral]?.continuation.writeCharacteristic?.resume()
264270
default:
265-
state.cache[peripheral]?.continuation.writeCharacteristic?.resume(throwing: status)
271+
state.cache[peripheral]?.continuation.writeCharacteristic?.resume(throwing: AndroidCentralError.gattStatus(status))
266272
}
267273
state.cache[peripheral]?.continuation.writeCharacteristic = nil
268274
}
269275
}
270276
}
271277

272-
public override func onDescriptorRead(
273-
gatt: Android.Bluetooth.Gatt,
274-
descriptor: Android.Bluetooth.GattDescriptor,
275-
status: Android.Bluetooth.Gatt.Status
278+
@JavaMethod
279+
public func onDescriptorRead(
280+
gatt: BluetoothGatt,
281+
descriptor: BluetoothGattDescriptor,
282+
status: Int32
276283
) {
277284
let peripheral = Peripheral(gatt)
278285

279-
guard let uuid = descriptor.getUUID().toString() else {
286+
guard let uuid = descriptor.getUuid().toString() else {
280287
assertionFailure()
281288
return
282289
}
@@ -292,22 +299,23 @@ extension AndroidCentral {
292299
.map { Data(unsafeBitCast($0, to: [UInt8].self)) } ?? Data()
293300
state.cache[peripheral]?.continuation.readDescriptor?.resume(returning: data)
294301
default:
295-
state.cache[peripheral]?.continuation.readDescriptor?.resume(throwing: status)
302+
state.cache[peripheral]?.continuation.readDescriptor?.resume(throwing: AndroidCentralError.gattStatus(status))
296303
}
297304
state.cache[peripheral]?.continuation.readDescriptor = nil
298305
}
299306
}
300307
}
301308

302-
public override func onDescriptorWrite(
303-
gatt: Android.Bluetooth.Gatt,
304-
descriptor: Android.Bluetooth.GattDescriptor,
305-
status: AndroidBluetoothGatt.Status
309+
@JavaMethod
310+
public func onDescriptorWrite(
311+
gatt: BluetoothGatt,
312+
descriptor: BluetoothGattDescriptor,
313+
status: Int32
306314
) {
307315

308316
let peripheral = Peripheral(gatt)
309317

310-
guard let uuid = descriptor.getUUID().toString() else {
318+
guard let uuid = descriptor.getUuid().toString() else {
311319
assertionFailure()
312320
return
313321
}
@@ -320,17 +328,18 @@ extension AndroidCentral {
320328
case .success:
321329
state.cache[peripheral]?.continuation.writeDescriptor?.resume()
322330
default:
323-
state.cache[peripheral]?.continuation.writeDescriptor?.resume(throwing: status)
331+
state.cache[peripheral]?.continuation.writeDescriptor?.resume(throwing: AndroidCentralError.gattStatus(status))
324332
}
325333
state.cache[peripheral]?.continuation.writeDescriptor = nil
326334
}
327335
}
328336
}
329337

330-
public override func onMtuChanged(
331-
gatt: Android.Bluetooth.Gatt,
338+
@JavaMethod
339+
public func onMtuChanged(
340+
gatt: BluetoothGatt,
332341
mtu: Int,
333-
status: Android.Bluetooth.Gatt.Status
342+
status: Int32
334343
) {
335344
central?.log?("\(type(of: self)): \(#function) Peripheral \(Peripheral(gatt)) MTU \(mtu) Status \(status)")
336345

@@ -366,17 +375,20 @@ extension AndroidCentral {
366375
}
367376
}
368377

369-
public override func onPhyRead(gatt: Android.Bluetooth.Gatt, txPhy: Android.Bluetooth.Gatt.TxPhy, rxPhy: Android.Bluetooth.Gatt.RxPhy, status: AndroidBluetoothGatt.Status) {
378+
@JavaMethod
379+
public func onPhyRead(gatt: BluetoothGatt, txPhy: Int32, rxPhy: Int32, status: Int32) {
370380

371381
central?.log?("\(type(of: self)): \(#function)")
372382
}
373383

374-
public override func onPhyUpdate(gatt: Android.Bluetooth.Gatt, txPhy: Android.Bluetooth.Gatt.TxPhy, rxPhy: Android.Bluetooth.Gatt.RxPhy, status: AndroidBluetoothGatt.Status) {
384+
@JavaMethod
385+
public func onPhyUpdate(gatt: BluetoothGatt, txPhy: Int32, rxPhy: Int32, status: Int32) {
375386

376387
central?.log?("\(type(of: self)): \(#function)")
377388
}
378389

379-
public override func onReadRemoteRssi(gatt: Android.Bluetooth.Gatt, rssi: Int, status: Android.Bluetooth.Gatt.Status) {
390+
@JavaMethod
391+
public func onReadRemoteRssi(gatt: BluetoothGatt, rssi: Int32, status: Int32) {
380392

381393
central?.log?("\(type(of: self)): \(#function) \(rssi) \(status)")
382394

@@ -388,16 +400,16 @@ extension AndroidCentral {
388400
case .success:
389401
state.cache[peripheral]?.continuation.readRemoteRSSI?.resume(returning: rssi)
390402
default:
391-
state.cache[peripheral]?.continuation.readRemoteRSSI?.resume(throwing: status)
403+
state.cache[peripheral]?.continuation.readRemoteRSSI?.resume(throwing: AndroidCentralError.gattStatus(status))
392404
}
393405
state.cache[peripheral]?.continuation.readRemoteRSSI = nil
394406
}
395407
}
396408
}
397409

398-
public override func onReliableWriteCompleted(gatt: Android.Bluetooth.Gatt, status: AndroidBluetoothGatt.Status) {
410+
@JavaMethod
411+
public override func onReliableWriteCompleted(gatt: BluetoothGatt, status: Int32) {
399412

400413
central?.log?("\(type(of: self)): \(#function)")
401-
}*/
402-
}
414+
}
403415
}

0 commit comments

Comments
 (0)