From 61dd8e7ce0e5a30bc1acf08ebc37d737260d7851 Mon Sep 17 00:00:00 2001 From: Bryce Jacobs Date: Thu, 8 Oct 2015 18:32:40 -0500 Subject: [PATCH] linux - have handle managment behave like os x --- lib/hci-socket/gatt.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/hci-socket/gatt.js b/lib/hci-socket/gatt.js index d1ae20fb..07df80af 100644 --- a/lib/hci-socket/gatt.js +++ b/lib/hci-socket/gatt.js @@ -211,7 +211,20 @@ Gatt.prototype.addService = function AddGATTService(service, indicate){ throw new Error('Not enough space to add service.'); } } else { - attHandle = handles.length; + /** + * This is put in place so that Linux handle management behaves how OS X handle management does. + * Quick Primer: On OS X, if the last service towards the end of our attributes is taken out, os x will + * use it's handles if another service is added after. Therefore, on linux we need the service that's + * to be added to take the first spot AFTER our last service that's on the array. + */ + var lastServiceIndex = null; + for(var i = 0; i < handles.length; i++){ + var handle = handles[i]; + if(handle && handle.type === 'service'){ + lastServiceIndex = i; + } + } + attHandle = handles[lastServiceIndex].endHandle + 1; } var startHandle = attHandle;