Skip to content

Commit 5cd6ccd

Browse files
cagnuleinclaude
andcommitted
Fix Garmin ConnectIQ crash with malformed messages
Fixes app crash when receiving corrupted or malformed messages from Garmin ConnectIQ devices that cause IllegalArgumentException or BufferUnderflowException during deserialization. Changes: - IQMessageReceiverWrapper: Changed catch block from specific exception types to generic Exception to handle all deserialization errors including wrapped exceptions - Garmin: Refactored initialization to create wrapped context BEFORE getInstance() call, ensuring ALL BroadcastReceivers registered by the SDK (including during getInstance) pass through the wrapper Root cause: The SDK was receiving the original context in getInstance() before the wrapper was created, so some receivers bypassed the wrapper's exception handling. Now the wrapped context is created first and passed to both getInstance() and initialize(). Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 2e0bd25 commit 5cd6ccd

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

src/android/src/Garmin.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ public static void init(Context c) {
8282
new Handler(Looper.getMainLooper()).post(new Runnable() {
8383
@Override
8484
public void run() {
85-
connectIQ = ConnectIQ.getInstance(c, ConnectIQ.IQConnectType.WIRELESS);
85+
// Create wrapped context BEFORE getInstance to ensure all SDK operations use it
86+
context = createWrappedContext(c);
87+
88+
connectIQ = ConnectIQ.getInstance(context, ConnectIQ.IQConnectType.WIRELESS);
8689

8790
// init a wrapped SDK with fix for "Cannot cast to Long" issue viz https://forums.garmin.com/forum/developers/connect-iq/connect-iq-bug-reports/158068-?p=1278464#post1278464
88-
context = initializeConnectIQWrapped(c, connectIQ, false, new ConnectIQ.ConnectIQListener() {
91+
initializeConnectIQWithContext(connectIQ, false, new ConnectIQ.ConnectIQListener() {
8992

9093
@Override
9194
public void onInitializeError(ConnectIQ.IQSdkErrorStatus errStatus) {
@@ -158,12 +161,8 @@ public void sendMessageToWatch(String message, ConnectIQ.IQSendMessageListener l
158161
connectIQ.sendMessage(getDevice(), getApp(), message, listener);
159162
}
160163

161-
private static Context initializeConnectIQWrapped(Context context, ConnectIQ connectIQ, boolean autoUI, ConnectIQ.ConnectIQListener listener) {
162-
if (connectIQ instanceof ConnectIQAdbStrategy) {
163-
connectIQ.initialize(context, autoUI, listener);
164-
return context;
165-
}
166-
Context wrappedContext = new ContextWrapper(context) {
164+
private static Context createWrappedContext(Context context) {
165+
return new ContextWrapper(context) {
167166
private HashMap<BroadcastReceiver, BroadcastReceiver> receiverToWrapper = new HashMap<>();
168167

169168
@Override
@@ -190,6 +189,18 @@ public void unregisterReceiver(BroadcastReceiver receiver) {
190189
if (wrappedReceiver != null) super.unregisterReceiver(wrappedReceiver);
191190
}
192191
};
192+
}
193+
194+
private static void initializeConnectIQWithContext(ConnectIQ connectIQ, boolean autoUI, ConnectIQ.ConnectIQListener listener) {
195+
connectIQ.initialize(context, autoUI, listener);
196+
}
197+
198+
private static Context initializeConnectIQWrapped(Context context, ConnectIQ connectIQ, boolean autoUI, ConnectIQ.ConnectIQListener listener) {
199+
if (connectIQ instanceof ConnectIQAdbStrategy) {
200+
connectIQ.initialize(context, autoUI, listener);
201+
return context;
202+
}
203+
Context wrappedContext = createWrappedContext(context);
193204
connectIQ.initialize(wrappedContext, autoUI, listener);
194205
return wrappedContext;
195206
}

src/android/src/IQMessageReceiverWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public void onReceive(Context context, Intent intent) {
3131

3232
try {
3333
receiver.onReceive(context, intent);
34-
} catch (IllegalArgumentException | BufferUnderflowException e) {
35-
QLog.d(TAG, e.toString());
34+
} catch (Exception e) {
35+
QLog.d(TAG, "Exception in Garmin message receiver: " + e.toString());
3636
}
3737
}
3838

0 commit comments

Comments
 (0)