Note: Web Bluetooth currently works on Mac OS, Android, Chromebook, Linux and
iOS (with this app). Google
were supposed to add Web Bluetooth support for Windows in early 2017, but this has
not yet happened - however there are workarounds.

To work, Web Bluetooth needs to be run from a website that's served over HTTPS
(not HTTP). While you can set one up yourself with Let's Encrypt,
we're not going to cover that here. Instead, we'll use GitHub Pages.

Click on the button marked On! and you should be presented with a window
like the following:

![](Puck.js Web Bluetooth/scanning.png)

If you choose your Puck.js device and click Pair, you should see the Red
LED light up on it after a few seconds. You can now click the Off! button
to turn it off, and it should turn off.

So what happened? The HTML above creates two buttons, and when they are
executed they call the Puck.write function, which is in the puck.js
script file that we loaded up right before.

Puck.write sends the string you pass to it directly to Puck.js
as a command. You need the \n (newline character) to tell Espruino that
it's the end of the command and it should execute it.

Can I send commands to Puck.js as soon as the page loads? Unfortunately
not - as a security precaution, Web Bluetooth implementations can only
connect to a Bluetooth LE device in response to user input. After that you
can do what you want though.

The example above is pretty basic - let's try and make something
that looks better.

First, we'll find a lightbulb icon. SVG is a nice choice as it looks good
when it's scaled up, and we can include the whole image in our HTML file.

Extra Features

The Puck.js library also has a Puck.setTime(optional_callback) function
which will set Puck.js's time to your computer's time. This can be great
if you're making some time-based device and you want to be sure that
the clock is always set correctly.

Reading from Puck.js

At the moment, all we're doing is sending data to Puck.js - not getting
anything back.

However, getting values back is quite easy. While you're connected to
Puck.js with the webpage you made, open up the Developer tools in Chrome
with Ctrl + Shift + I, F12, or from the menu (More tools -> Developer tools).

Now, enter the following in the Console window:

Puck.eval("BTN.read()",function(x) { console.log(x); })

This will print false to the console. However, if you press Puck.js down
and do it again, it'll print true.

This is evaluating a command on Puck.js (note there's no newline needed),
and then calling the callback with the result.

So why isn't this a normal function call that returns a value? Well,
it takes time to send the data to Puck.js and get a response back. If your
code waited for a response then the whole webpage would grind to a halt.

This way, you provide a function which is called back when data arrives, and
everything else keeps working.

So how would you use this? You could modify the light example above to
change the colour of the webpage icon depending on the amount of light
the Puck can sense.

Just create a new HTML file in GitHub as you did above and paste the
following code in:

Now, when you click the light bulb it'll connect to Puck.js, and you can
change the colour by shining light on or covering up your Puck.

Note: we're only requesting new data after we have got the last set of
data. This is better than using something like setInterval, as if there
are transmission errors, multiple requests for data could end up piling up.

This works, and it's easy - but it's not very fast.

Two way communications

What would be better is if we had lower-level control. We could then just
tell Espruino to automatically send data over Bluetooth (without being prompted)
and we could just handle it directly.

That's what you can do with Puck.connect(callback). Once connected it calls the callback
function with the connection, which you can then use to send and receive data.

Note: You can't use Puck.connect and Puck.write/eval on the same
connection at the same time. If you want to write to a Puck after having
used Puck.connect, you need to use connection.write and handle
any response in the connection.on("data", handler.

Registers a data handler with connection.on("data", ...), which then
calls onLine each time a line is received.

Sends reset() to Puck.js - while not required this performs a software
reset, which would clear out any previously send code.

Waits for the reset to complete. The reset is fast, but sending the text
created bythe reset itself is slow!

Sends setInterval(function(){Bluetooth.println(Puck.light());},100); - this
causes Espruino to write the current light value down the Bluetooth link every
100ms.

When each line is received, onLine gets called and it updates the color
of the light icon.

NRF.on('disconnect', function() {reset()}); is also sent. This ensures that
when disconnected, Puck.js resets itself - which makes sure that there isn't
a setInterval left running that will flatten your battery. If you're making
something with this you'll almost certainly want to be a bit more subtle -
using clearInterval to remove just the interval you started.

Note: We use Bluetooth.println not console.log because writing to the
console would cause the > prompt character to be removed, the text to be
written, and then > to be written again. By writing direct to Bluetooth the
console device is unaware of what's going on and doesn't output any extra
characters.