timothyb89 / lifx-java

A Java library for interacting with LIFX WiFi LED bulbs
MIT License
13 stars 13 forks source link

lifx-java

An unofficial Java library for interacting with LIFX WiFi LED bulbs, targetting both desktop Java 7 and recent Android releases (4.4+, requires full JDK 7 support). This library would not have been possible without the work of the lifxjs project.

Current State

This library isn't yet complete and may have unintended bugs. Currently API support exists for bulb discovery, power management (on / off), color changing, and event notifications. Most of the interesting packet types have at least been defined and can be sent and received.

Some features still needing to be implemented:

Why use this?

An official Android API was released shortly after this library. There are some advantages to using this library over the official SDK:

On the other hand, the official SDK:

Requirements

EventBus is now in Maven Central and does not need to be installed manually.

Building

mvn install

Quickstart

The rough procedure for getting access to a Gateway or a Bulb object (primarily what is interacted with) is:

  1. Use a BroadcastListener to search for gateway bulbs
  2. Wait for a GatewayDiscoveredEvent, connect to the gateway
  3. Send packets directly to the gateway, or wait for a GatewayBulbDiscoveredEvent

In code:

public class MyClient {

    public TestClient() throws IOException {
        BroadcastListener listener = new BroadcastListener();
        listener.bus().register(this);
        listener.startListen();
    }

    @EventHandler
    public void gatewayFound(GatewayDiscoveredEvent ev) {
        Gateway g = ev.getGateway();
        g.bus().register(this);

        try {
            g.connect(); // automatically discovers bulbs
        } catch (IOException ex) { }
    }

    @EventHandler
    public void bulbDiscovered(GatewayBulbDiscoveredEvent event) throws IOException {
        // register for bulb events
        event.getBulb().bus().register(this);

        // send some packets
        event.getBulb().turnOff();
        event.getBulb().setColor(LIFXColor.fromRGB(0, 255, 0));
    }

    @EventHandler
    public void bulbUpdated(BulbStatusUpdatedEvent event) {
        System.out.println("bulb updated");
    }

}

Wrapper methods for some common commands have been added (e.g. Bulb.turnOff(), etc), although many packet types don't have a nice API yet (or haven't been implemented at all). For now all defined packet types can be found in the package org.timothyb89.lifx.net.packet.

Events

Events will be pushed to EventBus instances for various objects. For each class listed below, you can use:

// call to begin receiving events
instance.bus().register(someObject);

// someObject will be scanned for methods annotated with @EventHandler
@EventHandler
public void someEvent(SomeEventClass clazz) {
    // do something
}

BroadcastListener

Gateway

Bulb

Android support

This library will happily work on Android or on normal JVMs, and is also being used in the lifx-tasker project. However, it takes advantage of Java 7 features and will require Android 4.4 or higher to run.

Specifically, it will attempt to discover and create MulticastLocks, which are required to receive multicast packets on Android devices. However, no binary dependency is created on any Android APIs, so the library can still be used on normal JVMs without any trouble. (This feature was unabashedly stolen (with some adaptions) from AndroidLIFX.)

Also note that you'll need the slf4j android plugin to get log messages.