Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ private String runAdbShell(int commandTimeout, String... args)
String result = commandFuture.get(commandTimeout, TimeUnit.SECONDS);
Exception error = streamError.get();
if (error != null) {
throw new AndroidDebugBridgeDeviceException(
throw new AndroidDebugBridgeDeviceStreamRejectedException(
Comment thread
wborn marked this conversation as resolved.
"Error opening adb shell stream " + ip + ":" + port + ": " + error.getMessage());
}
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.androiddebugbridge.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Thrown when the device refuses to open the adb shell stream, which a device in standby commonly
* does. It is a distinct type because the shell command provably never ran, so the caller may
* safely reconnect and retry it without risking a duplicate execution.
*
* @author Stamate Viorel - Initial contribution
*/
@NonNullByDefault
public class AndroidDebugBridgeDeviceStreamRejectedException extends AndroidDebugBridgeDeviceException {
private static final long serialVersionUID = 5471982041566281957L;

public AndroidDebugBridgeDeviceStreamRejectedException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,21 @@ public void handleCommand(ChannelUID channelUID, Command command) {
// try reconnect
adbConnection.connect();
}
handleCommandInternal(channelUID, command);
try {
handleCommandInternal(channelUID, command);
} catch (AndroidDebugBridgeDeviceStreamRejectedException e) {
// Socket.isConnected() only records that a connection was once established; it stays
// true after the device stops serving the socket, so the check above cannot notice a
// device that went into standby and the shell stream open is refused instead. The
// command provably never ran, so reconnect and run it once more rather than dropping
// it -- otherwise the first command after standby (typically the KEYCODE_WAKEUP meant
// to end it) is always lost.
logger.debug("{} - shell stream rejected, reconnecting and retrying command: {}", currentConfig.ip,
e.getMessage());
adbConnection.disconnect();
Comment thread
wborn marked this conversation as resolved.
Outdated
adbConnection.connect();
handleCommandInternal(channelUID, command);
}
} catch (InterruptedException ignored) {
} catch (AndroidDebugBridgeDeviceException | ExecutionException e) {
if (!(e.getCause() instanceof InterruptedException)) {
Expand Down
Loading