Problem at reconnection
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
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
Sorry but, after read a few times, I still in same , inclusive more confused
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.');
};
}`
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.
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 !
OK its working finally !!
`function startSearch() {
console.log("start Search")
}`
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