diff --git a/apps/BLEConnect/BLEConnect.js b/apps/BLEConnect/BLEConnect.js new file mode 100644 index 0000000000..47f5ff1584 --- /dev/null +++ b/apps/BLEConnect/BLEConnect.js @@ -0,0 +1,113 @@ +let menu = { + "": { "title": "BLECONNECT" }, + "RE-SCAN": () => scan() + }; + + function showMainMenu() { + menu["< Back"] = () => load(); + Bangle.drawWidgets(); + return E.showMenu(menu); + } + + function showDeviceInfo(device){ + const deviceMenu = { + "": { "title": "Device Info" }, + "name": { + value: device.name + }, + "Signal-Str(dBm)": { + value: device.rssi + // Very strong connection -30 to -60 + //strong -60 to -70 + //Moderate -70 to -80 + }, + "manufacturer": { + value: device.manufacturer===undefined ? "-" : device.manufacturer + }, + "CONNECT": () => connectDevice(device), // connect to a BLE device // + + "< Back": () => showMainMenu() // show a back button if wrong BLE selected // + }; + + return E.showMenu(deviceMenu); + } + + function connectDevice(device) { + E.showMessage("Connecting..."); + + NRF.connect(device.id) + .then(() => { + E.showMessage("Connected!"); + // Handle the connected device, for example, interact with its services + }) + .catch(err => { + E.showMessage("Failed to connect"); + console.log("Connection error: ", err); + }); + } + + function scan() { + menu = { + "": { "title": "BLE Detector" }, + "RE-SCAN": () => scan() + }; + + waitMessage(); + + NRF.findDevices(devices => { + devices.forEach(device => { + let deviceName = device.id.substring(0,17); + + if (device.name) { + deviceName = device.name; + } + + // Add a menu entry for each device + menu[deviceName] = () => showDeviceInfo(device); + }); + showMainMenu(); + }, { active: true }); + } + + function waitMessage() { + E.showMenu(); + E.showMessage("scanning"); + } + + Bangle.loadWidgets(); + scan(); + waitMessage(); + + // check error code status + + /*Please change your own errors for BLE machines */ + + function checkForErrorCode(status) { + if (status === "Auto Run") return; + if (status === "E-Stp") showErrorMessage("Emergency stop detected. Please check the machine."); + if (status === "Blocked") showErrorMessage("Machine blocked. Please inspect for obstructions."); + if (status === "Other") return; + // (other) showErrorMessage("Unknown error. Please investigate the issue."); + } + + // Show error message and allow Button 1 to confirm resolution + function showErrorMessage(status) { + Bangle.buzz(); + E.showMessage("Error Code: " + status + "\nDevice down! Please check"); + + // Wait for 3 seconds, then show the confirmation message + setTimeout(() => { + E.showMessage("Press Button 1 to confirm issue fixed"); + // Listen for Button 1 press to confirm issue fixed + setButtonWatch(); + }, 3000); + } + + // Wait for Button 1 press to confirm issue fixed + function setButtonWatch() { + // Button 1 is typically on the top-left of the Bangle.js (button 1 is the topmost button) + setWatch(() => { + confirmIssueFixed(); // Call the function to confirm issue has been fixed + }, BTN1, {repeat: false, edge: "falling"}); + } + diff --git a/apps/BLEConnect/BTApp.png b/apps/BLEConnect/BTApp.png new file mode 100644 index 0000000000..db5fafcd7d Binary files /dev/null and b/apps/BLEConnect/BTApp.png differ diff --git a/apps/BLEConnect/README.md b/apps/BLEConnect/README.md new file mode 100644 index 0000000000..f49e1926d3 --- /dev/null +++ b/apps/BLEConnect/README.md @@ -0,0 +1,3 @@ +# BLE-Connect + +custom app to scan and connect to ble devices. diff --git a/apps/BLEConnect/app-icon.js b/apps/BLEConnect/app-icon.js new file mode 100644 index 0000000000..f537afe90f --- /dev/null +++ b/apps/BLEConnect/app-icon.js @@ -0,0 +1,2 @@ +require("heatshrink").decompressatob("LS2DAP////////////////////////////////////////////////////////////////////////////2223////////////////+2222222//////////////22ySSSSS23////////////2ySSSySSSS3//////////+2ySSSfySSSS2/////////+2SSSST/ySSSSW////////+2SSSSSf/ySSSSW///////+2SSSSST//ySSSSW///////2SSSSSSf//ySSSSX//////2ySSSSST///ySSSS3/////2ySSSWySf///ySSSS3////+ySSSX+yT/z//ySSSS////+2SSST/+yf+T//ySSSW////2ySSSf/+z/yf/+SSSS3///+ySSSSf/+/+f/+SSSSS////2SSSSSf/////+SSSSSX///2ySSSSSf////+SSSSSS3//+2SSSSSSf///+SSSSSSW///2ySSSSSSf//+SSSSSSS3//+2SSSSSSSf/+SSSSSSSW///2ySSSSSSf//+SSSSSSS3//+2SSSSSSf///+SSSSSSW///2ySSSSSf////+SSSSSS3///2SSSSSf/////+SSSSSX///+ySSSSf/+f+f/+SSSSS////2ySSSf/+T/yf/+SSSS3///+2SSST/+Sf+X//ySSSW////+ySSST+ST/3//ySSSS/////2ySSSSSSf///ySSSS3/////2ySSSSST///ySSSS3/////+2SSSSSSf//ySSSSW//////+2SSSSST//ySSSSW///////+2SSSSSf/ySSSSW////////+2SSSST/ySSSSW/////////+2ySSSfySSSS2//////////+2ySSSySSSS2////////////22ySSSSS23/////////////+2222222/////////////////2223////////////////////////////////////////////////////////////////////////////+A=") + diff --git a/apps/BLEConnect/metadata.json b/apps/BLEConnect/metadata.json new file mode 100644 index 0000000000..39eceb1ff6 --- /dev/null +++ b/apps/BLEConnect/metadata.json @@ -0,0 +1,19 @@ +{ "id": "BLE_Connect", + "name": "BLE_Connect", + "shortName":"BLE_Conn", + "icon": "BTApp.png", + "version":"0.02", + + "description": "BT Scanner & Connect", + + "tags": "Connect", + "type": "Bluetooth" + + "supports": ["BANGLEJS"], + "readme": "README.md", + + "storage": [ + {"name":"BLE_Connect.app.js","url":"app.js"}, + {"name":"BTConnect_App.img","url":"app-icon.js","evaluate":true} + ] +} diff --git a/apps/BLE_Connect b/apps/BLE_Connect new file mode 100644 index 0000000000..a501673da2 --- /dev/null +++ b/apps/BLE_Connect @@ -0,0 +1,113 @@ +let menu = { + "": { "title": "BLE SCAN CONN" }, + "RE-SCAN": () => scan() + }; + + function showMainMenu() { + menu["< Back"] = () => load(); + Bangle.drawWidgets(); + return E.showMenu(menu); + } + + function showDeviceInfo(device){ + const deviceMenu = { + "": { "title": "Device Info" }, + "name": { + value: device.name + }, + "Signal-Str(dBm)": { + value: device.rssi + // Very strong connection -30 to -60 + //strong -60 to -70 + //Moderate -70 to -80 + }, + "manufacturer": { + value: device.manufacturer===undefined ? "-" : device.manufacturer + }, + "CONNECT": () => connectDevice(device), // connect to a BLE device // + + "< Back": () => showMainMenu() // show a back button if wrong BLE selected // + }; + + return E.showMenu(deviceMenu); + } + + function connectDevice(device) { + E.showMessage("Connecting..."); + + NRF.connect(device.id) + .then(() => { + E.showMessage("Connected!"); + // Handle the connected device, for example, interact with its services + }) + .catch(err => { + E.showMessage("Failed to connect"); + console.log("Connection error: ", err); + }); + } + + function scan() { + menu = { + "": { "title": "BLE Detector" }, + "RE-SCAN": () => scan() + }; + + waitMessage(); + + NRF.findDevices(devices => { + devices.forEach(device => { + let deviceName = device.id.substring(0,17); + + if (device.name) { + deviceName = device.name; + } + + // Add a menu entry for each device + menu[deviceName] = () => showDeviceInfo(device); + }); + showMainMenu(); + }, { active: true }); + } + + function waitMessage() { + E.showMenu(); + E.showMessage("scanning"); + } + + Bangle.loadWidgets(); + scan(); + waitMessage(); + + // check error code status + + /*Please change your own errors for BLE machines */ + + function checkForErrorCode(status) { + if (status === "Auto Run") return; + if (status === "E-Stp") showErrorMessage("Emergency stop detected. Please check the machine."); + if (status === "Blocked") showErrorMessage("Machine blocked. Please inspect for obstructions."); + if (status === "Other") return; + // (other) showErrorMessage("Unknown error. Please investigate the issue."); + } + + // Show error message and allow Button 1 to confirm resolution + function showErrorMessage(status) { + Bangle.buzz(); + E.showMessage("Error Code: " + status + "\nDevice down! Please check"); + + // Wait for 3 seconds, then show the confirmation message + setTimeout(() => { + E.showMessage("Press Button 1 to confirm issue fixed"); + // Listen for Button 1 press to confirm issue fixed + setButtonWatch(); + }, 3000); + } + + // Wait for Button 1 press to confirm issue fixed + function setButtonWatch() { + // Button 1 is typically on the top-left of the Bangle.js (button 1 is the topmost button) + setWatch(() => { + confirmIssueFixed(); // Call the function to confirm issue has been fixed + }, BTN1, {repeat: false, edge: "falling"}); + } +