Skip to content

Commit

Permalink
- report error or throw error if advertising API's are called and sta…
Browse files Browse the repository at this point in the history
…te is not poweredOn (#126)
  • Loading branch information
sandeepmistry committed Sep 5, 2015
1 parent a15f93e commit 42d5753
Showing 1 changed file with 70 additions and 30 deletions.
100 changes: 70 additions & 30 deletions lib/bleno.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
};
}

Expand Down

0 comments on commit 42d5753

Please sign in to comment.