Skip to content

Commit c562ead

Browse files
committed
Fix AndroidCentral.GattCallback
1 parent 8ad2a2b commit c562ead

File tree

1 file changed

+87
-59
lines changed

1 file changed

+87
-59
lines changed

Sources/AndroidBluetooth/AndroidCentralCallback.swift

Lines changed: 87 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ extension AndroidCentral.GattCallback {
169169
}
170170

171171
@JavaMethod
172-
public func onServicesDiscovered(
172+
func onServicesDiscovered(
173173
gatt: BluetoothGatt?,
174174
status: Int32
175175
) {
@@ -200,21 +200,25 @@ extension AndroidCentral.GattCallback {
200200
}
201201
}
202202
}
203-
/*
203+
204204
@JavaMethod
205-
public func onCharacteristicChanged(
205+
func onCharacteristicChanged(
206206
gatt: BluetoothGatt?,
207207
characteristic: BluetoothGattCharacteristic?
208208
) {
209-
let log = central?.log
209+
guard let central, let gatt, let characteristic else {
210+
assertionFailure()
211+
return
212+
}
213+
let log = central.log
210214
log?("\(type(of: self)): \(#function)")
211215

212216
let peripheral = Peripheral(gatt)
213217

214218
Task {
215-
await central?.storage.update { state in
219+
await central.storage.update { state in
216220

217-
guard let uuid = characteristic.getUuid().toString() else {
221+
guard let uuid = characteristic.getUuid()?.toString() else {
218222
assertionFailure()
219223
return
220224
}
@@ -226,8 +230,10 @@ extension AndroidCentral.GattCallback {
226230

227231
let id = cache.identifier(for: characteristic)
228232

229-
let data = characteristic.getValue()
230-
.map { Data(unsafeBitCast($0, to: [UInt8].self)) } ?? .init()
233+
let bytes = characteristic.getValue()
234+
235+
// TODO: Replace usage of Foundation.Data with byte array to prevent copying
236+
let data = Data(unsafeBitCast(bytes, to: [UInt8].self))
231237

232238
guard let characteristicCache = cache.characteristics.values[id] else {
233239
assertionFailure("Invalid identifier for \(uuid)")
@@ -245,22 +251,27 @@ extension AndroidCentral.GattCallback {
245251
}
246252

247253
@JavaMethod
248-
public func onCharacteristicRead(
249-
gatt: BluetoothGatt!,
250-
characteristic: BluetoothGattCharacteristic!,
254+
func onCharacteristicRead(
255+
gatt: BluetoothGatt?,
256+
characteristic: BluetoothGattCharacteristic?,
251257
status: Int32
252258
) {
253-
let log = central?.log
259+
guard let central, let gatt, let characteristic else {
260+
assertionFailure()
261+
return
262+
}
263+
let log = central.log
254264
let peripheral = Peripheral(gatt)
265+
let status = BluetoothGatt.Status(rawValue: status)
255266
log?("\(type(of: self)): \(#function) \(peripheral) Status: \(status)")
256267

257268
Task {
258-
await central?.storage.update { state in
269+
await central.storage.update { state in
259270

260271
switch status {
261272
case .success:
262-
let data = characteristic.getValue()
263-
.map { Data(unsafeBitCast($0, to: [UInt8].self)) } ?? Data()
273+
let bytes = characteristic.getValue()
274+
let data = Data(unsafeBitCast(bytes, to: [UInt8].self))
264275
state.cache[peripheral]?.continuation.readCharacteristic?.resume(returning: data)
265276
default:
266277
state.cache[peripheral]?.continuation.readCharacteristic?.resume(throwing: AndroidCentralError.gattStatus(status))
@@ -271,17 +282,21 @@ extension AndroidCentral.GattCallback {
271282
}
272283

273284
@JavaMethod
274-
public func onCharacteristicWrite(
275-
gatt: BluetoothGatt!,
276-
characteristic: BluetoothGattCharacteristic!,
285+
func onCharacteristicWrite(
286+
gatt: BluetoothGatt?,
287+
characteristic: BluetoothGattCharacteristic?,
277288
status: Int32
278289
) {
279-
central?.log?("\(type(of: self)): \(#function)")
280-
290+
guard let central, let gatt else {
291+
assertionFailure()
292+
return
293+
}
294+
let status = BluetoothGatt.Status(rawValue: status)
295+
central.log?("\(type(of: self)): \(#function)")
281296
let peripheral = Peripheral(gatt)
282297

283298
Task {
284-
await central?.storage.update { state in
299+
await central.storage.update { state in
285300
switch status {
286301
case .success:
287302
state.cache[peripheral]?.continuation.writeCharacteristic?.resume()
@@ -294,27 +309,32 @@ extension AndroidCentral.GattCallback {
294309
}
295310

296311
@JavaMethod
297-
public func onDescriptorRead(
298-
gatt: BluetoothGatt,
299-
descriptor: BluetoothGattDescriptor,
312+
func onDescriptorRead(
313+
gatt: BluetoothGatt?,
314+
descriptor: BluetoothGattDescriptor?,
300315
status: Int32
301316
) {
317+
guard let central, let gatt, let descriptor else {
318+
assertionFailure()
319+
return
320+
}
321+
let status = BluetoothGatt.Status(rawValue: status)
302322
let peripheral = Peripheral(gatt)
303323

304-
guard let uuid = descriptor.getUuid().toString() else {
324+
guard let uuid = descriptor.getUuid()?.toString() else {
305325
assertionFailure()
306326
return
307327
}
308328

309-
central?.log?(" \(type(of: self)): \(#function) \(uuid)")
329+
central.log?(" \(type(of: self)): \(#function) \(uuid)")
310330

311331
Task {
312-
await central?.storage.update { state in
332+
await central.storage.update { state in
313333

314334
switch status {
315335
case .success:
316-
let data = descriptor.getValue()
317-
.map { Data(unsafeBitCast($0, to: [UInt8].self)) } ?? Data()
336+
let bytes = descriptor.getValue()
337+
let data = Data(unsafeBitCast(bytes, to: [UInt8].self))
318338
state.cache[peripheral]?.continuation.readDescriptor?.resume(returning: data)
319339
default:
320340
state.cache[peripheral]?.continuation.readDescriptor?.resume(throwing: AndroidCentralError.gattStatus(status))
@@ -325,23 +345,27 @@ extension AndroidCentral.GattCallback {
325345
}
326346

327347
@JavaMethod
328-
public func onDescriptorWrite(
329-
gatt: BluetoothGatt,
330-
descriptor: BluetoothGattDescriptor,
348+
func onDescriptorWrite(
349+
gatt: BluetoothGatt?,
350+
descriptor: BluetoothGattDescriptor?,
331351
status: Int32
332352
) {
333-
353+
guard let central, let gatt, let descriptor else {
354+
assertionFailure()
355+
return
356+
}
357+
let status = BluetoothGatt.Status(rawValue: status)
334358
let peripheral = Peripheral(gatt)
335359

336-
guard let uuid = descriptor.getUuid().toString() else {
360+
guard let uuid = descriptor.getUuid()?.toString() else {
337361
assertionFailure()
338362
return
339363
}
340364

341-
central?.log?(" \(type(of: self)): \(#function) \(uuid)")
365+
central.log?(" \(type(of: self)): \(#function) \(uuid)")
342366

343367
Task {
344-
await central?.storage.update { state in
368+
await central.storage.update { state in
345369
switch status {
346370
case .success:
347371
state.cache[peripheral]?.continuation.writeDescriptor?.resume()
@@ -354,19 +378,19 @@ extension AndroidCentral.GattCallback {
354378
}
355379

356380
@JavaMethod
357-
public func onMtuChanged(
358-
gatt: BluetoothGatt,
359-
mtu: Int,
381+
func onMtuChanged(
382+
gatt: BluetoothGatt?,
383+
mtu: Int32,
360384
status: Int32
361385
) {
362-
central?.log?("\(type(of: self)): \(#function) Peripheral \(Peripheral(gatt)) MTU \(mtu) Status \(status)")
363-
364-
let peripheral = Peripheral(gatt)
365-
366-
guard let central = self.central else {
386+
guard let central, let gatt else {
367387
assertionFailure()
368388
return
369389
}
390+
let status = BluetoothGatt.Status(rawValue: status)
391+
central.log?("\(type(of: self)): \(#function) Peripheral \(Peripheral(gatt)) MTU \(mtu) Status \(status)")
392+
393+
let peripheral = Peripheral(gatt)
370394

371395
let oldMTU = central.options.maximumTransmissionUnit
372396

@@ -394,29 +418,33 @@ extension AndroidCentral.GattCallback {
394418
}
395419

396420
@JavaMethod
397-
public func onPhyRead(gatt: BluetoothGatt, txPhy: Int32, rxPhy: Int32, status: Int32) {
398-
399-
central?.log?("\(type(of: self)): \(#function)")
421+
func onPhyRead(gatt: BluetoothGatt?, txPhy: Int32, rxPhy: Int32, status: Int32) {
422+
let status = BluetoothGatt.Status(rawValue: status)
423+
central?.log?("\(type(of: self)): \(#function) \(status)")
400424
}
401425

402426
@JavaMethod
403-
public func onPhyUpdate(gatt: BluetoothGatt, txPhy: Int32, rxPhy: Int32, status: Int32) {
404-
405-
central?.log?("\(type(of: self)): \(#function)")
427+
func onPhyUpdate(gatt: BluetoothGatt?, txPhy: Int32, rxPhy: Int32, status: Int32) {
428+
let status = BluetoothGatt.Status(rawValue: status)
429+
central?.log?("\(type(of: self)): \(#function) \(status)")
406430
}
407431

408432
@JavaMethod
409-
public func onReadRemoteRssi(gatt: BluetoothGatt, rssi: Int32, status: Int32) {
410-
411-
central?.log?("\(type(of: self)): \(#function) \(rssi) \(status)")
433+
func onReadRemoteRssi(gatt: BluetoothGatt?, rssi: Int32, status: Int32) {
434+
guard let central, let gatt else {
435+
assertionFailure()
436+
return
437+
}
438+
let status = BluetoothGatt.Status(rawValue: status)
439+
central.log?("\(type(of: self)): \(#function) \(rssi) \(status)")
412440

413441
let peripheral = Peripheral(gatt)
414442

415443
Task {
416-
await central?.storage.update { state in
444+
await central.storage.update { state in
417445
switch status {
418446
case .success:
419-
state.cache[peripheral]?.continuation.readRemoteRSSI?.resume(returning: rssi)
447+
state.cache[peripheral]?.continuation.readRemoteRSSI?.resume(returning: Int(rssi))
420448
default:
421449
state.cache[peripheral]?.continuation.readRemoteRSSI?.resume(throwing: AndroidCentralError.gattStatus(status))
422450
}
@@ -426,8 +454,8 @@ extension AndroidCentral.GattCallback {
426454
}
427455

428456
@JavaMethod
429-
public override func onReliableWriteCompleted(gatt: BluetoothGatt, status: Int32) {
430-
431-
central?.log?("\(type(of: self)): \(#function)")
432-
}*/
457+
func onReliableWriteCompleted(gatt: BluetoothGatt?, status: Int32) {
458+
let status = BluetoothGatt.Status(rawValue: status)
459+
central?.log?("\(type(of: self)): \(#function) \(status)")
460+
}
433461
}

0 commit comments

Comments
 (0)