Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement characteristic instance id support #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rohitsangwan01
Copy link

@rohitsangwan01 rohitsangwan01 commented Jan 22, 2025

This Pr is not stable,
Tried adding Instance Id support, so that we can define multiple chars with same uuid in same service, and identify those with instanceId

Copy link

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Race Condition

The subscribedCharDevicesMap is accessed from multiple threads without proper synchronization, which could lead to race conditions when handling device subscriptions and notifications.

private val subscribedCharDevicesMap: MutableMap<String, MutableList<BluetoothGattCharacteristic>> =
    HashMap()
Memory Leak

The bluetoothGattCharacteristics list grows indefinitely as characteristics are added, without any cleanup mechanism when services are removed.

private val bluetoothGattCharacteristics: MutableList<BleCharCache> = mutableListOf()
Error Handling

The findCharacteristic method could return null without proper error handling in the calling code, potentially leading to null pointer exceptions.

fun String.findCharacteristic(instanceId: Long?): BluetoothGattCharacteristic? {
    if (instanceId != null) {
        return bluetoothGattCharacteristics.find { it.uuid == this && instanceId.toInt() == it.instanceId }?.char
    }
    return bluetoothGattCharacteristics.find { it.uuid == this }?.char
}

Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Score
Possible issue
Add thread synchronization

Add synchronized block around bluetoothGattCharacteristics list modifications to
prevent potential concurrent modification issues

android/src/main/kotlin/com/rohit/ble_peripheral/BleExtension.kt [93-94]

-bluetoothGattCharacteristics.add(BleCharCache(uuid, instanceId.toInt(), char))
+synchronized(bluetoothGattCharacteristics) {
+    bluetoothGattCharacteristics.add(BleCharCache(uuid, instanceId.toInt(), char))
+}
  • Apply this suggestion
Suggestion importance[1-10]: 8

Why: The suggestion prevents potential race conditions and data corruption by adding thread synchronization around shared list modifications, which is crucial for thread safety in a BLE implementation.

8
Add null safety check

Add null check before accessing device.name to avoid potential NullPointerException
when device name is not available

android/src/main/kotlin/com/rohit/ble_peripheral/BlePeripheralPlugin.kt [486-492]

 bleCallback?.onCharacteristicSubscriptionChange(
     it,
-    characteristicId, 
+    characteristicId,
     isSubscribed,
-    device.name,
+    device.name?.toString(),
     instanceIdArg = descriptor.characteristic.instanceId.toGivenInstanceId()
 ) {}
  • Apply this suggestion
Suggestion importance[1-10]: 7

Why: The suggestion addresses a potential NullPointerException by adding a null safety check for device.name, which is important for app stability and crash prevention.

7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant