bluetooth-helper
Advanced tools
Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "bluetooth-helper", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"homepage": "https://github.com/evanxd/bluetooth-helper", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -1,2 +0,2 @@ | ||
/* global EventEmitter2 */ | ||
/* global BluetoothDevice */ | ||
'use strict'; | ||
@@ -10,35 +10,26 @@ | ||
function BluetoothHelper(name, address) { | ||
this.name = name; | ||
this.address = address; | ||
function BluetoothHelper() { | ||
this._devices = []; | ||
this.startScan(); | ||
} | ||
BluetoothHelper.prototype = Object.create(EventEmitter2.prototype); | ||
BluetoothHelper.prototype.address = null, | ||
BluetoothHelper.prototype.name = null, | ||
BluetoothHelper.prototype.isConnected = false, | ||
BluetoothHelper.prototype._bluetooth = null, | ||
BluetoothHelper.prototype._devices = null, | ||
BluetoothHelper.prototype._gatt = null, | ||
BluetoothHelper.prototype._writeChar = null, | ||
BluetoothHelper.prototype._notifyChar = null, | ||
BluetoothHelper.prototype._isScanning = false, | ||
BluetoothHelper.prototype.send = function(data) { | ||
data = this._parseHexString(data); | ||
this._writeChar.writeValue(data); | ||
}; | ||
BluetoothHelper.prototype.addDevice = function(name, address) { | ||
var device = new BluetoothDevice(name, address); | ||
// TODO: Pop the device from array once it is disconnected. | ||
this._devices.push(device); | ||
return device; | ||
}, | ||
BluetoothHelper.prototype._parseHexString = function(str) { | ||
var arrayBuffer = new ArrayBuffer(Math.ceil(str.length / 2)); | ||
var uint8Array = new Uint8Array(arrayBuffer); | ||
for (var i = 0, j = 0; i < str.length; i += 2, j++) { | ||
uint8Array[j] = parseInt(str.substr(i, 2), 16); | ||
BluetoothHelper.prototype.startScan = function() { | ||
if (this._isScanning) { | ||
return; | ||
} | ||
return arrayBuffer; | ||
}; | ||
BluetoothHelper.prototype.connect = function() { | ||
if (bluetooth.defaultAdapter) { | ||
// For stability, We use startLeScan to instead of startDiscovery. | ||
// For stability, We use startScan to instead of startDiscovery. | ||
return bluetooth.defaultAdapter.startLeScan([]).then(scan => { | ||
this._isScanning = true; | ||
this._scan = scan; | ||
@@ -48,3 +39,3 @@ scan.addEventListener('devicefound', | ||
}).catch(() => { | ||
this.connect(); | ||
this.startScan(); | ||
}); | ||
@@ -57,3 +48,6 @@ } else { | ||
BluetoothHelper.prototype.disconnect = function() { | ||
BluetoothHelper.prototype.stopScan = function() { | ||
if (!this._isScanning) { | ||
return; | ||
} | ||
if (this._gatt) { | ||
@@ -63,4 +57,4 @@ return this._gatt.disconnect().then(() => { | ||
}).then(() => { | ||
this.emit('disconnected'); | ||
this.isConnected = false; | ||
this._isScanning = false; | ||
this.emit('scanningstoped'); | ||
}); | ||
@@ -78,3 +72,3 @@ } else { | ||
this._handleAttributechanged); | ||
this.connect(); | ||
this.startScan(); | ||
break; | ||
@@ -89,8 +83,10 @@ } | ||
this._gatt = gatt; | ||
if (devcie.name === this.name || | ||
devcie.address === this.address) { | ||
this.name = devcie.name; | ||
this.address = devcie.address; | ||
devcie = this._devices.find(function(item) { | ||
return (item.address === devcie.address || | ||
item.name === devcie.name) && | ||
!devcie.paired; | ||
}); | ||
if (devcie) { | ||
gatt.connect().then(() => { | ||
return this._discoverServices(); | ||
this._discoverServices(devcie); | ||
}); | ||
@@ -100,3 +96,3 @@ } | ||
BluetoothHelper.prototype._discoverServices = function() { | ||
BluetoothHelper.prototype._discoverServices = function(devcie) { | ||
var gatt = this._gatt; | ||
@@ -107,16 +103,19 @@ return gatt.discoverServices().then(() => { | ||
}); | ||
this._writeChar = | ||
var writeCharacteristic = | ||
service.characteristics.find(function(characteristic) { | ||
return characteristic.uuid === BLE_RX_UUID; | ||
}); | ||
this._notifyChar = | ||
var notifyCharacteristic = | ||
service.characteristics.find(function(characteristic) { | ||
return characteristic.uuid === BLE_TX_UUID; | ||
}); | ||
this.emit('connected'); | ||
this.isConnected = true; | ||
devcie.writeCharacteristic = writeCharacteristic; | ||
devcie.notifyCharacteristic = notifyCharacteristic; | ||
devcie.emit('connected', devcie); | ||
}); | ||
}; | ||
exports.Bluetooth = new BluetoothHelper(); | ||
// For testing. | ||
exports.BluetoothHelper = BluetoothHelper; | ||
}(window)); |
{ | ||
"name": "bluetooth-helper", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"homepage": "https://github.com/evanxd/bluetooth-helper", | ||
@@ -5,0 +5,0 @@ "authors": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6447
7
154