Problem at reconnection

edited July 2018 in JavaScript

Hi, I have a few metawears cpro and Im using the last javascript SDK..

I can connect and I can see when the board is disconnected, but I can reconnect again with the same board.

I have research a lot of internet and I don't find anything that help me.

Here is my code. I also tried adding a true like second parameter to says that SCAN_DUPLICATES = TRUE, but dosnt work neither :(

var MetaWear = require('metawear');
MetaWear.discoverAll(function (device) {
device.once('disconnect', function () {
console.log(disconnect ${device._peripheral.advertisement.localName})
})
device.connectAndSetUp(function (error) {
if (error) {
console.log("error" + error)
}
console.log(connected ${device._peripheral.advertisement.localName})
});
},true);

Can you help me please? Thanks !

I also created a post in stackoverflow https://stackoverflow.com/questions/51305847/metawear-javascript-reconnection-fails

Comments

  • edited July 2018

    In the official forum of MetaWear is there zero information

    The thread that addresses this issue is on the 2nd page at time of making this post:
    https://mbientlab.com/community/discussion/2413/no-connection-timeout-using-javascript-sdk/p2

  • edited July 2018

    Sorry but, after read a few times, I still in same :( , inclusive more confused :neutral:

    Wich is the best way to make the reconnection ??

    One Way would be removing the listeners of the device, true ?

    That's is what I'm trying to do but with no results

    thanks and regards !

    edit:``

    I also added the snippet of code that is in your link, and I call it at the end of connectAndSetup :

    `function disconnectionHandler(board) {
    // Callback to execute when commands have been recorded.
    let commandsRecorded = () => {
    console.log('MetaWear commands have been recorded.');
    };

    // Retrieve event pointer representing a disconnect event
    let dc_event = MetaWear.mbl_mw_settings_get_disconnect_event(board);
    // Start recording MetaWear (!) commands that shall be executed when the disconnect event is fired.
    MetaWear.mbl_mw_event_record_commands(dc_event);
    // Switch accelerometer to standby.
    MetaWear.mbl_mw_acc_stop(board);
    // Issue a soft reset.
    MetaWear.mbl_mw_debug_reset(board);
    // End the command recording and execute the callback.
    MetaWear.mbl_mw_event_end_record(dc_event, MetaWear.FnVoid_EventP_Int.toPointer(commandsRecorded));
    

    }`

    With the same results :(

    EDIT2:

    I also tried only with noble and I have the same problem, so I intuit its not problem with the javascript SDK else with the metawear itself. What can I do ? Thanks again !

    ``var noble = require('noble');

    noble.on('stateChange', function (state) {
    if (state === 'poweredOn') {
    noble.startScanning();
    } else {
    noble.stopScanning();
    }
    });

    noble.on('discover', function (peripheral) {
    if (isMyBlePeripheral(peripheral.advertisement.localName)) {
    peripheral.connect(function (error) {
    console.log('connected to peripheral: ' + peripheral.uuid);
    });
    }
    peripheral.on('disconnect',function () {
    })
    });

    function isMyBlePeripheral(bleName) {
    const BLE_NAME_TO_SEARCH = "MetaWear"
    if (bleName !== undefined && bleName.indexOf(BLE_NAME_TO_SEARCH) !== -1) {
    return true
    }
    return false
    }`

  • Why always that the metawear its disconnect you call to process.exit(0);

    Cant do anything else in disconnect event ?

    Only reset the app¿¿

  • If it's a problem with the MetaWear board, then none of the Android, IOS, or Windows apps would be working if the board lost connection, which is not the case.

    The code snippet you copied was in response to the MetaWear SDK event listeners, so it's not relevant to your issue. You need to remove the Noble event listeners, which is detailed in the 2nd code snippet.

    Also, please wrap your code with code tags so they render properly on the page.

  • edited July 2018

    @Eric said:
    If it's a problem with the MetaWear board, then none of the Android, IOS, or Windows apps would be working if the board lost connection, which is not the case.

    The code snippet you copied was in response to the MetaWear SDK event listeners, so it's not relevant to your issue. You need to remove the Noble event listeners, which is detailed in the 2nd code snippet.

    Also, please wrap your code with code tags so they render properly on the page.

    Yes these code its not relevant to my issue but its good to know, to can program it the blink led at disconnect for example !

    And yes , I was triying to remove the Event Listener of noble but, I can't see where is >:) >:)

    Wich 2nd snippet you wanted to say ? Here I'm dont see, and in the link neither !

    Thanks again Eric !

  • edited July 2018

    OK its working finally !! o:) o:)

    `function startSearch() {
    console.log("start Search")

    MetaWear.discover(function (device) {
        connect(device)
    });
    
    function connect(device) {
        device.on('disconnect', function (error) {
    
            console.log("Disconnected");
            device._peripheral._noble._address = null
            startSearch()
        });
    
        device.connectAndSetUp(function (error) {
            console.log("Connected", device.address);
            blinkStartup(device)
            disconnectionHandler(device.board)
        });
    }
    

    }`

    The trick is set the device._peripheral._noble._address at null in disconnect event, y call the funcion to search again, thats works for me !

    edit:sorry but i cant put all my code in code styling :/

This discussion has been closed.