ble.js
function gattServerCallbackHandler(winFunc, settings) {
	// collect read/write callbacks and add handles, so the native side can tell us which one to call.
	var readCallbacks = {};
	var writeCallbacks = {};
	var nextHandle = 1;

	function handleCallback(object, name, callbacks) {
		if(!object[name]) {
			throw name+" missing!";
		}
		callbacks[nextHandle] = object[name];
		object[name+"Handle"] = nextHandle;
		nextHandle += 1;
	}

	function handleReadWrite(object) {
		/* // primitive version
		if(!object.readRequestCallback) {
			throw "readRequestCallback missing!");
		}
		readCallbacks[nextHandle] = object.readRequestCallback;
		*/
		handleCallback(object, "onReadRequest", readCallbacks);
		handleCallback(object, "onWriteRequest", writeCallbacks);
	}

	for(var i=0; i<settings.services.length; i++) {
		var service = settings.services[i];
		for(var j=0; j<service.characteristics.length; j++) {
			var characteristic = service.characteristics[j];
			handleReadWrite(characteristic);
			for(var k=0; k<characteristic.descriptors.length; k++) {
				var descriptor = characteristic.descriptors[k];
				handleReadWrite(descriptor);
			}
		}
	}

	settings.nextHandle = nextHandle;

	return function(args) {
		// primitive version
		/*if(args.name == "win") {
			winFunc();
			return;
		}*/
		var funcs = {
			win: winFunc,
			connection: function() {
				settings.onConnectionStateChange(args.deviceHandle, args.connected);
			},
			write: function() {
				writeCallbacks[args.callbackHandle](args.deviceHandle, args.requestId, args.data);
			},
			read: function() {
				readCallbacks[args.callbackHandle](args.deviceHandle, args.requestId);
			},
		};
		funcs[args.name]();
	};
}
function gattServerCallbackHandler(winFunc, settings) {
	// collect read/write callbacks and add handles, so the native side can tell us which one to call.
	var readCallbacks = {};
	var writeCallbacks = {};
	var nextHandle = 1;

	function handleCallback(object, name, callbacks) {
		if(!object[name]) {
			throw name+" missing!";
		}
		callbacks[nextHandle] = object[name];
		object[name+"Handle"] = nextHandle;
		nextHandle += 1;
	}

	function handleReadWrite(object) {
		/* // primitive version
		if(!object.readRequestCallback) {
			throw "readRequestCallback missing!");
		}
		readCallbacks[nextHandle] = object.readRequestCallback;
		*/
		handleCallback(object, "onReadRequest", readCallbacks);
		handleCallback(object, "onWriteRequest", writeCallbacks);
	}

	for(var i=0; i<settings.services.length; i++) {
		var service = settings.services[i];
		for(var j=0; j<service.characteristics.length; j++) {
			var characteristic = service.characteristics[j];
			handleReadWrite(characteristic);
			for(var k=0; k<characteristic.descriptors.length; k++) {
				var descriptor = characteristic.descriptors[k];
				handleReadWrite(descriptor);
			}
		}
	}

	settings.nextHandle = nextHandle;

	return function(args) {
		// primitive version
		/*if(args.name == "win") {
			winFunc();
			return;
		}*/
		var funcs = {
			win: winFunc,
			connection: function() {
				settings.onConnectionStateChange(args.deviceHandle, args.connected);
			},
			write: function() {
				writeCallbacks[args.callbackHandle](args.deviceHandle, args.requestId, args.data);
			},
			read: function() {
				readCallbacks[args.callbackHandle](args.deviceHandle, args.requestId);
			},
		};
		funcs[args.name]();
	};
}
function gattServerCallbackHandler ( winFunc , settings ) { // collect read/write callbacks and add handles, so the native side can tell us which one to call. var readCallbacks = { } ; var writeCallbacks = { } ; var nextHandle = 1 ; function handleCallback ( object , name , callbacks ) { if ( ! object [ name ] ) { throw name + " missing!" ; } callbacks [ nextHandle ] = object [ name ] ; object [ name + "Handle" ] = nextHandle ; nextHandle += 1 ; } function handleReadWrite ( object ) { /* // primitive version
		if(!object.readRequestCallback) {
			throw "readRequestCallback missing!");
		}
		readCallbacks[nextHandle] = object.readRequestCallback;
		*/ handleCallback ( object , "onReadRequest" , readCallbacks ) ; handleCallback ( object , "onWriteRequest" , writeCallbacks ) ; } for ( var i = 0 ; i < settings . services . length ; i ++ ) { var service = settings . services [ i ] ; for ( var j = 0 ; j < service . characteristics . length ; j ++ ) { var characteristic = service . characteristics [ j ] ; handleReadWrite ( characteristic ) ; for ( var k = 0 ; k < characteristic . descriptors . length ; k ++ ) { var descriptor = characteristic . descriptors [ k ] ; handleReadWrite ( descriptor ) ; } } } settings . nextHandle = nextHandle ; return function ( args ) { // primitive version /*if(args.name == "win") {
			winFunc();
			return;
		}*/ var funcs = { win : winFunc , connection : function ( ) { settings . onConnectionStateChange ( args . deviceHandle , args . connected ) ; } , write : function ( ) { writeCallbacks [ args . callbackHandle ] ( args . deviceHandle , args . requestId , args . data ) ; } , read : function ( ) { readCallbacks [ args . callbackHandle ] ( args . deviceHandle , args . requestId ) ; } , } ; funcs [ args . name ] ( ) ; } ; }
Internal. Returns a function that will handle GATT server callbacks.
Internal . Returns a function that will handle GATT server callbacks .