yeeja is a Java implementation of Yeelight remote control protocol. yeeja implementation is based on Yeelight Inter-Operation Spec.
- Device discovery in local network
- Control commands
- State change notifications
- Add JitPack.io repository
repositories {
...
maven { url 'https://jitpack.io' }
}
- Add yeeja as dependency
dependencies {
...
implementation 'xyz.malkki:yeeja:1.1'
}
YeelightDiscovery discovery = new YeelightDiscovery(new DefaultDatagramSocketFactory(), new DefaultInetAddressFactory());
List<YeelightDevice> yeelights = disovery.discover();
for (YeelightDevice yeelight : yeelights) {
String id = yeelight.getId();
String address = yeelight.getAddress();
int port = yeelight.getPort();
YeelightModel model = yeelight.getModel();
}
YeelightDevice yeelight = /* YeelightDevice from previous example */
try (YeelightConnection connection = YeelightConnection.connect(new DefaultSocketFactory(), new DefaultInetAddressFactory(), yeelight, null /*No notification listener*/)) {
//Toggle power
connection.runCommand(new Toggle());
//Get power state and brightness
YeelightProps props = connection.runCommand(new GetProp("power", "bright"));
boolean power = props.getProp("power");
int brightness = props.getProp("bright");
} catch (YeelightCommandException e) {
//Failed to execute Yeelight command
int code = e.getCode(); //Error code returned by Yeelight
String message = e.getMessage(); //Error message returned by Yeelight
} catch (IOException e) {
e.printStackTrace();
}
YeelightDevice yeelight = /* YeelightDevice */
YeelightNotificationListener notificationListener = new YeelightNotificationListener() {
@Override
public void onNotification(@NotNull YeelightProps props) {
if (props.hasProp("power")) {
boolean powerOn = props.getProp("power");
System.out.println("Yeelight turned "+(powerOn ? "on" : "off"));
}
}
@Override
public void onError(@NotNull IOException e) {
//Connection error
e.printStackTrace();
}
};
try (YeelightConnection connection = YeelightConnection.connect(new DefaultSocketFactory(), new DefaultInetAddressFactory(), yeelight, notificationListener)) {
connection.runCommand(new Toggle());
} catch (IOException | YeelightCommandException e) {
e.printStackTrace();
}
On Android devices, you might want to specify which network is used for Yeelight connections. This can be done by creating factories that use Android's Network API.
For example, a SocketFactory that binds sockets to a specific network:
public class NetworkSocketFactory implements SocketFactory {
private Network network;
public NetworkSocketFactory(Network network) {
this.network = network;
}
@Override
public Socket create() {
Socket socket = new Socket();
network.bindSocket(socket);
return socket;
}
}