Control MagicBlue ble light bulb doesn't work

Posted on
  • Hello!

    I am trying without success since couple hours to make work a simple system: button press turns on a light bulb.

    Regarding the reverse engineering of the bulb https://medium.com/@urish/reverse-engineering-a-bluetooth-lightbulb-56580fcb7546

    I managed to control the bulb from a Cordova app, so the javascript should already be ready. I manage to connect to the device, to get the service and characteristic I need...but cannot make it to writeValue correctly (the bulb is not reacting). No error is thrown.

    I suppose the problem hides in the value passed - in Cordova through I library should be an ArrayBuffer - but here?

    My code

      LED1.write(false);
      LED2.write(false);
    var connected = false;
    
    setWatch(function() {
    //  NRF.findDevices(function(ds){
      //console.log(ds);
       if(!connected){ connecty();}
        else{console.log('already connected');}
      //},1000);
    }, BTN, {edge:"rising", debounce:50, repeat:true});
    
    
    function connecty() {
      var gatt;
      NRF.connect("c9:a2:57:28:a3:15 random").then(function(g) {
        connected=true;
        LED2.write(true);
        gatt = g;
        return gatt.getPrimaryService("0000ffe5-0000-1000-8000-00805f9b34fb");
      }).then(function(service) {
        return service.getCharacteristic("0000ffe9-0000-1000-8000-00805f9b34fb");
      }).then(function(chara) {
        LED3.write(true);
        chara.writeValue(makearr());
      }).then(function(a) {
        gatt.disconnect();
        connected=false;
        setTimeout(function(){
          LED2.write();
        LED3.write();},1000);
        console.log("Done!");
      }).catch(function(e){
      console.log('error');
        connected=false;
      });
    }
    
    
    function makearr(){
      var hex = 'ff085a';
     //'02','40','00','0e','00','0a','00','04','00',
      var arra = ['56',hex.substr(0,2),hex.substr(2,2),hex.substr(4,2),'00','f0','aa'];
      var bf = new Uint8Array(arra.length);
      for (var y in arra){
        bf[y]='0x'+arra[y];
      } 
      return bf.buffer;
    }
    
    

    Any suggestion will be very appreciated!

  • What you're doing looks ok... Can you try just sending the data explicitly with:

    function makearr(){
      return [0x56, 0xff, 0x08, 0x5a, 0x00, 0xf0, oxaa];
    }
    

    It'd remove one potential source of problems. I notice that what you're sending isn't related to what's in the comment at all - is that intentional?

  • @Gordon, thank you very much for support.

    Will try this evening if it works.

    I confirm, comment is totally unlinked from content.

  • Like a charm...thank you @Gordon!

  • Great! Looks like there's an issue with Espruino's conversion of strings into integers. For instance +"0x10" in the browser works fine but returns NaN in Espruino.

    For now I'd use parseInt("ff",16), but I'll file a bug for it

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

Control MagicBlue ble light bulb doesn't work

Posted by Avatar for Marko @Marko

Actions