Skip to content

Commit

Permalink
Use new noble async connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Mechazawa committed Nov 19, 2023
1 parent dae51e4 commit 194a34e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
8 changes: 5 additions & 3 deletions examples/doAFlip.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ const backFlip = parser.getCommand('minidrone', 'Animations', 'Flip', {direction


drone.on('connected', () => {
console.log('Connected!')

// Makes the code a bit clearer
const runCommand = x => drone.runCommand(x);

runCommand(takeoff);
// runCommand(takeoff);

setTimeout(runCommand, 2000, backFlip);
setTimeout(runCommand, 4000, landing);
// setTimeout(runCommand, 2000, backFlip);
// setTimeout(runCommand, 2000, landing);
setTimeout(process.exit, 5000);
});
50 changes: 30 additions & 20 deletions src/DroneConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const CommandParser = require('./CommandParser');
const { sendUuids, receiveUuids, serviceUuids, handshakeUuids} = require('./CharacteristicEnums');

const MANUFACTURER_SERIALS = [
'4300cf1900090100',
'4300cf1909090100',
'4300cf1907090100',
0x4300cf1900090100,
0x4300cf1909090100,
0x4300cf1907090100,
];

const DRONE_PREFIXES = [
Expand Down Expand Up @@ -66,12 +66,15 @@ class DroneConnection extends EventEmitter {
* @return {undefined}
* @private
*/
_onNobleStateChange(state) {
async _onNobleStateChange(state) {
Logger.debug(`Noble state changed to ${state}`);

if (state === 'poweredOn') {
Logger.info('Searching for drones...');
this.noble.startScanning();
while (state === 'poweredOn' && !this._peripheral) {
const result = await this.noble.startScanningAsync();

if (result) {
this._onPeripheralDiscovery(result);
}
}
}

Expand All @@ -83,23 +86,30 @@ class DroneConnection extends EventEmitter {
* @return {undefined}
* @private
*/
_onPeripheralDiscovery(peripheral) {
if (!this._validatePeripheral(peripheral)) {
async _onPeripheralDiscovery(peripheral) {
if (this._peripheral || !this._validatePeripheral(peripheral)) {
return;
}

Logger.info(`Peripheral found ${peripheral.advertisement.localName}`);

this.noble.stopScanning();
this._peripheral = peripheral;

peripheral.connect((error) => {
if (error) {
throw error;
}
this._peripheral = peripheral;
if (['disconnecting', 'disconnected', 'error'].includes(peripheral.state)) {
Logger.info(`Connecting to peripheral`);

this._setupPeripheral();
});
await peripheral.connectAsync();
}

if (['connecting', 'connected'].includes(peripheral.state)) {
Logger.info("Connected")
} else {
Logger.info("Something went wrong: " + peripheral.state)

this._peripheral = null;
}

this._setupPeripheral();
}

/**
Expand All @@ -109,12 +119,12 @@ class DroneConnection extends EventEmitter {
* @private
*/
_validatePeripheral(peripheral) {
if (!peripheral) {
if (typeof peripheral !== 'object') {
return false;
}

const localName = peripheral.advertisement.localName;
const manufacturer = peripheral.advertisement.manufacturerData;
const localName = peripheral.advertisement?.localName;
const manufacturer = peripheral.advertisement?.manufacturerData;
const matchesFilter = this.droneFilter ? localName === this.droneFilter : false;

const localNameMatch = matchesFilter || DRONE_PREFIXES.some((prefix) => localName && localName.indexOf(prefix) >= 0);
Expand Down

0 comments on commit 194a34e

Please sign in to comment.