Skip to content

Local/ble gatt object references fix#212

Draft
vicocz wants to merge 3 commits intodefaultfrom
local/ble-gatt-object-references-fix
Draft

Local/ble gatt object references fix#212
vicocz wants to merge 3 commits intodefaultfrom
local/ble-gatt-object-references-fix

Conversation

@vicocz
Copy link
Owner

@vicocz vicocz commented Mar 17, 2026

No description provided.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens BLE reconnection behavior (especially on Android) by ensuring cached GATT/service/characteristic references are cleared during disconnect and by reducing reliance on stale Android BluetoothGatt state across reconnects. It also refines LEGO Control+/TechnicMove handling by making port index mapping safer and by adding a feedback-driven completion path for PLAYVM calibration.

Changes:

  • Introduces a OnDeviceDisconnecting() hook on BluetoothDevice and implements it across device types to clear cached characteristic references before native disconnect/cleanup.
  • Adds an Android BLE “refresh service cache” reflection call during GATT disconnect/close to mitigate stale service/characteristic caching between reconnects.
  • Updates ControlPlus/TechnicMove logic: safer port-id-to-channel mapping, a dedicated output-feedback callback hook, and a PLAYVM calibration wait based on hub feedback (with timeout fallback).

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
BrickController2/BrickController2/DeviceManagement/Wedo2Device.cs Clears cached characteristic references on disconnect to avoid stale native objects after reconnect.
BrickController2/BrickController2/DeviceManagement/Vengit/SBrickLightDevice.cs Clears cached characteristic references on disconnect.
BrickController2/BrickController2/DeviceManagement/TechnicMoveDevice.cs Filters unsupported configurations, adds feedback-based PLAYVM calibration wait, and updates port/channel mapping for non-index port IDs.
BrickController2/BrickController2/DeviceManagement/SBrickDevice.cs Clears cached characteristic references on disconnect.
BrickController2/BrickController2/DeviceManagement/PfxBrickDevice.cs Clears cached characteristic references on disconnect.
BrickController2/BrickController2/DeviceManagement/MouldKing/MK_DIY.cs Clears cached characteristic reference on disconnect.
BrickController2/BrickController2/DeviceManagement/Lego/RemoteControl.cs Clears cached characteristic reference on disconnect.
BrickController2/BrickController2/DeviceManagement/ControlPlusDevice.cs Adds disconnect cleanup, safe port-id mapping via TryGetChannelIndex, feedback hook for message 0x82, and debug-only logging.
BrickController2/BrickController2/DeviceManagement/CircuitCubeDevice.cs Clears cached characteristic references on disconnect.
BrickController2/BrickController2/DeviceManagement/BuwizzDevice.cs Clears cached characteristic reference on disconnect.
BrickController2/BrickController2/DeviceManagement/BuWizz3Device.cs Clears cached characteristic references on disconnect.
BrickController2/BrickController2/DeviceManagement/BuWizz2Device.cs Clears cached characteristic references on disconnect.
BrickController2/BrickController2/DeviceManagement/BluetoothDevice.cs Adds abstract OnDeviceDisconnecting() and invokes it as part of the disconnect sequence; makes disconnect callback invocation more robust.
BrickController2/BrickController2.Android/PlatformServices/BluetoothLE/BluetoothLEDevice.cs Adds reflective BluetoothGatt.refresh() call before close/dispose to reduce stale service caching across reconnects.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +110 to 115
// if PLAYVM enabled, suppress A / B / C channels — all controlled via PLAYVM commands,
// and the hub is still settling after PLAYVM_CALIBRATE_STEERING when the output task starts
// or if LED channels (3-8) all share PORT_6LEDS — suppress initial burst to avoid flooding the hub's BLE receive buffer
if ((_applyPlayVmMode && channel < CHANNEL_C) ||
(channel > CHANNEL_C && channel < NumberOfChannels))
{
@vicocz vicocz requested a review from Copilot March 18, 2026 21:44
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens Bluetooth LE reconnection behavior by introducing a disconnect hook to clear cached GATT characteristic references (avoiding stale native Android objects), and adds an Android-specific GATT service-cache refresh on disconnect. It also improves Control+ port-to-channel mapping safety and enhances Technic Move PLAYVM calibration handling.

Changes:

  • Add OnDeviceDisconnecting() lifecycle hook to clear cached IGattCharacteristic references across BLE devices before native disconnect.
  • Add Android BluetoothGatt.refresh() (reflection) on disconnect to reduce stale service caching between reconnects.
  • Improve Control+ message parsing with TryGetChannelIndex(...) and update Technic Move PLAYVM calibration to wait for hub feedback (with timeout fallback).

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated no comments.

Show a summary per file
File Description
BrickController2/BrickController2/DeviceManagement/BluetoothDevice.cs Adds OnDeviceDisconnecting() hook and ensures callback handling is safe during disconnect.
BrickController2/BrickController2/DeviceManagement/ControlPlusDevice.cs Switches to TryGetChannelIndex to avoid invalid indexing and adds a disconnect cleanup for cached characteristic.
BrickController2/BrickController2/DeviceManagement/TechnicMoveDevice.cs Filters unsupported channel configs, improves init suppression for LED/PLAYVM, and waits for PLAYVM calibration feedback instead of fixed delays.
BrickController2/BrickController2/DeviceManagement/Wedo2Device.cs Clears cached characteristics on disconnect.
BrickController2/BrickController2/DeviceManagement/SBrickDevice.cs Clears cached characteristics on disconnect.
BrickController2/BrickController2/DeviceManagement/Vengit/SBrickLightDevice.cs Clears cached characteristics on disconnect.
BrickController2/BrickController2/DeviceManagement/PfxBrickDevice.cs Clears cached characteristics on disconnect.
BrickController2/BrickController2/DeviceManagement/MouldKing/MK_DIY.cs Clears cached characteristic on disconnect.
BrickController2/BrickController2/DeviceManagement/Lego/RemoteControl.cs Clears cached characteristic on disconnect.
BrickController2/BrickController2/DeviceManagement/CircuitCubeDevice.cs Clears cached characteristics on disconnect.
BrickController2/BrickController2/DeviceManagement/BuwizzDevice.cs Clears cached characteristic on disconnect.
BrickController2/BrickController2/DeviceManagement/BuWizz2Device.cs Clears cached characteristics on disconnect.
BrickController2/BrickController2/DeviceManagement/BuWizz3Device.cs Clears cached characteristics on disconnect.
BrickController2/BrickController2.Android/PlatformServices/BluetoothLE/BluetoothLEDevice.cs Adds reflected BluetoothGatt.refresh() before closing GATT to mitigate stale service cache on Android.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants