Skip to content

Instantly share code, notes, and snippets.

@akey7
Created October 25, 2013 22:52
Show Gist options
  • Save akey7/51906412b8bfb5abc24b to your computer and use it in GitHub Desktop.
Save akey7/51906412b8bfb5abc24b to your computer and use it in GitHub Desktop.
BLE event handlers in JS
// There are some contrived things in here, but if you are talking about putting handlers in aan array, this may help you.
var allBleDevices = [];
function genericBleEventHanlder(ble_device, event) {
// Do something with the event
}
function makeSpecificBleHandler(bleDevice) {
return (function(bleDevice, event) {
// Do something with the bleDevice and event here
});
}
// Calls the callback on successful connection
function waitForBleToConnect(function(bleDevice) {
var deviceAndHandler = {
handler: makeSpecificBleHandler(bleDevice),
device: bleDevice;
};
allBleDevices.push(deviceAndHandler);
});
// Calls call back upon events
function awaitEvents(function(bleDeviceIndex, event) {
if (typeof allBleDevices[bleDeviceIndex] !== "undefined") {
allBleDevices[bleDeviceIndex].handler(event);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment