cck33
Published © MIT

BLE Detection with the Bangle.js 2 smartwatch

The BLE-Detect app transforms your Bangle. js smartwatch into a portable BLE device scanner, identifying nearby Bluetooth Low Energy Devices.

BeginnerFull instructions provided1 hour172
BLE Detection with the Bangle.js 2 smartwatch

Things used in this project

Hardware components

Espruino Bangle.js 2
×1

Software apps and online services

Espruino IDE

Story

Read more

Code

ble_detect.app.js

JavaScript
This code provides a comprehensive framework for a BLE (Bluetooth Low Energy) detecting app on a Bangle.js 2 smartwatch.
var menu = {
  "": { "title": "BLE-Detect" },
  "Detect BLE-Devices": function() { scan(); }
};


function showMainMenu() {
  menu["< Back"] = function() { load(); }; 
  Bangle.drawWidgets(); 
  E.showMenu(menu); 
}

function waitMessage(scanProcess) {
  scanProcess = scanProcess || "Detecting...";
  E.showMenu(); 
  E.showMessage(scanProcess, "BLE Detector");
}

var startTime;
var scanDuration = 15;

function updateScanAnimation() {
  var currentTime = getTime();
  var elapsedTime = currentTime - startTime;
  var progress = elapsedTime / scanDuration;
  var centerX = 120; 
  var centerY = 120; 
  var maxRadius = 100; 

  g.clear();
  g.drawCircle(centerX, centerY, 12); 

  for (var i = 0; i < progress * 20; i++) {
    var angle = Math.random() * 2 * Math.PI;
    var radius = Math.random() * maxRadius * Math.sqrt(Math.random());
    var x = centerX + radius * Math.cos(angle);
    var y = centerY + radius * Math.sin(angle);
    g.fillCircle(x, y, 3); 
  }
}

function scan(options) {
  startTime = getTime(); 
  options = options || { active: true, timeout: scanDuration * 1000 }; 
  waitMessage("Detecting..."); 
  var animationInterval = setInterval(updateScanAnimation, 500); 

  NRF.findDevices(function(devices) {
    clearInterval(animationInterval); 
    handleScanResults(devices); 
  }, options);
}

function handleScanResults(devices) {
  if (devices.length === 0) {
    E.showMessage("No devices found", "Scan Result");
    setTimeout(showMainMenu, 2000); 
    return;
  }

  devices.forEach(function(device) {
    addDeviceToMenu(device);
  });

  showMainMenu();
}

function addDeviceToMenu(device) {
  var deviceName = device.name || device.id.substring(0, 17); 
  menu[deviceName] = function() { showDeviceInfo(device); };
}

function showDeviceInfo(device) {
  var deviceMenu = {
    "": { "title": "Device Info" },
    "Name": { value: device.name || "-" },
    "RSSI": { value: device.rssi.toString() },
    "Manufacturer": { value: device.manufacturer || "-" }
  };

  deviceMenu["< Back"] = function() { showMainMenu(); };

  E.showMenu(deviceMenu); 
}


Bangle.loadWidgets();
showMainMenu();
waitMessage("Initializing...");
scan({ active: true, timeout: 15000 }); 

ble_detector.img

JavaScript
No preview (download only).

ble_detector.info

JavaScript
require("Storage").write("ble_detector.info", {
  "id": "ble_detector",
  "name": "BLE Detect",
  "src": "ble_detector.app.js",
  "icon": "ble_detector.img",
  "type": "app"
});

Credits

cck33

cck33

3 projects • 0 followers

Comments