Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bluetooth-helper

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bluetooth-helper - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

.npmignore

7

bower.json
{
"name": "bluetooth-helper",
"version": "0.0.1",
"version": "0.0.2",
"homepage": "https://github.com/evanxd/bluetooth-helper",

@@ -15,3 +15,6 @@ "authors": [

],
"license": "Apache License 2.0"
"license": "Apache License 2.0",
"dependencies": {
"eventemitter2": "0.4.14"
}
}

@@ -0,112 +1,115 @@

/* global EventEmitter2 */
'use strict';
(function(exports) {
var BLESHIELD_SERVICE_UUID = '713d0000-503e-4c75-ba94-3148f18d941e';
var BLESHIELD_RX_UUID = '713d0003-503e-4c75-ba94-3148f18d941e';
var BLESHIELD_TX_UUID = '713d0002-503e-4c75-ba94-3148f18d941e';
var BLE_SERVICE_UUID = '713d0000-503e-4c75-ba94-3148f18d941e';
var BLE_RX_UUID = '713d0003-503e-4c75-ba94-3148f18d941e';
var BLE_TX_UUID = '713d0002-503e-4c75-ba94-3148f18d941e';
var bluetooth = navigator.mozBluetooth;
function BluetoothHelper(address) {
this.address = address;
this._bluetooth = window.navigator.mozBluetooth;
this._bluetooth.addEventListener('attributechanged',
this._handleBluetoothAttributechanged.bind(this));
function BluetoothHelper(options) {
if (options && (options.name || options.address)) {
this.name = options.name;
this.address = options.address;
bluetooth.addEventListener('attributechanged',
this._handleAttributechanged.bind(this));
}
}
BluetoothHelper.prototype = {
address: null,
name: null,
isConnected: false,
_bluetooth: null,
_gatt: null,
_writeChar: null,
_notifyChar: null,
BluetoothHelper.prototype = Object.create(EventEmitter2.prototype);
sendData: function(data) {
data = this._parseHexString(data);
this._writeChar.writeValue(data);
},
BluetoothHelper.prototype.address = null,
BluetoothHelper.prototype.name = null,
BluetoothHelper.prototype.isConnected = false,
BluetoothHelper.prototype._bluetooth = null,
BluetoothHelper.prototype._gatt = null,
BluetoothHelper.prototype._writeChar = null,
BluetoothHelper.prototype._notifyChar = null,
_parseHexString: function(str) {
var arrayBuffer = new ArrayBuffer(Math.ceil(str.length / 2));
var uint8Array = new Uint8Array(arrayBuffer);
BluetoothHelper.prototype.send = function(data) {
data = this._parseHexString(data);
this._writeChar.writeValue(data);
};
for (var i = 0, j = 0; i < str.length; i += 2, j++) {
uint8Array[j] = parseInt(str.substr(i, 2), 16);
}
return arrayBuffer;
},
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);
}
return arrayBuffer;
};
_connectBleServer: function() {
console.log('Connecting...');
return this._bluetooth.defaultAdapter.startDiscovery().catch(() => {
// Retry to connect the BLE server if failed.
this._connectBleServer();
}).then(discovery => {
discovery.addEventListener('devicefound',
this._handleDevicefound.bind(this));
}).catch(() => {
// Retry to connect the BLE server if failed.
this._connectBleServer();
BluetoothHelper.prototype.connect = function() {
return bluetooth.defaultAdapter.startDiscovery().catch(() => {
// Retry to connect the BLE server if failed.
this.connect();
}).then(discovery => {
discovery.addEventListener('devicefound',
this._handleDevicefound.bind(this));
}).catch(() => {
// Retry to connect the BLE server if failed.
this.connect();
});
};
BluetoothHelper.prototype.disconnect = function() {
if (this._gatt) {
return this._gatt.disconnect().then(() => {
return bluetooth.defaultAdapter.stopDiscovery();
}).then(() => {
this.emit('disconnected');
this.isConnected = false;
});
},
} else {
return Promise.reject('GATT is undefined.');
}
};
_disconnectBleServer: function() {
console.log('Disconnecting...');
if (this._gatt) {
return this._gatt.disconnect().then(() => {
return this._bluetooth.defaultAdapter.stopDiscovery();
}).then(() => {
this.isConnected = false;
});
} else {
return Promise.resolve();
BluetoothHelper.prototype._handleAttributechanged = function(evt) {
for (var key in evt.attrs) {
switch (evt.attrs[key]) {
case 'defaultAdapter':
this.connect();
break;
}
},
}
};
_handleBluetoothAttributechanged: function(evt) {
for (var key in evt.attrs) {
switch (evt.attrs[key]) {
case 'defaultAdapter':
this._connectBleServer();
break;
}
}
},
BluetoothHelper.prototype._handleDevicefound = function(evt) {
var devcie = evt.device;
var gatt = devcie.gatt;
this._gatt = gatt;
if (devcie.name === this.name ||
devcie.address === this.address) {
this.name = devcie.name;
this.address = devcie.address;
gatt.connect().then(() => {
return this._discoverServices();
});
}
};
_handleDevicefound: function(evt) {
var devcie = evt.device;
var gatt = devcie.gatt;
this._gatt = gatt;
if (devcie.address === this.address) {
this.name = devcie.name;
gatt.connect().then(() => {
return this._discoverServices(gatt);
BluetoothHelper.prototype._discoverServices = function() {
var gatt = this._gatt;
return gatt.discoverServices().then(() => {
var service = gatt.services.find(function(service) {
return service.uuid === BLE_SERVICE_UUID;
});
this._writeChar =
service.characteristics.find(function(characteristic) {
return characteristic.uuid === BLE_RX_UUID;
});
this._notifyChar =
service.characteristics.find(function(characteristic) {
return characteristic.uuid === BLE_TX_UUID;
});
if (this._notifyChar && Array.isArray(this._notifyChar.descriptors)) {
this.emit('connected');
this.isConnected = true;
} else {
// Retry to discover services if failed.
this._discoverServices();
}
},
_discoverServices: function(gatt) {
return gatt.discoverServices().then(() => {
var service = gatt.services.find(function(service) {
return service.uuid === BLESHIELD_SERVICE_UUID;
});
this._writeChar =
service.characteristics.find(function(characteristic) {
return characteristic.uuid === BLESHIELD_RX_UUID;
});
this._notifyChar =
service.characteristics.find(function(characteristic) {
return characteristic.uuid === BLESHIELD_TX_UUID;
});
if (this._writeChar && Array.isArray(this._notifyChar.descriptors)) {
console.log('bluetoothready');
window.dispatchEvent(new CustomEvent('bluetoothready'));
this.isConnected = true;
} else {
console.log('Discovering services...');
this._discoverServices(gatt);
}
});
}
});
};

@@ -116,2 +119,1 @@

}(window));
{
"name": "bluetooth-helper",
"version": "0.0.1",
"version": "0.0.2",
"homepage": "https://github.com/evanxd/bluetooth-helper",

@@ -15,3 +15,6 @@ "authors": [

],
"license": "Apache License 2.0"
"license": "Apache License 2.0",
"dependencies": {
"eventemitter2": "0.4.14"
}
}
# bluetooth-helper
A wrapper for BLE APIs in FxOS. It help developers handle BLE things easier.
## How to Install
1. Declare bluetooth-helper as dependency module in `bower.json`. Please check [bower.json](http://bower.io/docs/creating-packages/).
2. Download the module: `bower install`.
3. Import the module in `<head>` in HTML file. `html<script src="bower_components/bluetooth-helper/lib/bluetooth_helper.js"></script>`
4. Done.
## How to Use
### Connect the device.
```js
var ble = new BluetoothHelper({
// Need BLE device's name or address to connect it.
name: 'BT_NAME',
// address: 'e4:a9:35:a4:ee:10'
});
```
### Send one byte data `EE` to BLE device.
```js
ble.on('connected', function() {
// You need to send one byte or more data at one time.
ble.send('EE');
});
```
### Reconnect the device.
```js
ble.disconnected();
ble.on('disconnected', function() {
ble.connect();
})
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc