Created
April 22, 2025 06:51
-
-
Save 491239/103021cac323b1d0d1804affe06df8f7 to your computer and use it in GitHub Desktop.
web Bluetooth test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Elecrtron Bluetooth Test</title> | |
<script src="renderer.js"></script> | |
</head> | |
<body> | |
<h3>Elecrtron Bluetooth Test</h3> | |
<button id="scan">scan</button> | |
<button id="cancel">cancel</button> | |
<ul id="device-list"></ul> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ipcMain, app, BrowserWindow } = require('electron') | |
const path = require('path') | |
let win = null; | |
var callbackForBluetoothEvent = null; | |
// Create the browser window. | |
function createWindow () { | |
win = new BrowserWindow({ | |
webPreferences: { | |
preload: path.join(__dirname, 'preload.js') | |
} | |
}) | |
win.maximize(); | |
win.webContents.openDevTools(); | |
win.loadFile("index.html"); | |
//This sender sends the devicelist from the main process to all renderer processes | |
win.webContents.on('select-bluetooth-device', (event, deviceList, callback) => { | |
event.preventDefault(); //important, otherwise first available device will be selected | |
let bluetoothDeviceList = deviceList; | |
callbackForBluetoothEvent = callback; //to make it accessible outside createWindow() | |
win.webContents.send('channelForBluetoothDeviceList', bluetoothDeviceList); | |
}); | |
} | |
app.on('ready', createWindow); | |
//cancels Discovery | |
ipcMain.on('channelForTerminationSignal', _ => { | |
console.log("Cancel Discovery"); | |
if (callbackForBluetoothEvent) { | |
callbackForBluetoothEvent(''); //reference to callback of win.webContents.on('select-bluetooth-device'...) | |
console.log("Discovery cancelled"); | |
} | |
}); | |
//resolves navigator.bluetooth.requestDevice() and stops device discovery | |
ipcMain.on('channelForSelectingDevice', (event, DeviceId) => { | |
callbackForBluetoothEvent(DeviceId); //reference to callback of win.webContents.on('select-bluetooth-device'...) | |
console.log("Device selected, discovery finished"); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "stormy-teaching-trick-204bh", | |
"productName": "stormy-teaching-trick-204bh", | |
"description": "My Electron application description", | |
"keywords": [], | |
"main": "./main.js", | |
"version": "1.0.0", | |
"author": "ENERGIZE230228", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"dependencies": {}, | |
"devDependencies": { | |
"electron": "35.1.5" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ipcRenderer, contextBridge } = require('electron'); | |
contextBridge.exposeInMainWorld('bluetooth', { | |
onFoundDevice: (callback) => { | |
ipcRenderer.on('channelForBluetoothDeviceList', (event, list) => callback(list)); | |
}, | |
cancelScan: () => { | |
ipcRenderer.send('channelForTerminationSignal'); | |
}, | |
selectDevice: (deviceId) => { | |
ipcRenderer.send('channelForSelectingDevice', deviceId); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let deviceListEl; | |
window.onload = function () { | |
deviceListEl = document.getElementById('device-list'); | |
let scanEl = document.getElementById('scan'); | |
let cancelEl = document.getElementById('cancel'); | |
scanEl.addEventListener('click', _ => { | |
navigator.bluetooth.requestDevice( | |
{ | |
// filters: [{ services: [0xffff] }], | |
acceptAllDevices: true | |
} | |
).then(device => { | |
// console.log(device); | |
}).catch(error => { | |
console.log(error); | |
}); | |
}); | |
cancelEl.addEventListener('click', _ => { | |
bluetooth.cancelScan(); | |
}); | |
bluetooth.onFoundDevice((list) => { | |
//console.log(list); | |
deviceListEl.innerHTML = ''; | |
list.forEach(device => { | |
let option = document.createElement('li'); | |
option.innerHTML = device.deviceName; | |
option.addEventListener('click', _ => { | |
bluetooth.selectDevice(device.deviceId); | |
}); | |
// console.log(option); | |
deviceListEl.appendChild(option); | |
}); | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* styles.css */ | |
/* Add styles here to customize the appearance of your app */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment