fix(bluetoothle): fix typo in BluetoothLeService and migrate to snippets#934
fix(bluetoothle): fix typo in BluetoothLeService and migrate to snippets#934ithinkihaveacat wants to merge 22 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces Bluetooth Low Energy (BLE) GATT service and activity implementations in both Java and Kotlin. The code reviewer identified several critical issues, including potential GATT connection leaks due to unclosed BluetoothGatt instances, ServiceConnection leaks from missing unbindService calls in onDestroy, control flow bugs where connection attempts proceed after initialization failures, and unsafe null assertions (!!) in the Kotlin activity that could cause runtime crashes.
| if (!bluetoothService.initialize()) { | ||
| Log.e(TAG, "Unable to initialize Bluetooth"); | ||
| finish(); | ||
| } | ||
| // [END gatt_initialize_activity_java] | ||
| // perform device connection | ||
| // [START gatt_connect_activity_java] | ||
| bluetoothService.connect(deviceAddress); | ||
| // [END gatt_connect_activity_java] |
There was a problem hiding this comment.
If bluetoothService.initialize() fails, finish() is called to destroy the activity, but the method continues executing and attempts to call connect(). Add an early return; statement to prevent further execution.
if (!bluetoothService.initialize()) {\n Log.e(TAG, "Unable to initialize Bluetooth");\n finish();\n return;\n }\n // [END gatt_initialize_activity_java]\n // perform device connection\n // [START gatt_connect_activity_java]\n bluetoothService.connect(deviceAddress);\n // [END gatt_connect_activity_java]c7852e3 to
a02d6a9
Compare
|
These snippets have been ported directly from https://developer.android.com/develop/connectivity/bluetooth/ble/connect-gatt-server using a tool I'm testing. The comments from gemini-code-assist seem mostly reasonable however I am not a SME and so I'd rather not make any changes if possible. |
fc4336f to
439afc5
Compare
- Fix typo 'Return;' -> 'return;' in Java BluetoothLeService. - Migrate inline code samples from connect-gatt-server.md to snippets. - Create BluetoothLeService and DeviceControlActivity in Java and Kotlin. - Add gatt_services_characteristics layout and string resources to enable compilation. Bug: 359501045
439afc5 to
51240a7
Compare
|
Thanks Michael! Would you mind getting a Bluetooth SME to approve first? Then Yacine/I can approve to merge. |
|
Hey @kkuan2011
I checked with our Bluetooth SME and made a small change (0007881). |
kkuan2011
left a comment
There was a problem hiding this comment.
When I do a side by side visual comparison with the original page, I see the snippets have changed
https://developer.android.com/develop/connectivity/bluetooth/ble/connect-gatt-server
They omit the Service declaration that wraps a lot of the original snippets, function names have changed (which I understand was to avoid name conflicts within the same file), some global variables also got dropped, annotations got added. Is that what you were expecting?
I do try handling some of these cases in my snippet skill so you may want to try migrating the page with that, or fixing each difference in your current CL by iterating with the agent. It's a tedious process though.. i know :/
…nection snippets - Implemented 'Silent Wrapper' technique to resolve name inconsistencies (simpleConnect -> connect, simpleBluetoothGattCallback -> bluetoothGattCallback, simpleServiceConnection -> serviceConnection) without breaking compilation. - Moved @RequiresPermission annotations outside of snippet region tags or wrapped them in silent excludes to match original documentation. - Renamed 'Dummy implementation' to 'Placeholder implementation'. - Removed temporary 'FIXED' comment in BluetoothLeService.java. - Cleaned up duplicate region tags in BluetoothLeService.kt.
Extend update receiver regions in DeviceControlActivity (Java/Kotlin) to cover lifecycle methods, using silent excludes to hide onCreate and permission annotations. This matches the DAC documentation page structure exactly while keeping the repository code compilable.
Migrate the entire BluetoothLeService binder code block (including class declaration) into the repository as a new region tag 'android_bluetooth_service_binder'. Use silent exclusions to hide the rest of the class methods, ensuring the snippet is 100% generated from the source file without hardcoding wrappers in the markdown.
Revert the risky nested exclusions in the main BluetoothLeService files. Instead, split the binder-only snippet into dedicated files in a separate 'binder' package (to avoid name clashes). This ensures the binder snippet is cleanly imported with its class wrapper, without using dangerous nested exclusions, and keeping all code 100% in the repository.
Remove the old 'android_bluetooth_binder' and 'android_bluetooth_binder_java' region tags from the main BluetoothLeService files, as they have been replaced by the new dedicated binder snippets.
Remove 'android_bluetooth_receiver_lifecycle' and 'android_bluetooth_receiver_lifecycle_java' tags from DeviceControlActivity files. These sub-regions are now redundant because they are fully merged into the parent 'android_bluetooth_update_receiver' regions to fix the snippet length issue.
Add the 'android_bluetooth_connect_gatt' (and Java) tags around the connectGatt call in BluetoothLeService to allow importing it without class wrappers. Expand the 'android_bluetooth_bind_service' (and Java) tags to cover the entire onCreate method, enabling clean imports in the documentation.
Now that we have simplified the documentation to remove all hardcoded class wrappers, the binder snippet also no longer needs the class wrapper. Thus, we can revert the split and move the binder snippet tags back to the main BluetoothLeService files, deleting the redundant 'binder' package files.
No, and I apologize for missing this, and the other issues you pointed out! These should now be resolved, although it's quite difficult for anyone to review without the context of a rendered DAC page that incorporates the snippets. Note: as part of the most recent changes I removed the outer |
Merge consecutive region tags into single combined tags to simplify the documentation imports and remove redundant splits: 1. Merge 'service_connection' and 'bind_service' in DeviceControlActivity by reordering onCreate to be immediately after serviceConnection. 2. Merge 'callback' and 'constants' in BluetoothLeService by reordering the companion object and callback to the top of the class. Also fix the silent wrapper tagging in BluetoothLeService callback to ensure the declaration and its closing brace are rendered.
Apply spotless formatting fix to BluetoothLeService.kt.
kkuan2011
left a comment
There was a problem hiding this comment.
I haven't done a full review yet, but since it looks like what will be shown in the resulting DAC page will look different, could you run it by a bluetooth SME one more time? It may be easier to generate a diff report of what is different, so it's easier to see + confirm if those changes are ok. Thank you Michael!
Pull request was converted to draft
Remove unused Java snippet files and update Kotlin snippets to use silent wrappers and correct region tags to match DevSite structure.
b37ec93 to
5a135dd
Compare
…ing @SuppressLint
|
Ok, flipping this PR back to ready for review. Thanks for the comments! Here's a summary of the PR and the rationale for the (small!) differences between the repository code and the original hardcoded documentation. 1. Bug Fixes, Safety & Correctness
2. Structural Changes & Render DifferencesTo prioritize repository maintainability and avoid duplicating entire Service/Activity files 5-6 times, we consolidated progressive steps of the same class into single helper files (
3. Rendered PreviewThe changes have been verified using an internal staging system. The rendered output matches the original documentation layout while incorporating the safety, correctness, and context improvements noted above. (Details shared separately). |
Bug: 359501045