UNPKG

@s89/ble-ancs

Version:

An Apple ANCS reciever from Linux. It is a combination of the Bleno, Noble and ANCS projects from Sandeep Mistry

181 lines (143 loc) 4.45 kB
/*jshint loopfunc: true */ var debug = require('debug')('peripheral'); var events = require('events'); var util = require('util'); function Peripheral(able, id, address, addressType, connectable, advertisement, rssi) { this._able = able; this.id = id; this.uuid = id; // for legacy this.address = address; this.addressType = addressType; this.connectable = connectable; this.advertisement = advertisement; this.rssi = rssi; this.services = null; this.state = 'disconnected'; } util.inherits(Peripheral, events.EventEmitter); Peripheral.prototype.toString = function() { return JSON.stringify({ id: this.id, address: this.address, addressType: this.addressType, connectable: this.connectable, advertisement: this.advertisement, rssi: this.rssi, state: this.state }); }; Peripheral.prototype.connect = function(callback) { if (callback) { this.once('connect', function(error) { callback(error); }); } if (this.state === 'connected') { this.emit('connect', new Error('Peripheral already connected')); } else { this.state = 'connecting'; this._able.connect(this.id); } }; Peripheral.prototype.disconnect = function(callback) { if (callback) { this.once('disconnect', function() { callback(null); }); } this.state = 'disconnecting'; this._able.disconnect(this.id); }; Peripheral.prototype.updateRssi = function(callback) { if (callback) { this.once('rssiUpdate', function(rssi) { callback(null, rssi); }); } this._able.updateRssi(this.id); }; Peripheral.prototype.discoverServices = function(uuids, callback) { if (callback) { this.once('servicesDiscover', function(services) { callback(null, services); }); } this._able.discoverServices(this.id, uuids); }; Peripheral.prototype.findService = function(uuid, callback) { if (callback) { this.once('servicesDiscover', function(services) { callback(null, services); }); } this._able.findService(this.id, uuid); }; Peripheral.prototype.findServiceAndCharacteristics = function(serviceUuid, characteristicsUuids, callback) { this.findService(serviceUuid, function(err, services) { var numDiscovered = 0; var allCharacteristics = []; for (var i in services) { var service = services[i]; service.discoverCharacteristics(characteristicsUuids, function(error, characteristics) { numDiscovered++; if (error === null) { for (var j in characteristics) { var characteristic = characteristics[j]; allCharacteristics.push(characteristic); } } if (numDiscovered === services.length) { if (callback) { callback(null, services, allCharacteristics); } } }.bind(this)); } }.bind(this)); }; Peripheral.prototype.discoverSomeServicesAndCharacteristics = function(serviceUuids, characteristicsUuids, callback) { this.discoverServices(serviceUuids, function(err, services) { var numDiscovered = 0; var allCharacteristics = []; for (var i in services) { var service = services[i]; service.discoverCharacteristics(characteristicsUuids, function(error, characteristics) { numDiscovered++; if (error === null) { for (var j in characteristics) { var characteristic = characteristics[j]; allCharacteristics.push(characteristic); } } if (numDiscovered === services.length) { if (callback) { callback(null, services, allCharacteristics); } } }.bind(this)); } }.bind(this)); }; Peripheral.prototype.discoverAllServicesAndCharacteristics = function(callback) { this.discoverSomeServicesAndCharacteristics([], [], callback); }; Peripheral.prototype.readHandle = function(handle, callback) { if (callback) { this.once('handleRead' + handle, function(data) { callback(null, data); }); } this._able.readHandle(this.id, handle); }; Peripheral.prototype.writeHandle = function(handle, data, withoutResponse, callback) { if (!(data instanceof Buffer)) { throw new Error('data must be a Buffer'); } if (callback) { this.once('handleWrite' + handle, function() { callback(null); }); } this._able.writeHandle(this.id, handle, data, withoutResponse); }; module.exports = Peripheral;