Skip to content

Commit

Permalink
- gatttool read
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepmistry committed May 4, 2013
1 parent ab82628 commit 313f528
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/linux/bindings.js
Expand Up @@ -62,6 +62,7 @@ nobleBindings.connect = function(peripheralUuid) {
this._gatttool[address].on('disconnect', this.onDisconnect.bind(this));
this._gatttool[address].on('servicesDiscover', this.onServicesDiscovered.bind(this));
this._gatttool[address].on('characteristicsDiscover', this.onCharacteristicsDiscovered.bind(this));
this._gatttool[address].on('read', this.onRead.bind(this));

this._gatttool[address].connect();
};
Expand Down Expand Up @@ -124,7 +125,15 @@ nobleBindings.onCharacteristicsDiscovered = function(address, serviceUuid, chara
};

nobleBindings.read = function(peripheralUuid, serviceUuid, characteristicUuid) {
var address = this._addresses[peripheralUuid];

this._gatttool[address].read(serviceUuid, characteristicUuid);
};

nobleBindings.onRead = function(address, serviceUuid, characteristicUuid, data) {
var uuid = address.split(':').join('').toLowerCase();

this.emit('read', uuid, serviceUuid, characteristicUuid, data, false);
};

nobleBindings.write = function(peripheralUuid, serviceUuid, characteristicUuid, data, notify) {
Expand Down
24 changes: 24 additions & 0 deletions lib/linux/gatttool.js
Expand Up @@ -81,6 +81,8 @@ Gatttool.prototype.executeCommand = function(command, callback) {
};

Gatttool.prototype.connect = function() {
// TODO: handle 'public' and 'random'

this.executeCommand('connect', function(output) {
// console.log('connect output: ' + JSON.stringify(output));
}.bind(this));
Expand Down Expand Up @@ -210,4 +212,26 @@ Gatttool.prototype.discoverCharacteristics = function(serviceUuid) {
}.bind(this));
};

Gatttool.prototype.read = function(serviceUuid, characteristicUuid) {
var characteristic = this._characteristics[serviceUuid][characteristicUuid];

this.executeCommand('char-read-hnd ' + characteristic.valueHandle, function(output) {
console.log('read output: ' + JSON.stringify(output));

for (var i in output) {
var line = output[i].trim();

if(found = line.match(/^Characteristic value\/descriptor: (.*)$/)) {
var bytes = found[1].trim().split(' ');

for (var i in bytes) {
bytes[i] = parseInt(bytes[i], 16);
}

this.emit('read', this._address, serviceUuid, characteristicUuid, new Buffer(bytes));
}
}
}.bind(this));
};

module.exports = Gatttool;
4 changes: 2 additions & 2 deletions test.js
Expand Up @@ -102,11 +102,11 @@ noble.on('discover', function(peripheral) {
});


//characteristics[characteristicIndex].read();
characteristics[characteristicIndex].read();
//characteristics[characteristicIndex].write(new Buffer('hello'));
//characteristics[characteristicIndex].broadcast(true);
//characteristics[characteristicIndex].notify(true);
characteristics[characteristicIndex].discoverDescriptors();
// characteristics[characteristicIndex].discoverDescriptors();
});


Expand Down

0 comments on commit 313f528

Please sign in to comment.