Skip to content

Latest commit

 

History

History
125 lines (99 loc) · 3.72 KB

README.md

File metadata and controls

125 lines (99 loc) · 3.72 KB

Build Status codecov GitHub

yeeja

yeeja is a Java implementation of Yeelight remote control protocol. yeeja implementation is based on Yeelight Inter-Operation Spec.

Supported features

  • Device discovery in local network
  • Control commands
  • State change notifications

Download

  1. Add JitPack.io repository
repositories {
	...
	maven { url 'https://jitpack.io' }
}
  1. Add yeeja as dependency
dependencies {
	...
	implementation 'xyz.malkki:yeeja:1.1'
}

Examples

Discover Yeelights in local network

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();
}

Run command

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();
}

Listen state

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();
}

Use a specific network (Android)

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;
    }
}

Documentation