diff --git a/README.md b/README.md index 18a94b79..c3407d85 100644 --- a/README.md +++ b/README.md @@ -134,8 +134,8 @@ bleno.startAdvertisingIBeacon(uuid, major, minor, measuredPower[, callback(error ##### Start advertising with EIR data (__Linux only__) ```javascript -var scanData = new Buffer(...); // maximum 31 bytes -var advertisementData = new Buffer(...); // maximum 31 bytes +var scanData = Buffer.alloc(...); // maximum 31 bytes +var advertisementData = Buffer.alloc(...); // maximum 31 bytes bleno.startAdvertisingWithEIRData(advertisementData[, scanData, callback(error)]); ``` @@ -227,7 +227,7 @@ Parameters to handler are ```javascript var result = Characteristic.RESULT_SUCCESS; -var data = new Buffer( ... ); +var data = Buffer.alloc( ... ); callback(result, data); ``` diff --git a/examples/battery-service/battery-level-characteristic.js b/examples/battery-service/battery-level-characteristic.js index c63a850e..e1150bbf 100644 --- a/examples/battery-service/battery-level-characteristic.js +++ b/examples/battery-service/battery-level-characteristic.js @@ -18,7 +18,7 @@ var BatteryLevelCharacteristic = function() { }), new Descriptor({ uuid: '2904', - value: new Buffer([0x04, 0x01, 0x27, 0xAD, 0x01, 0x00, 0x00 ]) // maybe 12 0xC unsigned 8 bit + value: Buffer.from([0x04, 0x01, 0x27, 0xAD, 0x01, 0x00, 0x00 ]) // maybe 12 0xC unsigned 8 bit }) ] }); @@ -34,11 +34,11 @@ BatteryLevelCharacteristic.prototype.onReadRequest = function(offset, callback) var percent = data.split('\t')[1].split(';')[0]; console.log(percent); percent = parseInt(percent, 10); - callback(this.RESULT_SUCCESS, new Buffer([percent])); + callback(this.RESULT_SUCCESS, Buffer.from([percent])); }); } else { // return hardcoded value - callback(this.RESULT_SUCCESS, new Buffer([98])); + callback(this.RESULT_SUCCESS, Buffer.from([98])); } }; diff --git a/examples/blink1/blink1-fade-rgb-characteristic.js b/examples/blink1/blink1-fade-rgb-characteristic.js index f5865c9c..c193f967 100644 --- a/examples/blink1/blink1-fade-rgb-characteristic.js +++ b/examples/blink1/blink1-fade-rgb-characteristic.js @@ -34,7 +34,7 @@ Blink1FaceRGBCharacteristic.prototype.onWriteRequest = function(data, offset, wi this.blink1.fadeToRGB(fadeMillis, r, g, b, function() { if (this.updateValueCallback) { - this.updateValueCallback(new Buffer([r, g, b])); + this.updateValueCallback(Buffer.from([r, g, b])); } }.bind(this)); diff --git a/examples/blink1/hardware-revision-characteristic.js b/examples/blink1/hardware-revision-characteristic.js index d39d657d..ac2850bf 100644 --- a/examples/blink1/hardware-revision-characteristic.js +++ b/examples/blink1/hardware-revision-characteristic.js @@ -26,7 +26,7 @@ HardwareRevisionCharacteristic.prototype.onReadRequest = function(offset, callba callback(this.RESULT_ATTR_NOT_LONG, null); } else { this.blink1.version(function(version) { - callback(this.RESULT_SUCCESS, new Buffer(version)); + callback(this.RESULT_SUCCESS, Buffer.from(version)); }.bind(this)); } }; diff --git a/examples/blink1/serial-number-characteristic.js b/examples/blink1/serial-number-characteristic.js index 7c06eb53..09c1d320 100644 --- a/examples/blink1/serial-number-characteristic.js +++ b/examples/blink1/serial-number-characteristic.js @@ -9,7 +9,7 @@ function SerialNumberCharacteristic(blink1) { SerialNumberCharacteristic.super_.call(this, { uuid: '2a25', properties: ['read'], - value: new Buffer(blink1.serialNumber), + value: Buffer.from(blink1.serialNumber), descriptors: [ new BlenoDescriptor({ uuid: '2901', diff --git a/examples/echo/characteristic.js b/examples/echo/characteristic.js index 5e8e9657..68174765 100644 --- a/examples/echo/characteristic.js +++ b/examples/echo/characteristic.js @@ -11,7 +11,7 @@ var EchoCharacteristic = function() { value: null }); - this._value = new Buffer(0); + this._value = Buffer.alloc(0); this._updateValueCallback = null; }; diff --git a/examples/pizza/pizza-bake-characteristic.js b/examples/pizza/pizza-bake-characteristic.js index 984058af..922e7ed2 100644 --- a/examples/pizza/pizza-bake-characteristic.js +++ b/examples/pizza/pizza-bake-characteristic.js @@ -31,7 +31,7 @@ PizzaBakeCharacteristic.prototype.onWriteRequest = function(data, offset, withou var self = this; this.pizza.once('ready', function(result) { if (self.updateValueCallback) { - var data = new Buffer(1); + var data = Buffer.alloc(1); data.writeUInt8(result, 0); self.updateValueCallback(data); } diff --git a/examples/pizza/pizza-crust-characteristic.js b/examples/pizza/pizza-crust-characteristic.js index 287db2b8..2801ca4b 100644 --- a/examples/pizza/pizza-crust-characteristic.js +++ b/examples/pizza/pizza-crust-characteristic.js @@ -47,7 +47,7 @@ PizzaCrustCharacteristic.prototype.onReadRequest = function(offset, callback) { callback(this.RESULT_ATTR_NOT_LONG, null); } else { - var data = new Buffer(1); + var data = Buffer.alloc(1); data.writeUInt8(this.pizza.crust, 0); callback(this.RESULT_SUCCESS, data); } diff --git a/examples/pizza/pizza-toppings-characteristic.js b/examples/pizza/pizza-toppings-characteristic.js index cd1f8084..4b536d9c 100644 --- a/examples/pizza/pizza-toppings-characteristic.js +++ b/examples/pizza/pizza-toppings-characteristic.js @@ -37,7 +37,7 @@ PizzaToppingsCharacteristic.prototype.onReadRequest = function(offset, callback) callback(this.RESULT_ATTR_NOT_LONG, null); } else { - var data = new Buffer(2); + var data = Buffer.alloc(2); data.writeUInt16BE(this.pizza.toppings, 0); callback(this.RESULT_SUCCESS, data); } diff --git a/lib/bleno.js b/lib/bleno.js index 0ec8b18e..0919ee02 100644 --- a/lib/bleno.js +++ b/lib/bleno.js @@ -134,9 +134,9 @@ Bleno.prototype.startAdvertisingIBeacon = function(uuid, major, minor, measuredP } } else { var undashedUuid = UuidUtil.removeDashes(uuid); - var uuidData = new Buffer(undashedUuid, 'hex'); + var uuidData = Buffer.from(undashedUuid, 'hex'); var uuidDataLength = uuidData.length; - var iBeaconData = new Buffer(uuidData.length + 5); + var iBeaconData = Buffer.alloc(uuidData.length + 5); for (var i = 0; i < uuidDataLength; i++) { iBeaconData[i] = uuidData[i]; diff --git a/lib/descriptor.js b/lib/descriptor.js index 2e42682e..8dda1c00 100644 --- a/lib/descriptor.js +++ b/lib/descriptor.js @@ -4,7 +4,7 @@ var UuidUtil = require('./uuid-util'); function Descriptor(options) { this.uuid = UuidUtil.removeDashes(options.uuid); - this.value = options.value || new Buffer(0); + this.value = options.value || Buffer.alloc(0); } Descriptor.prototype.toString = function() { diff --git a/lib/hci-socket/crypto.js b/lib/hci-socket/crypto.js index 148f62d7..b9300804 100644 --- a/lib/hci-socket/crypto.js +++ b/lib/hci-socket/crypto.js @@ -15,7 +15,7 @@ function c1(k, r, pres, preq, iat, ia, rat, ra) { var p2 = Buffer.concat([ ra, ia, - new Buffer('00000000', 'hex') + Buffer.from('00000000', 'hex') ]); var res = xor(r, p1); @@ -47,7 +47,7 @@ function e(key, data) { } function xor(b1, b2) { - var result = new Buffer(b1.length); + var result = Buffer.alloc(b1.length); for (var i = 0; i < b1.length; i++) { result[i] = b1[i] ^ b2[i]; @@ -57,7 +57,7 @@ function xor(b1, b2) { } function swap(input) { - var output = new Buffer(input.length); + var output = Buffer.alloc(input.length); for (var i = 0; i < output.length; i++) { output[i] = input[input.length - i - 1]; diff --git a/lib/hci-socket/gap.js b/lib/hci-socket/gap.js index 57382876..7cf14734 100644 --- a/lib/hci-socket/gap.js +++ b/lib/hci-socket/gap.js @@ -41,7 +41,7 @@ Gap.prototype.startAdvertising = function(name, serviceUuids) { if (serviceUuids && serviceUuids.length) { for (i = 0; i < serviceUuids.length; i++) { - var serviceUuid = new Buffer(serviceUuids[i].match(/.{1,2}/g).reverse().join(''), 'hex'); + var serviceUuid = Buffer.from(serviceUuids[i].match(/.{1,2}/g).reverse().join(''), 'hex'); if (serviceUuid.length === 2) { serviceUuids16bit.push(serviceUuid); @@ -59,8 +59,8 @@ Gap.prototype.startAdvertising = function(name, serviceUuids) { advertisementDataLength += 2 + 16 * serviceUuids128bit.length; } - var advertisementData = new Buffer(advertisementDataLength); - var scanData = new Buffer(scanDataLength); + var advertisementData = Buffer.alloc(advertisementDataLength); + var scanData = Buffer.alloc(scanDataLength); // flags advertisementData.writeUInt8(2, 0); @@ -97,7 +97,7 @@ Gap.prototype.startAdvertising = function(name, serviceUuids) { // name if (name && name.length) { - var nameBuffer = new Buffer(name); + var nameBuffer = Buffer.from(name); scanData.writeUInt8(1 + nameBuffer.length, 0); scanData.writeUInt8(0x08, 1); @@ -116,8 +116,8 @@ Gap.prototype.startAdvertisingIBeacon = function(data) { var advertisementDataLength = 5 + manufacturerDataLength; var scanDataLength = 0; - var advertisementData = new Buffer(advertisementDataLength); - var scanData = new Buffer(0); + var advertisementData = Buffer.alloc(advertisementDataLength); + var scanData = Buffer.alloc(0); // flags advertisementData.writeUInt8(2, 0); @@ -136,8 +136,8 @@ Gap.prototype.startAdvertisingIBeacon = function(data) { }; Gap.prototype.startAdvertisingWithEIRData = function(advertisementData, scanData) { - advertisementData = advertisementData || new Buffer(0); - scanData = scanData || new Buffer(0); + advertisementData = advertisementData || Buffer.alloc(0); + scanData = scanData || Buffer.alloc(0); debug('startAdvertisingWithEIRData: advertisement data = ' + advertisementData.toString('hex') + ', scan data = ' + scanData.toString('hex')); diff --git a/lib/hci-socket/gatt.js b/lib/hci-socket/gatt.js index 818e2176..83df9928 100644 --- a/lib/hci-socket/gatt.js +++ b/lib/hci-socket/gatt.js @@ -88,14 +88,14 @@ Gatt.prototype.setServices = function(services) { uuid: '2a00', properties: ['read'], secure: [], - value: new Buffer(deviceName), + value: Buffer.from(deviceName), descriptors: [] }, { uuid: '2a01', properties: ['read'], secure: [], - value: new Buffer([0x80, 0x00]), + value: Buffer.from([0x80, 0x00]), descriptors: [] } ] @@ -107,7 +107,7 @@ Gatt.prototype.setServices = function(services) { uuid: '2a05', properties: ['indicate'], secure: [], - value: new Buffer([0x00, 0x00, 0x00, 0x00]), + value: Buffer.from([0x00, 0x00, 0x00, 0x00]), descriptors: [] } ] @@ -214,7 +214,7 @@ Gatt.prototype.setServices = function(services) { attribute: characteristic, properties: (0x02 | 0x04 | 0x08), // read/write secure: (secure & 0x10) ? (0x02 | 0x04 | 0x08) : 0, - value: new Buffer([0x00, 0x00]) + value: Buffer.from([0x00, 0x00]) }; } @@ -282,7 +282,7 @@ Gatt.prototype.onAclStreamEnd = function() { if (this._handles[i] && this._handles[i].type === 'descriptor' && this._handles[i].uuid === '2902' && this._handles[i].value.readUInt16LE(0) !== 0) { - this._handles[i].value = new Buffer([0x00, 0x00]); + this._handles[i].value = Buffer.from([0x00, 0x00]); if (this._handles[i].attribute && this._handles[i].attribute.emit) { this._handles[i].attribute.emit('unsubscribe'); @@ -297,7 +297,7 @@ Gatt.prototype.send = function(data) { }; Gatt.prototype.errorResponse = function(opcode, handle, status) { - var buf = new Buffer(5); + var buf = Buffer.alloc(5); buf.writeUInt8(ATT_OP_ERROR, 0); buf.writeUInt8(opcode, 1); @@ -383,7 +383,7 @@ Gatt.prototype.handleMtuRequest = function(request) { this.emit('mtuChange', this._mtu); - var response = new Buffer(3); + var response = Buffer.alloc(3); response.writeUInt8(ATT_OP_MTU_RESP, 0); response.writeUInt16LE(mtu, 1); @@ -445,7 +445,7 @@ Gatt.prototype.handleFindInfoRequest = function(request) { var maxInfo = Math.floor((this._mtu - 2) / lengthPerInfo); numInfo = Math.min(numInfo, maxInfo); - response = new Buffer(2 + numInfo * lengthPerInfo); + response = Buffer.alloc(2 + numInfo * lengthPerInfo); response[0] = ATT_OP_FIND_INFO_RESP; response[1] = (uuidSize === 2) ? 0x01 : 0x2; @@ -455,7 +455,7 @@ Gatt.prototype.handleFindInfoRequest = function(request) { response.writeUInt16LE(info.handle, 2 + i * lengthPerInfo); - uuid = new Buffer(info.uuid.match(/.{1,2}/g).reverse().join(''), 'hex'); + uuid = Buffer.from(info.uuid.match(/.{1,2}/g).reverse().join(''), 'hex'); for (var j = 0; j < uuid.length; j++) { response[2 + i * lengthPerInfo + 2 + j] = uuid[j]; } @@ -500,7 +500,7 @@ Gatt.prototype.handleFindByTypeRequest = function(request) { numHandles = Math.min(numHandles, maxHandles); - response = new Buffer(1 + numHandles * lengthPerHandle); + response = Buffer.alloc(1 + numHandles * lengthPerHandle); response[0] = ATT_OP_FIND_BY_TYPE_RESP; @@ -558,7 +558,7 @@ Gatt.prototype.handleReadByGroupRequest = function(request) { var maxServices = Math.floor((this._mtu - 2) / lengthPerService); numServices = Math.min(numServices, maxServices); - response = new Buffer(2 + numServices * lengthPerService); + response = Buffer.alloc(2 + numServices * lengthPerService); response[0] = ATT_OP_READ_BY_GROUP_RESP; response[1] = lengthPerService; @@ -569,7 +569,7 @@ Gatt.prototype.handleReadByGroupRequest = function(request) { response.writeUInt16LE(service.startHandle, 2 + i * lengthPerService); response.writeUInt16LE(service.endHandle, 2 + i * lengthPerService + 2); - var serviceUuid = new Buffer(service.uuid.match(/.{1,2}/g).reverse().join(''), 'hex'); + var serviceUuid = Buffer.from(service.uuid.match(/.{1,2}/g).reverse().join(''), 'hex'); for (var j = 0; j < serviceUuid.length; j++) { response[2 + i * lengthPerService + 4 + j] = serviceUuid[j]; } @@ -626,7 +626,7 @@ Gatt.prototype.handleReadByTypeRequest = function(request) { var maxCharacteristics = Math.floor((this._mtu - 2) / lengthPerCharacteristic); numCharacteristics = Math.min(numCharacteristics, maxCharacteristics); - response = new Buffer(2 + numCharacteristics * lengthPerCharacteristic); + response = Buffer.alloc(2 + numCharacteristics * lengthPerCharacteristic); response[0] = ATT_OP_READ_BY_TYPE_RESP; response[1] = lengthPerCharacteristic; @@ -638,7 +638,7 @@ Gatt.prototype.handleReadByTypeRequest = function(request) { response.writeUInt8(characteristic.properties, 2 + i * lengthPerCharacteristic + 2); response.writeUInt16LE(characteristic.valueHandle, 2 + i * lengthPerCharacteristic + 3); - var characteristicUuid = new Buffer(characteristic.uuid.match(/.{1,2}/g).reverse().join(''), 'hex'); + var characteristicUuid = Buffer.from(characteristic.uuid.match(/.{1,2}/g).reverse().join(''), 'hex'); for (var j = 0; j < characteristicUuid.length; j++) { response[2 + i * lengthPerCharacteristic + 5 + j] = characteristicUuid[j]; } @@ -679,7 +679,7 @@ Gatt.prototype.handleReadByTypeRequest = function(request) { if (ATT_ECODE_SUCCESS === result) { var dataLength = Math.min(data.length, this._mtu - 4); - callbackResponse = new Buffer(4 + dataLength); + callbackResponse = Buffer.alloc(4 + dataLength); callbackResponse[0] = ATT_OP_READ_BY_TYPE_RESP; callbackResponse[1] = dataLength + 2; @@ -735,7 +735,7 @@ Gatt.prototype.handleReadOrReadBlobRequest = function(request) { if (ATT_ECODE_SUCCESS === result) { var dataLength = Math.min(data.length, this._mtu - 1); - callbackResponse = new Buffer(1 + dataLength); + callbackResponse = Buffer.alloc(1 + dataLength); callbackResponse[0] = (requestType === ATT_OP_READ_BLOB_REQ) ? ATT_OP_READ_BLOB_RESP : ATT_OP_READ_RESP; for (i = 0; i < dataLength; i++) { @@ -753,12 +753,12 @@ Gatt.prototype.handleReadOrReadBlobRequest = function(request) { if (handleType === 'service' || handleType === 'includedService') { result = ATT_ECODE_SUCCESS; - data = new Buffer(handle.uuid.match(/.{1,2}/g).reverse().join(''), 'hex'); + data = Buffer.from(handle.uuid.match(/.{1,2}/g).reverse().join(''), 'hex'); } else if (handleType === 'characteristic') { - var uuid = new Buffer(handle.uuid.match(/.{1,2}/g).reverse().join(''), 'hex'); + var uuid = Buffer.from(handle.uuid.match(/.{1,2}/g).reverse().join(''), 'hex'); result = ATT_ECODE_SUCCESS; - data = new Buffer(3 + uuid.length); + data = Buffer.alloc(3 + uuid.length); data.writeUInt8(handle.properties, 0); data.writeUInt16LE(handle.valueHandle, 1); @@ -793,7 +793,7 @@ Gatt.prototype.handleReadOrReadBlobRequest = function(request) { } if (data && typeof data === 'string') { - data = new Buffer(data); + data = Buffer.alloc(data); } if (result === ATT_ECODE_SUCCESS && data && offset) { @@ -842,7 +842,7 @@ Gatt.prototype.handleWriteRequestOrCommand = function(request) { var callbackResponse = null; if (ATT_ECODE_SUCCESS === result) { - callbackResponse = new Buffer([ATT_OP_WRITE_RESP]); + callbackResponse = Buffer.from([ATT_OP_WRITE_RESP]); } else { callbackResponse = this.errorResponse(requestType, valueHandle, result); } @@ -876,7 +876,7 @@ Gatt.prototype.handleWriteRequestOrCommand = function(request) { var i; if (useNotify) { - var notifyMessage = new Buffer(3 + dataLength); + var notifyMessage = Buffer.alloc(3 + dataLength); notifyMessage.writeUInt8(ATT_OP_HANDLE_NOTIFY, 0); notifyMessage.writeUInt16LE(valueHandle, 1); @@ -890,7 +890,7 @@ Gatt.prototype.handleWriteRequestOrCommand = function(request) { attribute.emit('notify'); } else if (useIndicate) { - var indicateMessage = new Buffer(3 + dataLength); + var indicateMessage = Buffer.alloc(3 + dataLength); indicateMessage.writeUInt8(ATT_OP_HANDLE_IND, 0); indicateMessage.writeUInt16LE(valueHandle, 1); @@ -960,7 +960,7 @@ Gatt.prototype.handlePrepareWriteRequest = function(request) { data ]); - response = new Buffer(request.length); + response = Buffer.alloc(request.length); request.copy(response); response[0] = ATT_OP_PREP_WRITE_RESP; } else { @@ -974,7 +974,7 @@ Gatt.prototype.handlePrepareWriteRequest = function(request) { data: data }; - response = new Buffer(request.length); + response = Buffer.alloc(request.length); request.copy(response); response[0] = ATT_OP_PREP_WRITE_RESP; } @@ -1001,14 +1001,14 @@ Gatt.prototype.handleExecuteWriteRequest = function(request) { var valueHandle = this._preparedWriteRequest.valueHandle; if (flag === 0x00) { - response = new Buffer([ATT_OP_EXEC_WRITE_RESP]); + response = Buffer.from([ATT_OP_EXEC_WRITE_RESP]); } else if (flag === 0x01) { var callback = (function(requestType, valueHandle) { return function(result) { var callbackResponse = null; if (ATT_ECODE_SUCCESS === result) { - callbackResponse = new Buffer([ATT_OP_EXEC_WRITE_RESP]); + callbackResponse = Buffer.from([ATT_OP_EXEC_WRITE_RESP]); } else { callbackResponse = this.errorResponse(requestType, valueHandle, result); } diff --git a/lib/hci-socket/hci.js b/lib/hci-socket/hci.js index 6a8a509b..3e644875 100644 --- a/lib/hci-socket/hci.js +++ b/lib/hci-socket/hci.js @@ -149,7 +149,7 @@ Hci.prototype.initDev = function() { } Hci.prototype.setSocketFilter = function() { - var filter = new Buffer(14); + var filter = Buffer.alloc(14); var typeMask = (1 << HCI_EVENT_PKT)| (1 << HCI_ACLDATA_PKT); var eventMask1 = (1 << EVT_DISCONN_COMPLETE) | (1 << EVT_ENCRYPT_CHANGE) | (1 << EVT_CMD_COMPLETE) | (1 << EVT_CMD_STATUS) | ( 1 << EVT_NUMBER_OF_COMPLETED_PACKETS); var eventMask2 = (1 << (EVT_LE_META_EVENT - 32)); @@ -165,8 +165,8 @@ Hci.prototype.setSocketFilter = function() { }; Hci.prototype.setEventMask = function() { - var cmd = new Buffer(12); - var eventMask = new Buffer('fffffbff07f8bf3d', 'hex'); + var cmd = Buffer.alloc(12); + var eventMask = Buffer.from('fffffbff07f8bf3d', 'hex'); // header cmd.writeUInt8(HCI_COMMAND_PKT, 0); @@ -182,7 +182,7 @@ Hci.prototype.setEventMask = function() { }; Hci.prototype.reset = function() { - var cmd = new Buffer(4); + var cmd = Buffer.alloc(4); // header cmd.writeUInt8(HCI_COMMAND_PKT, 0); @@ -196,7 +196,7 @@ Hci.prototype.reset = function() { }; Hci.prototype.readLeHostSupported = function() { - var cmd = new Buffer(4); + var cmd = Buffer.alloc(4); // header cmd.writeUInt8(HCI_COMMAND_PKT, 0); @@ -210,7 +210,7 @@ Hci.prototype.readLeHostSupported = function() { }; Hci.prototype.writeLeHostSupported = function() { - var cmd = new Buffer(6); + var cmd = Buffer.alloc(6); // header cmd.writeUInt8(HCI_COMMAND_PKT, 0); @@ -228,7 +228,7 @@ Hci.prototype.writeLeHostSupported = function() { }; Hci.prototype.readLocalVersion = function() { - var cmd = new Buffer(4); + var cmd = Buffer.alloc(4); // header cmd.writeUInt8(HCI_COMMAND_PKT, 0); @@ -242,7 +242,7 @@ Hci.prototype.readLocalVersion = function() { }; Hci.prototype.readBdAddr = function() { - var cmd = new Buffer(4); + var cmd = Buffer.alloc(4); // header cmd.writeUInt8(HCI_COMMAND_PKT, 0); @@ -256,8 +256,8 @@ Hci.prototype.readBdAddr = function() { }; Hci.prototype.setLeEventMask = function() { - var cmd = new Buffer(12); - var leEventMask = new Buffer('1f00000000000000', 'hex'); + var cmd = Buffer.alloc(12); + var leEventMask = Buffer.from('1f00000000000000', 'hex'); // header cmd.writeUInt8(HCI_COMMAND_PKT, 0); @@ -273,7 +273,7 @@ Hci.prototype.setLeEventMask = function() { }; Hci.prototype.setAdvertisingParameters = function() { - var cmd = new Buffer(19); + var cmd = Buffer.alloc(19); // header cmd.writeUInt8(HCI_COMMAND_PKT, 0); @@ -290,7 +290,7 @@ Hci.prototype.setAdvertisingParameters = function() { cmd.writeUInt8(0x00, 8); // adv type cmd.writeUInt8(0x00, 9); // own addr typ cmd.writeUInt8(0x00, 10); // direct addr type - (new Buffer('000000000000', 'hex')).copy(cmd, 11); // direct addr + (Buffer.from('000000000000', 'hex')).copy(cmd, 11); // direct addr cmd.writeUInt8(0x07, 17); cmd.writeUInt8(0x00, 18); @@ -299,7 +299,7 @@ Hci.prototype.setAdvertisingParameters = function() { }; Hci.prototype.setAdvertisingData = function(data) { - var cmd = new Buffer(36); + var cmd = Buffer.alloc(36); cmd.fill(0x00); @@ -319,7 +319,7 @@ Hci.prototype.setAdvertisingData = function(data) { }; Hci.prototype.setScanResponseData = function(data) { - var cmd = new Buffer(36); + var cmd = Buffer.alloc(36); cmd.fill(0x00); @@ -339,7 +339,7 @@ Hci.prototype.setScanResponseData = function(data) { }; Hci.prototype.setAdvertiseEnable = function(enabled) { - var cmd = new Buffer(5); + var cmd = Buffer.alloc(5); // header cmd.writeUInt8(HCI_COMMAND_PKT, 0); @@ -356,7 +356,7 @@ Hci.prototype.setAdvertiseEnable = function(enabled) { }; Hci.prototype.disconnect = function(handle, reason) { - var cmd = new Buffer(7); + var cmd = Buffer.alloc(7); reason = reason || HCI_OE_USER_ENDED_CONNECTION; @@ -376,7 +376,7 @@ Hci.prototype.disconnect = function(handle, reason) { }; Hci.prototype.readRssi = function(handle) { - var cmd = new Buffer(6); + var cmd = Buffer.alloc(6); // header cmd.writeUInt8(HCI_COMMAND_PKT, 0); @@ -393,7 +393,7 @@ Hci.prototype.readRssi = function(handle) { }; Hci.prototype.leReadBufferSize = function() { - var cmd = new Buffer(4); + var cmd = Buffer.alloc(4); // header cmd.writeUInt8(HCI_COMMAND_PKT, 0); @@ -407,7 +407,7 @@ Hci.prototype.leReadBufferSize = function() { }; Hci.prototype.readBufferSize = function() { - var cmd = new Buffer(4); + var cmd = Buffer.alloc(4); // header cmd.writeUInt8(HCI_COMMAND_PKT, 0); @@ -423,7 +423,7 @@ Hci.prototype.readBufferSize = function() { Hci.prototype.queueAclDataPkt = function(handle, cid, data) { var hf = handle | ACL_START_NO_FLUSH << 12; // l2cap pdu may be fragmented on hci level - var l2capPdu = new Buffer(4 + data.length); + var l2capPdu = Buffer.alloc(4 + data.length); l2capPdu.writeUInt16LE(data.length, 0); l2capPdu.writeUInt16LE(cid, 2); data.copy(l2capPdu, 4); @@ -432,7 +432,7 @@ Hci.prototype.queueAclDataPkt = function(handle, cid, data) { while (l2capPdu.length) { var frag = l2capPdu.slice(0, this._aclMtu); l2capPdu = l2capPdu.slice(frag.length); - var pkt = new Buffer(5 + frag.length); + var pkt = Buffer.alloc(5 + frag.length); // hci header pkt.writeUInt8(HCI_ACLDATA_PKT, 0); diff --git a/lib/hci-socket/mgmt.js b/lib/hci-socket/mgmt.js index 1b7d5361..32b2aff8 100644 --- a/lib/hci-socket/mgmt.js +++ b/lib/hci-socket/mgmt.js @@ -29,7 +29,7 @@ Mgmt.prototype.onSocketError = function(error) { }; Mgmt.prototype.addLongTermKey = function(address, addressType, authenticated, master, ediv, rand, key) { - var ltkInfo = new Buffer(LTK_INFO_SIZE); + var ltkInfo = Buffer.alloc(LTK_INFO_SIZE); address.copy(ltkInfo, 0); ltkInfo.writeUInt8(addressType.readUInt8(0) + 1, 6); // BDADDR_LE_PUBLIC = 0x01, BDADDR_LE_RANDOM 0x02, so add one @@ -55,7 +55,7 @@ Mgmt.prototype.clearLongTermKeys = function() { Mgmt.prototype.loadLongTermKeys = function() { var numLongTermKeys = this._ltkInfos.length; - var op = new Buffer(2 + numLongTermKeys * LTK_INFO_SIZE); + var op = Buffer.alloc(2 + numLongTermKeys * LTK_INFO_SIZE); op.writeUInt16LE(numLongTermKeys, 0); @@ -73,7 +73,7 @@ Mgmt.prototype.write = function(opcode, index, data) { length = data.length; } - var pkt = new Buffer(6 + length); + var pkt = Buffer.alloc(6 + length); pkt.writeUInt16LE(opcode, 0); pkt.writeUInt16LE(index, 2); diff --git a/lib/hci-socket/smp.js b/lib/hci-socket/smp.js index 87b32c32..49de2cc1 100644 --- a/lib/hci-socket/smp.js +++ b/lib/hci-socket/smp.js @@ -21,10 +21,10 @@ var SMP_UNSPECIFIED = 0x08; var Smp = function(aclStream, localAddressType, localAddress, remoteAddressType, remoteAddress) { this._aclStream = aclStream; - this._iat = new Buffer([(remoteAddressType === 'random') ? 0x01 : 0x00]); - this._ia = new Buffer(remoteAddress.split(':').reverse().join(''), 'hex'); - this._rat = new Buffer([(localAddressType === 'random') ? 0x01 : 0x00]); - this._ra = new Buffer(localAddress.split(':').reverse().join(''), 'hex'); + this._iat = Buffer.from([(remoteAddressType === 'random') ? 0x01 : 0x00]); + this._ia = Buffer.from(remoteAddress.split(':').reverse().join(''), 'hex'); + this._rat = Buffer.from([(localAddressType === 'random') ? 0x01 : 0x00]); + this._ra = Buffer.from(localAddress.split(':').reverse().join(''), 'hex'); this._stk = null; this._random = null; @@ -65,12 +65,12 @@ Smp.prototype.onAclStreamEncryptChange = function(encrypted) { if (encrypted) { if (this._stk && this._diversifier && this._random) { this.write(Buffer.concat([ - new Buffer([SMP_ENCRYPT_INFO]), + Buffer.from([SMP_ENCRYPT_INFO]), this._stk ])); this.write(Buffer.concat([ - new Buffer([SMP_MASTER_IDENT]), + Buffer.from([SMP_MASTER_IDENT]), this._diversifier, this._random ])); @@ -79,7 +79,7 @@ Smp.prototype.onAclStreamEncryptChange = function(encrypted) { }; Smp.prototype.onAclStreamLtkNegReply = function() { - this.write(new Buffer([ + this.write(Buffer.from([ SMP_PAIRING_FAILED, SMP_UNSPECIFIED ])); @@ -97,7 +97,7 @@ Smp.prototype.onAclStreamEnd = function() { Smp.prototype.handlePairingRequest = function(data) { this._preq = data; - this._pres = new Buffer([ + this._pres = Buffer.from([ SMP_PAIRING_RESPONSE, 0x03, // IO capability: NoInputNoOutput 0x00, // OOB data: Authentication data not present @@ -113,11 +113,11 @@ Smp.prototype.handlePairingRequest = function(data) { Smp.prototype.handlePairingConfirm = function(data) { this._pcnf = data; - this._tk = new Buffer('00000000000000000000000000000000', 'hex'); + this._tk = Buffer.from('00000000000000000000000000000000', 'hex'); this._r = crypto.r(); this.write(Buffer.concat([ - new Buffer([SMP_PAIRING_CONFIRM]), + Buffer.from([SMP_PAIRING_CONFIRM]), crypto.c1(this._tk, this._r, this._pres, this._preq, this._iat, this._ia, this._rat, this._ra) ])); }; @@ -126,23 +126,23 @@ Smp.prototype.handlePairingRandom = function(data) { var r = data.slice(1); var pcnf = Buffer.concat([ - new Buffer([SMP_PAIRING_CONFIRM]), + Buffer.from([SMP_PAIRING_CONFIRM]), crypto.c1(this._tk, r, this._pres, this._preq, this._iat, this._ia, this._rat, this._ra) ]); if (this._pcnf.toString('hex') === pcnf.toString('hex')) { - this._diversifier = new Buffer('0000', 'hex'); - this._random = new Buffer('0000000000000000', 'hex'); + this._diversifier = Buffer.from('0000', 'hex'); + this._random = Buffer.from('0000000000000000', 'hex'); this._stk = crypto.s1(this._tk, this._r, r); mgmt.addLongTermKey(this._ia, this._iat, 0, 0, this._diversifier, this._random, this._stk); this.write(Buffer.concat([ - new Buffer([SMP_PAIRING_RANDOM]), + Buffer.from([SMP_PAIRING_RANDOM]), this._r ])); } else { - this.write(new Buffer([ + this.write(Buffer.from([ SMP_PAIRING_FAILED, SMP_PAIRING_CONFIRM ])); diff --git a/lib/mac/highsierra.js b/lib/mac/highsierra.js index f4f5c8d1..edfeebc2 100644 --- a/lib/mac/highsierra.js +++ b/lib/mac/highsierra.js @@ -99,7 +99,7 @@ blenoBindings.startAdvertising = function(name, serviceUuids) { if (serviceUuids && serviceUuids.length) { for(var i = 0; i < serviceUuids.length; i++) { - advertisement.kCBAdvDataServiceUUIDs[i] = new Buffer(serviceUuids[i], 'hex'); + advertisement.kCBAdvDataServiceUUIDs[i] = Buffer.from(serviceUuids[i], 'hex'); } } @@ -110,7 +110,7 @@ blenoBindings.startAdvertisingIBeacon = function(data) { var args = {}; args.kCBAdvDataAppleMfgData = Buffer.concat([ - new Buffer([data.length + 5, 0xff, 0x4c, 0x00, 0x02, data.length]), + Buffer.from([data.length + 5, 0xff, 0x4c, 0x00, 0x02, data.length]), data ]); @@ -160,7 +160,7 @@ blenoBindings.setServices = function(services) { kCBMsgArgAttributeIDs: [], kCBMsgArgCharacteristics: [], kCBMsgArgType: 1, // 1 => primary, 0 => included - kCBMsgArgUUID: new Buffer(service.uuid, 'hex') + kCBMsgArgUUID: Buffer.from(service.uuid, 'hex') }; this._attributes[attributeId] = service; @@ -226,7 +226,7 @@ blenoBindings.setServices = function(services) { kCBMsgArgCharacteristicProperties: properties, kCBMsgArgData: characteristic.value, kCBMsgArgDescriptors: [], - kCBMsgArgUUID: new Buffer(characteristic.uuid, 'hex') + kCBMsgArgUUID: Buffer.from(characteristic.uuid, 'hex') }; this._attributes[attributeId] = characteristic; @@ -236,7 +236,7 @@ blenoBindings.setServices = function(services) { characteristicArg.kCBMsgArgDescriptors.push({ kCBMsgArgData: descriptor.value, - kCBMsgArgUUID: new Buffer(descriptor.uuid, 'hex') + kCBMsgArgUUID: Buffer.from(descriptor.uuid, 'hex') }); } @@ -285,7 +285,7 @@ blenoBindings.on('kCBMsgId57', function(args) { var deviceUUID = args.kCBMsgArgDeviceUUID.toString('hex'); var mtu = args.kCBMsgArgATTMTU; - this._deviceUUID = new Buffer(deviceUUID, 'hex'); + this._deviceUUID = Buffer.from(deviceUUID, 'hex'); this._deviceUUID.isUuid = true; uuidToAddress(deviceUUID, function(error, address) { diff --git a/lib/mac/yosemite.js b/lib/mac/yosemite.js index 81ea6cfa..a3c6c1aa 100644 --- a/lib/mac/yosemite.js +++ b/lib/mac/yosemite.js @@ -106,7 +106,7 @@ blenoBindings.startAdvertising = function(name, serviceUuids) { if (serviceUuids && serviceUuids.length) { for(var i = 0; i < serviceUuids.length; i++) { - advertisement.kCBAdvDataServiceUUIDs[i] = new Buffer(serviceUuids[i], 'hex'); + advertisement.kCBAdvDataServiceUUIDs[i] = Buffer.from(serviceUuids[i], 'hex'); } } @@ -118,7 +118,7 @@ blenoBindings.startAdvertisingIBeacon = function(data) { if (osRelease >= 14) { args.kCBAdvDataAppleMfgData = Buffer.concat([ - new Buffer([data.length + 5, 0xff, 0x4c, 0x00, 0x02, data.length]), + Buffer.from([data.length + 5, 0xff, 0x4c, 0x00, 0x02, data.length]), data ]); } else { @@ -175,7 +175,7 @@ blenoBindings.setServices = function(services) { kCBMsgArgAttributeIDs: [], kCBMsgArgCharacteristics: [], kCBMsgArgType: 1, // 1 => primary, 0 => included - kCBMsgArgUUID: new Buffer(service.uuid, 'hex') + kCBMsgArgUUID: Buffer.from(service.uuid, 'hex') }; this._attributes[attributeId] = service; @@ -241,7 +241,7 @@ blenoBindings.setServices = function(services) { kCBMsgArgCharacteristicProperties: properties, kCBMsgArgData: characteristic.value, kCBMsgArgDescriptors: [], - kCBMsgArgUUID: new Buffer(characteristic.uuid, 'hex') + kCBMsgArgUUID: Buffer.from(characteristic.uuid, 'hex') }; this._attributes[attributeId] = characteristic; @@ -251,7 +251,7 @@ blenoBindings.setServices = function(services) { characteristicArg.kCBMsgArgDescriptors.push({ kCBMsgArgData: descriptor.value, - kCBMsgArgUUID: new Buffer(descriptor.uuid, 'hex') + kCBMsgArgUUID: Buffer.from(descriptor.uuid, 'hex') }); } @@ -300,7 +300,7 @@ blenoBindings.on('kCBMsgId53', function(args) { var deviceUUID = args.kCBMsgArgDeviceUUID.toString('hex'); var mtu = args.kCBMsgArgATTMTU; - this._deviceUUID = new Buffer(deviceUUID, 'hex'); + this._deviceUUID = Buffer.from(deviceUUID, 'hex'); this._deviceUUID.isUuid = true; uuidToAddress(deviceUUID, function(error, address) { diff --git a/test.js b/test.js index d1c8ea1e..1233ffb4 100644 --- a/test.js +++ b/test.js @@ -13,7 +13,7 @@ var StaticReadOnlyCharacteristic = function() { StaticReadOnlyCharacteristic.super_.call(this, { uuid: 'fffffffffffffffffffffffffffffff1', properties: ['read'], - value: new Buffer('value'), + value: Buffer.from('value'), descriptors: [ new BlenoDescriptor({ uuid: '2901', @@ -35,7 +35,7 @@ util.inherits(DynamicReadOnlyCharacteristic, BlenoCharacteristic); DynamicReadOnlyCharacteristic.prototype.onReadRequest = function(offset, callback) { var result = this.RESULT_SUCCESS; - var data = new Buffer('dynamic value'); + var data = Buffer.from('dynamic value'); if (offset > data.length) { result = this.RESULT_INVALID_OFFSET; @@ -58,7 +58,7 @@ util.inherits(LongDynamicReadOnlyCharacteristic, BlenoCharacteristic); LongDynamicReadOnlyCharacteristic.prototype.onReadRequest = function(offset, callback) { var result = this.RESULT_SUCCESS; - var data = new Buffer(512); + var data = Buffer.alloc(512); for (var i = 0; i < data.length; i++) { data[i] = i % 256; @@ -103,7 +103,7 @@ NotifyOnlyCharacteristic.prototype.onSubscribe = function(maxValueSize, updateVa this.counter = 0; this.changeInterval = setInterval(function() { - var data = new Buffer(4); + var data = Buffer.alloc(4); data.writeUInt32LE(this.counter, 0); console.log('NotifyOnlyCharacteristic update value: ' + this.counter); @@ -139,7 +139,7 @@ IndicateOnlyCharacteristic.prototype.onSubscribe = function(maxValueSize, update this.counter = 0; this.changeInterval = setInterval(function() { - var data = new Buffer(4); + var data = Buffer.alloc(4); data.writeUInt32LE(this.counter, 0); console.log('IndicateOnlyCharacteristic update value: ' + this.counter); diff --git a/test/test-characteristic.js b/test/test-characteristic.js index 29c04c02..e49040bb 100644 --- a/test/test-characteristic.js +++ b/test/test-characteristic.js @@ -8,7 +8,7 @@ describe('Characteristic', function() { var mockUuid = 'mockuuid'; var mockProperties = ['property1', 'property2', 'property3']; var mockSecure = ['secure1', 'secure2', 'secure3']; - var mockValue = new Buffer('mock value'); + var mockValue = Buffer.from('mock value'); var mockDescriptors = [{}, {}, {}]; var mockOnReadRequest = function() {}; @@ -152,7 +152,7 @@ describe('Characteristic', function() { it('should handle write request', function(done) { var characteristic = new Characteristic({}); - characteristic.emit('writeRequest', new Buffer(0), 0, false, function(result) { + characteristic.emit('writeRequest', Buffer.alloc(0), 0, false, function(result) { result.should.equal(0x0e); done(); diff --git a/test/test-descriptor.js b/test/test-descriptor.js index 5370ee76..c22c95a1 100644 --- a/test/test-descriptor.js +++ b/test/test-descriptor.js @@ -6,7 +6,7 @@ var Descriptor = require('../lib/descriptor'); describe('Descriptor', function() { var mockUuid = 'mockuuid'; - var mockValue = new Buffer('mock value'); + var mockValue = Buffer.from('mock value'); it('should create with uuid option', function() { var descriptor = new Descriptor({