index.js
/*
* Connect to the earbud and discover its services 
*/
Connect to the earbud and discover its services
Connect to the earbud and discover its services
function connectToPeripheral(peripheral) {
	logger.info("Trying to connect")
	io.sockets.emit('status', { message: "Earbud found." })
	io.sockets.emit('status', { message: "Trying to connect..." })
	earbud = peripheral
	peripheral.connect(function (err) {
		io.sockets.emit('status', { message: "Earbud connected!" });
		io.sockets.emit('eSense-connect', { isConnected: true });
		peripheral.discoverServices(['ff06'], function (err, services) {
			services.forEach(function (service) {
				logger.info('found service:', service.uuid);
				service.discoverCharacteristics(['ff07', 'ff08', 'ff09', 'ff0d', 'ff0e'], function (err, characteristics) {
					characteristics.forEach(function (characteristic) {
						logger.info("Found sensor characteristic : " + characteristic.uuid)
						if (characteristic.uuid == "ff07")
							control_characteristic = characteristic
						else if (characteristic.uuid == "ff08")
							data_characteristic = characteristic
						else if (characteristic.uuid == "ff09")
							subscribeToButtonEvents(characteristic)
						else if (characteristic.uuid == "ff0d")
							readFactoryCalibrationValues(characteristic)
						else if (characteristic.uuid == "ff0e") {
							sensor_config_characteristic = characteristic;
							readSensorConfiguration(sensor_config_characteristic);
						}
					})
				})
			})
		})
	})
	disconnect_handler = function () {
		logger.info('Peripheral disconnected ' + peripheral);
		io.sockets.emit('status', { message: "Earbud disconnected" })
		io.sockets.emit('eSense-connect', { isConnected: false });
		earbud = null;
		peripheral.removeListener('disconnect', disconnect_handler);
	};
	peripheral.on('disconnect', disconnect_handler);
}
function connectToPeripheral ( peripheral ) { logger . info ( "Trying to connect" ) io . sockets . emit ( 'status' , { message : "Earbud found." } ) io . sockets . emit ( 'status' , { message : "Trying to connect..." } ) earbud = peripheral peripheral . connect ( function ( err ) { io . sockets . emit ( 'status' , { message : "Earbud connected!" } ) ; io . sockets . emit ( 'eSense-connect' , { isConnected : true } ) ; peripheral . discoverServices ( [ 'ff06' ] , function ( err , services ) { services . forEach ( function ( service ) { logger . info ( 'found service:' , service . uuid ) ; service . discoverCharacteristics ( [ 'ff07' , 'ff08' , 'ff09' , 'ff0d' , 'ff0e' ] , function ( err , characteristics ) { characteristics . forEach ( function ( characteristic ) { logger . info ( "Found sensor characteristic : " + characteristic . uuid ) if ( characteristic . uuid == "ff07" ) control_characteristic = characteristic else if ( characteristic . uuid == "ff08" ) data_characteristic = characteristic else if ( characteristic . uuid == "ff09" ) subscribeToButtonEvents ( characteristic ) else if ( characteristic . uuid == "ff0d" ) readFactoryCalibrationValues ( characteristic ) else if ( characteristic . uuid == "ff0e" ) { sensor_config_characteristic = characteristic ; readSensorConfiguration ( sensor_config_characteristic ) ; } } ) } ) } ) } ) } ) disconnect_handler = function ( ) { logger . info ( 'Peripheral disconnected ' + peripheral ) ; io . sockets . emit ( 'status' , { message : "Earbud disconnected" } ) io . sockets . emit ( 'eSense-connect' , { isConnected : false } ) ; earbud = null ; peripheral . removeListener ( 'disconnect' , disconnect_handler ) ; } ; peripheral . on ( 'disconnect' , disconnect_handler ) ; }
Connect to the earbud and discover its services
Connect to the earbud and discover its services