Write Characteristic to Web Bluetooth.

Posted on
  • I have a pixl that currently advertises a custom service and characteristic which a puck can then discover connect to and write to.
    I would like to replicate this with webbluetooth so that the puck can control a browser app instead of the pixl, is that possible? obviously doing something on the page instead of turning on the LED
    Below is the code on my pixl.

    NRF.setServices({
      "60970001-dddd-49b8-acea-2b7f5d2660dd": {
        "60970002-dddd-49b8-acea-2b7f5d2660dd": {
          value : [0],
          maxLen : 1,
          writable : true,
          onWrite : function(evt) {
            digitalWrite(LED, evt.data[0]);
            digitalWrite(D13, evt.data[0]);
            
          }
        }
      }
    }, { uart : true });
    
    
    // On disconnect
    NRF.on('disconnect', function() {
      digitalWrite(LED,0);
      digitalWrite(D13,0)
    });
    // Change name 
    NRF.setAdvertising({}, {name:"BLEPTT"});6
    
  • I think I answered this at JSOxford, but if anyone else is interested:

    Web Bluetooth doesn't let you create services on the device with the Web Browser.

    However to 'push' data from Espruino to a device with a Web Bluetooth browser, you just need to use notifications: http://www.espruino.com/About+Bluetooth+LE#services-and-characteristics

    So on Espruino you do:

    NRF.setServices({
      0xBCDE : {
        0xABCD : {
          value : "Hello",
          maxLen : 5, 
          notify : true
    }}});
    function pushData(d) {
     NRF.updateServices({
      0xBCDE : {
        0xABCD : {
          value : d,
          notify : true
     }}});
    }
    

    And then on Web Bluetooth you get the characteristic as normal and then call startNotifications on it, which causes a characteristicvaluechanged event on it whenever you update the characteristic on Espruino.

    I don't think there's any info on the Espruino site about that yet, but there is a workshop I did which has the relevant code: https://github.com/gfwilliams/workshop-thingy52/blob/master/step5.md

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Write Characteristic to Web Bluetooth.

Posted by Avatar for sammachin @sammachin

Actions