From 037fc8611ad4afb99e26b6a84118c086b0037c31 Mon Sep 17 00:00:00 2001 From: Todd Showalter Date: Wed, 18 Sep 2024 16:31:56 -0400 Subject: [PATCH] Lint fixes & a linter target in the makefile. --- .../com/spruceid/mobile/sdk/GattClient.kt | 72 ++++++++++++------- makefile | 4 ++ 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/MobileSdk/src/main/java/com/spruceid/mobile/sdk/GattClient.kt b/MobileSdk/src/main/java/com/spruceid/mobile/sdk/GattClient.kt index 22f5aa7..167c51d 100644 --- a/MobileSdk/src/main/java/com/spruceid/mobile/sdk/GattClient.kt +++ b/MobileSdk/src/main/java/com/spruceid/mobile/sdk/GattClient.kt @@ -412,18 +412,30 @@ class GattClient(private var callback: GattClientCallback, // The android docs recommend cancelling discovery before connecting a socket for // perfomance reasons. - btAdapter?.cancelDiscovery() + try { + btAdapter?.cancelDiscovery() + } catch (e: SecurityException) { + reportLog("Unable to cancel discovery.") + } val connectThread: Thread = object : Thread() { override fun run() { try { // createL2capChannel() requires/initiates pairing, so we have to use - // the "insecure" version. - l2capSocket = device.createInsecureL2capChannel(channelPSM) - l2capSocket?.connect() + // the "insecure" version. This requires at least API 29, which we did + // check elsewhere (we'd never have got this far on a lower API), but + // the linter isn't smart enough to know that, and we have PR merging + // gated on a clean bill of health from the linter... + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + l2capSocket = device.createInsecureL2capChannel(channelPSM) + l2capSocket?.connect() + } } catch (e: IOException) { reportError("Error connecting to L2CAP socket: ${e.message}") return + } catch (e: SecurityException) { + reportError("Not authorized to connect to L2CAP socket.") + return } l2capWriteThread = Thread { writeResponse() } @@ -470,7 +482,7 @@ class GattClient(private var callback: GattClientCallback, reportError("Failure reading request, peer disconnected.") return } - payload.writeBytes(buf) + payload.write(buf, 0, buf.count()) dprint("Currently have ${buf.count()} bytes.") @@ -545,29 +557,33 @@ class GattClient(private var callback: GattClientCallback, dprint("-- ind: ${characteristic.getProperties()}") - if (!gatt.setCharacteristicNotification(characteristic, true)) { - reportError("Error setting notification on ${name}; call failed.") - return - } - - val descriptor: BluetoothGattDescriptor = characteristic.getDescriptor(clientCharacteristicConfigUuid) - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - val res = gatt.writeDescriptor(descriptor, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE) - if(res != BluetoothStatusCodes.SUCCESS) { - reportError("Error writing to ${name}. Code: $res") + try { + if (!gatt.setCharacteristicNotification(characteristic, true)) { + reportError("Error setting notification on ${name}; call failed.") return } - } else { - // Above code addresses the deprecation but requires API 33+ - @Suppress("deprecation") - descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE - @Suppress("deprecation") - if (!gatt.writeDescriptor(descriptor)) { - reportError("Error writing to ${name} clientCharacteristicConfig: desc.") - return + val descriptor: BluetoothGattDescriptor = characteristic.getDescriptor(clientCharacteristicConfigUuid) + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + val res = gatt.writeDescriptor(descriptor, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE) + if(res != BluetoothStatusCodes.SUCCESS) { + reportError("Error writing to ${name}. Code: $res") + return + } + } else { + // Above code addresses the deprecation but requires API 33+ + @Suppress("deprecation") + descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE + + @Suppress("deprecation") + if (!gatt.writeDescriptor(descriptor)) { + reportError("Error writing to ${name} clientCharacteristicConfig: desc.") + return + } } + } catch (e: SecurityException) { + reportError("Not authorized to enable notification on ${name}") } // An onDescriptorWrite() call will come in for the pair of this characteristic and the client @@ -608,8 +624,12 @@ class GattClient(private var callback: GattClientCallback, // when we call connect() on the socket we get a "no resources available" exception, and // the socket remains unopen. - //usingL2CAP = characteristicL2CAP != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q - usingL2CAP = false + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + //usingL2CAP = characteristicL2CAP != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q + usingL2CAP = false + } else { + usingL2CAP = false + } if (usingL2CAP) { enableNotification(gatt, characteristicL2CAP, "L2CAP") diff --git a/makefile b/makefile index 08ab306..0f3ece4 100644 --- a/makefile +++ b/makefile @@ -29,6 +29,10 @@ clean: #@ Clean the build. build: #@ Make the build; currently not working, use 'install' instead. @$(GRADLE) build +.phony: lint +lint: #@ Lint the build. + @$(GRADLE) lintDebug + .phony: install install: #@ Install the build to all devices. @$(GRADLE) installDebug