While Android Lollipop added a flashlight toggle into Quick Settings, circumventing most third-party torch apps, the function was only accessible in the notification drop-down and as an on/off switch. If you wanted to use the flashlight with morse code, for signaling, or other patterns, you still had to use a separate application and developers of said apps didn't have any clear API to build their software on. They had to hack together solutions for various phones, relying on whatever way the different OEMs had created to access the camera's flash.

With Android M, a new Flashlight API is accessible to developers with CameraManager.setTorchMode(). The flash will be switched on until the app is closed, it is toggled off, or some other app takes over control — flash isn't restricted or exclusive to any apps.

Developers can also check the flashlight's status with CameraManager.registerTorchCallback(), and code actions that get triggered when the mode changes with CameraManager.TorchCallback.onTorchModeChanged().

Here's the relevant part (starting at 29:01) of the What's New in Android talk at I/O 2015:

Alternative title: Writing Flashlight Apps For Android Is No Longer Torch-ure