Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 6a27a43

Browse files
authored
Merge pull request #888 from iankchristie/increaseMtu
Change MTU size of BLE transfer packets
2 parents b0d937f + 037a48c commit 6a27a43

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

nodejs/FatBeaconPeripheral/FatBeacon.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ var SERVICE_UUID = 'ae5946d4-e587-4ba8-b6a5-a97cca6affd3';
1313
var AdvertisementData =
1414
require('eddystone-beacon/lib/util/advertisement-data');
1515
var Eir = require('eddystone-beacon/lib/util/eir');
16+
var Gatt = require('bleno/lib/hci-socket/gatt');
1617

1718
var FAT_BEACON_FRAME_TYPE = 0x0e;
1819
var MAX_URL_LENGTH = 18;
1920
var ADVERTISING_HEADER_UUID = 'feaa';
21+
var ATT_OP_MTU_RESP = 0x03; // Refer to bleno/lib/hci-socket/gatt for doc
22+
var MIN_MTU = 23;
23+
var MAX_MTU = 505;
2024

2125
/**
2226
* this patch ensures that the correct Fatbeacon eir flag (0x06) is added
@@ -52,14 +56,41 @@ AdvertisementData.makeUrlBuffer = function(name) {
5256
return AdvertisementData.makeEirData(serviceData);
5357
}
5458

59+
/**
60+
* This allows us to change the negotiate the MTU size from 0 - 505. We have
61+
* set REQUESTING_MTU to 505 for maximum transfer rate.
62+
*/
63+
Gatt.prototype.handleMtuRequest = function(request) {
64+
var mtu = request.readUInt16LE(1);
65+
66+
if (mtu < MIN_MTU) {
67+
mtu = MIN_MTU;
68+
} else if (mtu > MAX_MTU) {
69+
mtu = MAX_MTU;
70+
}
71+
72+
this._mtu = mtu;
73+
74+
this.emit('mtuChange', this._mtu);
75+
76+
var response = Buffer.alloc(3);
77+
78+
response.writeUInt8(ATT_OP_MTU_RESP, 0);
79+
response.writeUInt16LE(mtu, 1);
80+
81+
return response;
82+
};
83+
5584
/*********************************************************/
5685

5786
var characteristic = new webpageCharacteristic();
5887

5988
fs.readFile("./html/fatBeaconDefault.html", function(err, data) {
60-
if(err) throw err;
89+
if (err) {
90+
throw err;
91+
}
92+
6193
characteristic.onWriteRequest(data, 0, null, null);
62-
console.log(service);
6394
});
6495

6596
var service = new bleno.PrimaryService({
@@ -71,7 +102,7 @@ var service = new bleno.PrimaryService({
71102

72103
bleno.once('advertisingStart', function(err) {
73104

74-
if(err) {
105+
if (err) {
75106
throw err;
76107
}
77108

nodejs/FatBeaconPeripheral/webpageCharacteristic.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var WebpageCharacteristic = function() {
1212
this._value = Buffer.alloc(0);
1313
this._updateValueCallback = null;
1414

15-
this._mtuSize = 251; // 5 less than the specified mtu
15+
this._mtuSize = 500; // 5 less than the specified mtu
1616
this._start = 0;
1717
this._end = this._mtuSize;
1818
};
@@ -34,7 +34,7 @@ WebpageCharacteristic.prototype.onReadRequest = function(offset, callback) {
3434
WebpageCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) {
3535
this._value = data;
3636

37-
console.log(`Writing - ${this._value}`);
37+
console.log(`Writing ${this._value.length} bytes`);
3838

3939
if (this._updateValueCallback) {
4040
console.log('WebpageCharacteristic - onWriteRequest: notifying');

0 commit comments

Comments
 (0)