Android + Kotlin / Java API (Unofficial)

Hello Yeelight Forum,

I’ve developed an unofficial API for controlling the devices based on operation spec.

Includes:

  • pure JAVA core module
  • Kotlin ViewModel and LiveData implementations
  • demo application

You can find compiled demo apk in releases or browse entire repository:

Note that the demo itself relies on running a successful device discovery so it might have problems functioning on emulators (due to emulated network).

Sample implementation:

class DemoActivity : AppCompatActivity(){
    var myDevices : YeelightDeviceMap? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val yeelightViewModel = ViewModelProviders.of(this).get(YeelightActiveViewModel::class.java)

        yeelightViewModel.devices.observe(this, Observer {devices ->
            // we get a map of [id, YeelightDevice] here
            Log.d("demo", "there are ${devices.size} devices")
            myDevices = devices
        })
    }

    fun toggleDevicePower(){
        myDevices?.values?.firstOrNull()?.let{ device ->
            device.togglePower()
        }
    }
}

All that’s needed to scan and start listening to device announcements here is observe the LiveData. When activity is paused all connections and sockets are stopped and new scan/listening is started as soon as activity is resumed again.

Additionally LiveData will update its value whenever any device property is changed (due to socket response or after announcement) so current state of the lights should always be available (in this example in myDevices).

Note that I’ve only performed limited testing myself (with 1 phone and rgb bulb v1/rgb stripe). There’s no standalone documentation but feel free to browse though the source as there are docs there.

3 Likes

Really cool, good job :+1: