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;