Avatar for tronic98776

tronic98776

Member since Mar 2019 • Last active May 2019
  • 2 conversations
  • 15 comments

Most recent activity

  • in Puck.js, Pixl.js and MDBT42
    Avatar for tronic98776

    OK, thanks @Gordon, so I think I understand the problem and why it won't work with the firmware as it is.

    It seems from this that what you want is actually a lot simpler than that.

    I'm afraid not.

    I assumed you wanted to be able to connect with another device while stopping the phone connecting

    This is correct. I want to connect the Puck to Phone A, send an HID string to it based on x-event, then, based on y-event I want the Puck to disconnect from Phone A or power-down and then be discoverable and able to connect to Phone B without Phone A ever auto-reconnecting.

    I assume that using an E.on function as well as an onInit won't solve my problem? as they are essentially the same thing?

    Is there a way to set the Puck to connect without bonding? Would that even work?

    Is there a way other than changing the MAC address of making the Puck appear to be a different/unknown device to a previously paired Phone?

    Can you think of any other ways around this other than a firmware update?

  • in Puck.js, Pixl.js and MDBT42
    Avatar for tronic98776

    So, if I follow your steps above it works.
    But it doesn't work if I add it to the onInit.

    Here are the first few lines of my code:

    function onInit() {
    var kb = require("ble_hid_keyboard");
    NRF.setAddress("c3:92:e6:f6:f6:f6 random");
    NRF.setServices(undefined, { hid : kb.report });
    
    

    Also, when I add the setAddress to my onInit it seems to also prevent the HID service from starting, as when I connect to my android with the onInit as above it no longer connects as a BLE HID.

  • in Puck.js, Pixl.js and MDBT42
    Avatar for tronic98776

    So I'm still having no success with this:

    NRF.setAddress("yy:yy:xx:xx:xx:xx random");
    
    

    With yy:yy being the same as the original hardcoded MAC address to match the address type and avoid the INVALID_BLE_ADDR.

    The Puck still advertises and connects to my phone with the original hard-coded MAC address.

    The exact steps I have taken.
    I am using the web IDE in chrome. On the right hand side I have my BLE HID code (which is all functioning correctly). I connect the Puck to the web Ide. Upload my code from the RHS on to the puck. I then enter NRF.setAddress("C3:92:xx:xx:xx:xx random"); into the command on the left hand side. It returns undefined. I type save(). I type NRF.disconnect(). The Puck disconnects from the web ide. I restart the Puck, by lifting and releasing the battery. I then open the bluetooth settings on my android phone, scan for a new device, I find the Puck, connect and pair with it. I then click on the Puck in my Android bluetooth settings to check the MAC address and it displays the same hard-coded address that is returned when i type NRF.getAddress() into the ide. So as far as I can tell I am not successfully changing the MAC address.

    I just tried here and it works fine

    @Gordon, can you please tell me that exact steps you took to get the change to work

    I really appreciate your help on this Gordon.

  • in Puck.js, Pixl.js and MDBT42
    Avatar for tronic98776

    Thank you @Gordon.

    Just to make sure I understand - you're connecting to the Puck from a device like a PC or phone?

    That is correct I am using the Puck as a Ble HID Keyboard, I want to connect it to my phone, use it to pass a string of characters to the phone (based on an event), then (based on another event) I want to disconnect the Puck from the phone (without touching the phone) and have it not auto-reconnect to the phone.

    I had looked at changing the MAC address using each of the 3 variations as I wasn't sure from reading the documentation what each type of address was. It seemed a little confusing to me have a static address that is random?

    NRF.setAddress("xx:xx:xx:xx:xx:xx");
    NRF.setAddress("xx:xx:xx:xx:xx:xx public");
    NRF.setAddress("xx:xx:xx:xx:xx:xx random");
    
    

    But none of them seemed to have any effect on the device's mac address. I including the line in my onInit() function, I tried having it outside of the function, uploading my code and using the save() command. I have also tried entering it into the command line directly. When entering into the command line on the web IDE, it returns "undefined", then when I check for the change by entering NRF.getAddress(), it just returns the original default MAC address of the Puck. I have tried save(), on the device, then power cycling the puck, but the changes are still not working.

    If I can get the MAC address to successfully change with the NRF.setAddress, then this should work as a solution, because I can create a random string on init of the Puck, which will hopefully prevent reconnection to the phone as it will not appear to be a previously paired/bonded device.

    On another note, (although this wasn't a solution for me) I was able to change the advertised name with:

    NRF.setAdvertising({},{name:"Hello"});
    

    But I couldn't find a way of resetting this back to the default "puck.js xxxx" where I think the xxxx was the last few characters of the MAC address. Is there a command to reset this back to factory default?

  • in Puck.js, Pixl.js and MDBT42
    Avatar for tronic98776

    I'm trying to prevent the puck from automatically reconnecting to a device that it has previously been connected to and paired with. So after the puck has been connected and paired with a device, I want to action a reset/power cycle of the puck and for it to not connect to the device again unless I go through the connection process again on the device.

    I know that I can just select "forget this device" from bluetooth settings on the device, but I want a way of implementing this from the puck itself (ie via a button press).

    My initial plan was to set it to create a device name containing a random string on init, and then calling a reset() on the button press. This plan failed as the phone/laptop device that the puck has been paired with ignores the fact that the bluetooth device name has changed and still reconnects. I assume it looks at the underlying MAC address instead.

    Does anybody know a way of implementing this?

    Perhaps there is a mode I can set which enables connection without pairing?

    Many thanks.

    • 31 comments
    • 1,014 views
  • in Puck.js, Pixl.js and MDBT42
    Avatar for tronic98776

    You are right. I hadn't retried it after I removed the logging. It's working now without the timeout. Thanks again.

  • in Puck.js, Pixl.js and MDBT42
    Avatar for tronic98776

    Thank you @allObjects. I have this working now. Initially it had the same error, but after increasing the setTimeout from 1 to 50, it worked in the console. However, it would fail if anything was added to FIFO string whilst a send was in progress, but increasing the setTimeout to 100 has cured this.

    To get it to type the string over bluetooth I had to get rid of all the console.logs. Here is the code I have working now:

    
    function onInit() {
      var kb = require("ble_hid_keyboard");
    NRF.setServices(undefined, { hid : kb.report });
    
    // setup of you convenience hidKeyBoardObject... (here or later)
    var hidKBD = ( { string: ""
                    
    , type: function(s) { 
        this.string += s; 
        if (this.string.length == s.length) { 
                          
                          
          this._send(this.string.charAt(0)); // get first char out
      } }
    ,   _send: function(c) {
           // console.log("About to send char",c);
            setTimeout(function(_){
                kb.tap(kb.KEY[c],0,_._sent.bind(_)); },100,this);
        }
    ,   _sent: function() {
            //console.log("Char",this.string.charAt(­0),"has been sent / tapped");
            this.string = this.string.substring(1);
            //console.log("Left to send / tap is",this.string);
            if (this.string.length > 0) {
              //  console.log("Since length of this.string //is",this.string.length
      //                     ,"and is > 0, next char 'in line' -"
                           //,this.string.charAt(0)," - has to be sent / tapped.");
                this._send(this.string.charAt(0));
            } //else {
              //  console.log("Since nothing is left to tap / send - all is tapped");
                //console.log("/ sent, (FIFO  is empty), nothing has to be done");
                //console.log(" anymore and  'logical recursion' stops/ends  here.");
            //}
        }
    });
    
    // setup and other code functions of yours (here or before hidKBD)
       // invoke your set up code as needed (init and getting connections)
       function startSend(){
         hidKBD.type("ABCCANNONBALL");}
      // hidKBD.type("XYZ");
    
        
    setWatch(startSend, BTN, {edge:"rising",repeat:true,debounce:50})­;
    }
    
    
    
  • in Puck.js, Pixl.js and MDBT42
    Avatar for tronic98776

    By "playing around", I was using an editor and the console in Chrome to try and get my head around callbacks. In the end I went through a number of the tutorials on w3schools and think I have a better idea of it now. I wanted to add console.logs so I could see exactly when/where the code was failing because I would need to be disconnected from the IDE to test the BLE HID sending.

    I am conscious that I am taking up your time in what may be considered my JS education rather than anything Espruino specific. I am grateful of everybody who has contributed to this thread.

    @allObjects, I tried implementing your updated code with the console.logs. (here is the entire script)

    
    var kb = require("ble_hid_keyboard");
    NRF.setServices(undefined, { hid : kb.report });
    
    // setup of you convenience hidKeyBoardObject... (here or later)
    var hidKBD = ( { string: ""
                    
    , type: function(s) { 
        this.string += s; 
        if (this.string.length == s.length) { 
                          
                          
          this._send(this.string.charAt(0)); // get first char out
      } }
    ,   _send: function(c) {
            console.log("About to send char",c);
            kb.tap(kb.KEY[c],0,this._sent.bind(this)­);
        }
    ,   _sent: function() {
            console.log("Char",this.string.charAt(0)­,"has been sent / tapped");
            this.string = this.string.substring(1);
            console.log("Left to send / tap is",this.string);
            if (this.string.length > 0) {
                console.log("Since length of this.string is",this.string.length
                           ,"and is > 0, next char 'in line' -"
                           ,this.string.charAt(0)," - has to be sent / tapped.");
                this._send(this.string.charAt(0));
            } else {
                console.log("Since nothing is left to tap / send - all is tapped");
                console.log("/ sent, (FIFO  is empty), nothing has to be done");
                console.log(" anymore and  'logical recursion' stops/ends  here.");
            }
        }
    });
    
    // setup and other code functions of yours (here or before hidKBD)
       // invoke your set up code as needed (init and getting connections)
       function startSend(){
         hidKBD.type("ABC");}
      // hidKBD.type("XYZ");
    
        
    setWatch(startSend, BTN, {edge:"rising",repeat:true,debounce:50})­;
    
    
    
    

    I can now see that I am getting an error after the second character has been sent, here is the full console output from espruino IDE:

    BLE Connected, queueing BLE restart for later
    About to send char A
    Char A has been sent / tapped
    Left to send / tap is BC
    Since length of this.string is 2 and is > 0, next char 'in line' - B - has to be sent / tapped.
    About to send char B
    Char B has been sent / tapped
    Left to send / tap is C
    Since length of this.string is 1 and is > 0, next char 'in line' - C - has to be sent / tapped.
    About to send char C
    Uncaught Error: Got BLE error 0x3004 (NO_TX_PACKETS)
    at line 1 col 104
    ...0,0,0],function(){a&&a()})})

                              ^
    

    in function "tap" called from line 16 col 49

        kb.tap(kb.KEY[c],0,this._sent.bind(this)­);
                                                ^
    

    in function "_send" called from line 26 col 45

            this._send(this.string.charAt(0));
                                            ^
    

    in function "a" called from line 1 col 6
    a&&a()

     ^
    

    in function called from system
    ab

Actions