Skip to content

Commit

Permalink
Merge pull request #24 from NivSv/NivSv-patch-1
Browse files Browse the repository at this point in the history
Update index.html
  • Loading branch information
NivSv committed Dec 11, 2023
2 parents 73c6e8a + 46a0c93 commit aed31cc
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,69 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/svg+xml" href="/sv-logo.svg" />
<title>Niv Shtibel</title>
<script>
// Function to check if Bluetooth is available
function checkBluetoothAvailability() {
return navigator.bluetooth.getAvailability()
.then(isAvailable => {
if (isAvailable) {
console.log('Bluetooth is available!');
} else {
console.error('Bluetooth is not available on this device.');
}
return isAvailable;
});
}

// Function to request a Bluetooth device
function requestBluetoothDevice() {
console.log('Requesting any Bluetooth Device...');
return navigator.bluetooth.requestDevice({
// Filters for devices
acceptAllDevices: true
})
.then(device => {
console.log('> Selected Device: ' + device.name);
return device;
})
.catch(error => {
console.error('Argh! ' + error);
});
}

// Function to connect to the device and get the GATT Server
function connectToDevice(device) {
if (!device.gatt.connected) {
return device.gatt.connect()
.then(server => {
console.log('> GATT Server connected, getting service...');
// Here you can define the service you want to interact with
// For example: return server.getPrimaryService('battery_service');
})
.catch(error => {
console.error('Argh! ' + error);
});
}
console.log('Device is already connected');
}

// Event listener for initiating Bluetooth functionality
checkBluetoothAvailability()
.then(isAvailable => {
if (isAvailable) {
return requestBluetoothDevice();
}
})
.then(device => {
if (device) {
return connectToDevice(device);
}
})
.then(service => {
// Do something with the service
// ...
});
</script>
</head>
<body>
<div id="root"></div>
Expand Down

0 comments on commit aed31cc

Please sign in to comment.