diff --git a/lib/bleno.js b/lib/bleno.js index dc3f7fe1..fd1b0a03 100644 --- a/lib/bleno.js +++ b/lib/bleno.js @@ -81,42 +81,62 @@ Bleno.prototype.onDisconnect = function(clientAddress) { }; Bleno.prototype.startAdvertising = function(name, serviceUuids, callback) { - if (callback) { - this.once('advertisingStart', callback); - } + if (this.state !== 'poweredOn') { + var error = new Error('Could not start advertising, state is ' + this.state + ' (not poweredOn)'); - var undashedServiceUuids = []; + if (typeof callback === 'function') { + callback(error); + } else { + throw error; + } + } else { + if (callback) { + this.once('advertisingStart', callback); + } - if (serviceUuids && serviceUuids.length) { - for (var i = 0; i < serviceUuids.length; i++) { - undashedServiceUuids[i] = UuidUtil.removeDashes(serviceUuids[i]); + var undashedServiceUuids = []; + + if (serviceUuids && serviceUuids.length) { + for (var i = 0; i < serviceUuids.length; i++) { + undashedServiceUuids[i] = UuidUtil.removeDashes(serviceUuids[i]); + } } - } - this._bindings.startAdvertising(name, undashedServiceUuids); + this._bindings.startAdvertising(name, undashedServiceUuids); + } }; Bleno.prototype.startAdvertisingIBeacon = function(uuid, major, minor, measuredPower, callback) { - var undashedUuid = UuidUtil.removeDashes(uuid); - var uuidData = new Buffer(undashedUuid, 'hex'); - var uuidDataLength = uuidData.length; - var iBeaconData = new Buffer(uuidData.length + 5); + if (this.state !== 'poweredOn') { + var error = new Error('Could not start advertising, state is ' + this.state + ' (not poweredOn)'); - for (var i = 0; i < uuidDataLength; i++) { - iBeaconData[i] = uuidData[i]; - } + if (typeof callback === 'function') { + callback(error); + } else { + throw error; + } + } else { + var undashedUuid = UuidUtil.removeDashes(uuid); + var uuidData = new Buffer(undashedUuid, 'hex'); + var uuidDataLength = uuidData.length; + var iBeaconData = new Buffer(uuidData.length + 5); + + for (var i = 0; i < uuidDataLength; i++) { + iBeaconData[i] = uuidData[i]; + } - iBeaconData.writeUInt16BE(major, uuidDataLength); - iBeaconData.writeUInt16BE(minor, uuidDataLength + 2); - iBeaconData.writeInt8(measuredPower, uuidDataLength + 4); + iBeaconData.writeUInt16BE(major, uuidDataLength); + iBeaconData.writeUInt16BE(minor, uuidDataLength + 2); + iBeaconData.writeInt8(measuredPower, uuidDataLength + 4); - if (callback) { - this.once('advertisingStart', callback); - } + if (callback) { + this.once('advertisingStart', callback); + } - debug('iBeacon data = ' + iBeaconData.toString('hex')); + debug('iBeacon data = ' + iBeaconData.toString('hex')); - this._bindings.startAdvertisingIBeacon(iBeaconData); + this._bindings.startAdvertisingIBeacon(iBeaconData); + } }; Bleno.prototype.onAdvertisingStart = function(error) { @@ -132,18 +152,38 @@ Bleno.prototype.onAdvertisingStart = function(error) { if (platform === 'linux') { // Linux only API Bleno.prototype.startAdvertisingWithEIRData = function(advertisementData, scanData, callback) { - if (callback) { - this.once('advertisingStart', callback); + if (this.state !== 'poweredOn') { + var error = new Error('Could not advertising scanning, state is ' + this.state + ' (not poweredOn)'); + + if (typeof callback === 'function') { + callback(error); + } else { + throw error; + } + } else { + if (callback) { + this.once('advertisingStart', callback); + } + this._bindings.startAdvertisingWithEIRData(advertisementData, scanData); } - this._bindings.startAdvertisingWithEIRData(advertisementData, scanData); }; } else if (platform === 'darwin' && parseFloat(os.release()) >= 14) { // OS X >= 10.10 API Bleno.prototype.startAdvertisingWithEIRData = function(advertisementData, callback) { - if (callback) { - this.once('advertisingStart', callback); + if (this.state !== 'poweredOn') { + var error = new Error('Could not start advertising, state is ' + this.state + ' (not poweredOn)'); + + if (typeof callback === 'function') { + callback(error); + } else { + throw error; + } + } else { + if (callback) { + this.once('advertisingStart', callback); + } + this._bindings.startAdvertisingWithEIRData(advertisementData); } - this._bindings.startAdvertisingWithEIRData(advertisementData); }; }