Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
408 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<title>Battery Level</title> | ||
<body> | ||
<input id="name" type="text" placeholder="Device Name"> | ||
<input id="namePrefix" type="text" placeholder="Device Name Prefix"> | ||
<button type="button" onclick="onButtonClick()">Get Bluetooth Device's Battery Level</button> | ||
<pre id="log"></pre> | ||
<script> | ||
function onButtonClick() { | ||
clear(); | ||
var options = {filters: [{services: ['battery_service']}], optinalServices: []}; | ||
|
||
var filterName = document.getElementById('name').value; | ||
if (filterName) | ||
options.filters.push({name: filterName}); | ||
|
||
var filterNamePrefix = document.getElementById('namePrefix').value; | ||
if (filterNamePrefix) | ||
options.filters.push({namePrefix: filterNamePrefix}); | ||
|
||
try { | ||
log('Requesting Bluetooth Device...'); | ||
var bluetooth = window.navigator.bluetooth; | ||
var device = bluetooth.requestDevice(options); | ||
|
||
log('Connecting to GATT Server on device...'); | ||
var server = device.gatt.connect(); | ||
|
||
log('Getting Battery Service...'); | ||
var service = server.getPrimaryService('battery_service'); | ||
|
||
log('Getting Battery Level Characteristic...'); | ||
var characteristic = service.getCharacteristic('battery_level'); | ||
|
||
log('Reading Battery Level...'); | ||
var value = AsciiToDecimal(characteristic.readValue()); | ||
log('> Battery Level is ' + value + '%'); | ||
} catch(err) { | ||
log(err); | ||
} | ||
} | ||
|
||
function clear() { | ||
document.getElementById("log").textContent = ""; | ||
} | ||
|
||
function log(line) { | ||
document.getElementById("log").textContent += line + '\n'; | ||
} | ||
|
||
function AsciiToDecimal(bytestr) { | ||
var result = []; | ||
for(i = 0; i < bytestr.length; i++) { | ||
result[i] = bytestr[i].charCodeAt(0) ; | ||
} | ||
return result; | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<title>Characteristic info</title> | ||
<body> | ||
<input id="service" type="text" autofocus placeholder="Bluetooth Service"> | ||
<input id="characteristic" type="text" autofocus placeholder="Bluetooth Characteristic"> | ||
<button type="button" onclick="onButtonClick()">Get Characteristic Info</button> | ||
<pre id="log"></pre> | ||
<script> | ||
function onButtonClick() { | ||
clear(); | ||
var serviceUuid = document.getElementById('service').value; | ||
if (serviceUuid.startsWith('0x')) | ||
serviceUuid = parseInt(serviceUuid, 16); | ||
|
||
var characteristicUuid = document.getElementById('characteristic').value; | ||
if (characteristicUuid.startsWith('0x')) | ||
characteristicUuid = parseInt(characteristicUuid, 16); | ||
|
||
try { | ||
log('Requesting Bluetooth Device...'); | ||
var device = window.navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]}); | ||
|
||
log('Connecting to GATTserver on device...'); | ||
var server = device.gatt.connect(); | ||
|
||
log('Getting Primary Service...'); | ||
var primaryService = server.getPrimaryService(serviceUuid); | ||
|
||
log('Getting Characteristic...'); | ||
var characteristic = primaryService.getCharacteristic(characteristicUuid); | ||
|
||
log('Characteristic found!'); | ||
log('> Characteristic service: ' + characteristic.service.uuid); | ||
log('> Characteristic UUID: ' + characteristic.uuid); | ||
log('> Broadcast: ' + characteristic.properties.broadcast); | ||
log('> Read: ' + characteristic.properties.read); | ||
log('> Write w/o response: ' + characteristic.properties.writeWithoutResponse); | ||
log('> Write: ' + characteristic.properties.write); | ||
log('> Notify: ' + characteristic.properties.notify); | ||
log('> Indicate: ' + characteristic.properties.indicate); | ||
log('> Signed Write: ' + characteristic.properties.authenticatedSignedWrites); | ||
log('> Queued Write: ' + characteristic.properties.reliableWrite); | ||
log('> Writable Auxiliaries: ' + characteristic.properties.writableAuxiliaries); | ||
characteristic.readValue(); | ||
log('> Characteristic value: ' + AsciiToDecimal(characteristic.value)); | ||
} catch(err) { | ||
log(err); | ||
} | ||
} | ||
|
||
function clear() { | ||
document.getElementById("log").textContent = ""; | ||
} | ||
|
||
function log(line) { | ||
document.getElementById("log").textContent += line + '\n'; | ||
} | ||
|
||
function AsciiToDecimal(bytestr) { | ||
var result = []; | ||
for(i = 0; i < bytestr.length; i++) { | ||
result[i] = bytestr[i].charCodeAt(0) ; | ||
} | ||
return result; | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<title>Descriptor info</title> | ||
<body> | ||
<input id="service" type="text" autofocus placeholder="Bluetooth Service"> | ||
<input id="characteristic" type="text" autofocus placeholder="Bluetooth Characteristic"> | ||
<input id="descriptor" type="text" autofocus placeholder="Bluetooth Descriptor"> | ||
<button type="button" onclick="onButtonClick()">Get Descriptor Info</button> | ||
<pre id="log"></pre> | ||
<script> | ||
function onButtonClick() { | ||
clear(); | ||
var serviceUuid = document.getElementById('service').value; | ||
if (serviceUuid.startsWith('0x')) | ||
serviceUuid = parseInt(serviceUuid, 16); | ||
|
||
var characteristicUuid = document.getElementById('characteristic').value; | ||
if (characteristicUuid.startsWith('0x')) | ||
characteristicUuid = parseInt(characteristicUuid, 16); | ||
|
||
var descriptorUuid = document.getElementById('descriptor').value; | ||
if (descriptorUuid.startsWith('0x')) | ||
descriptorUuid = parseInt(descriptorUuid, 16); | ||
|
||
try { | ||
log('Requesting Bluetooth Device...'); | ||
var device = window.navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]}); | ||
|
||
log('Connecting to GATTserver on device...'); | ||
var server = device.gatt.connect(); | ||
|
||
log('Getting Primary Service...'); | ||
var primaryService = server.getPrimaryService(serviceUuid); | ||
|
||
log('Getting Characteristic...'); | ||
var characteristic = primaryService.getCharacteristic(characteristicUuid); | ||
|
||
log('Getting Descriptor...'); | ||
var descriptor = characteristic.getDescriptor(descriptorUuid); | ||
|
||
log('Descriptor found!'); | ||
log('> Descriptor characteristic: ' + descriptor.characteristic.uuid); | ||
log('> Descriptor UUID: ' + descriptor.uuid); | ||
descriptor.readValue(); | ||
log('> Descriptor value: ' + AsciiToDecimal(descriptor.value)); | ||
} catch(err) { | ||
log(err); | ||
} | ||
} | ||
|
||
function clear() { | ||
document.getElementById("log").textContent = ""; | ||
} | ||
|
||
function log(line) { | ||
document.getElementById("log").textContent += line + '\n'; | ||
} | ||
|
||
function AsciiToDecimal(bytestr) { | ||
var result = []; | ||
for(i = 0; i < bytestr.length; i++) { | ||
result[i] = bytestr[i].charCodeAt(0) ; | ||
} | ||
return result; | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<title>Device Disconnect</title> | ||
<body> | ||
<input id="service" type="text" autofocus placeholder="Bluetooth Service"> | ||
<input id="name" type="text" placeholder="Device Name"> | ||
<input id="namePrefix" type="text" placeholder="Device Name Prefix"> | ||
<button type="button" onclick="onScanButtonClick()">Scan()</button> | ||
<button type="button" onclick="onDisconnectButtonClick()">Disconnect()</button> | ||
<button type="button" onclick="onReconnectButtonClick()">Reconnect()</button> | ||
<pre id="log"></pre> | ||
<script> | ||
var bluetoothDevice; | ||
|
||
function onScanButtonClick() { | ||
clear(); | ||
var options = {filters: []}; | ||
|
||
var filterService = document.getElementById('service').value; | ||
if (filterService.startsWith('0x')) | ||
filterService = parseInt(filterService, 16); | ||
|
||
if (filterService) | ||
options.filters.push({services: [filterService]}); | ||
|
||
var filterName = document.getElementById('name').value; | ||
if (filterName) | ||
options.filters.push({name: filterName}); | ||
|
||
var filterNamePrefix = document.getElementById('namePrefix').value; | ||
if (filterNamePrefix) | ||
options.filters.push({namePrefix: filterNamePrefix}); | ||
|
||
bluetoothDevice = null; | ||
|
||
try { | ||
log('Requesting Bluetooth Device...'); | ||
bluetoothDevice = window.navigator.bluetooth.requestDevice(options); | ||
connect(); | ||
} catch(err) { | ||
log(err); | ||
} | ||
} | ||
|
||
function onDisconnectButtonClick() { | ||
clear(); | ||
if (!bluetoothDevice) | ||
return; | ||
|
||
try { | ||
log('Disconnecting from Bluetooth Device...'); | ||
if (bluetoothDevice.gatt.connected) { | ||
bluetoothDevice.gatt.disconnect(); | ||
log('> Bluetooth Device connected: ' + bluetoothDevice.gatt.connected); | ||
} else { | ||
log('> Bluetooth Device is already disconnected'); | ||
} | ||
} catch(err) { | ||
log(err); | ||
} | ||
} | ||
|
||
function onReconnectButtonClick() { | ||
clear(); | ||
if (!bluetoothDevice) | ||
log('> There is no connected Bluetooth Device instance') | ||
if (bluetoothDevice.gatt.connected) { | ||
log('> Bluetooth Device is already connected'); | ||
return; | ||
} else { | ||
connect(); | ||
} | ||
} | ||
|
||
function connect() { | ||
try { | ||
log('Connecting to Bluetooth Device...'); | ||
bluetoothDevice.gatt.connect(); | ||
log('> Bluetooth Device connected: ' + bluetoothDevice.gatt.connected); | ||
} catch(err) { | ||
log(err); | ||
} | ||
} | ||
|
||
function clear() { | ||
document.getElementById("log").textContent = ""; | ||
} | ||
|
||
function log(line) { | ||
document.getElementById("log").textContent += line + '\n'; | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<title>Device Info</title> | ||
<body> | ||
<input id="service" type="text" autofocus placeholder="Bluetooth Service"> | ||
<input id="name" type="text" placeholder="Device Name"> | ||
<input id="namePrefix" type="text" placeholder="Device Name Prefix"> | ||
<button type="button" onclick="onButtonClick()">Get Bluetooth Device Info</button> | ||
<pre id="log"></pre> | ||
<script> | ||
function onButtonClick() { | ||
clear(); | ||
var options = {filters: [], optinalServices: []}; | ||
|
||
var filterService = document.getElementById('service').value; | ||
if (filterService.startsWith('0x')) | ||
filterService = parseInt(filterService, 16); | ||
|
||
if (filterService) | ||
options.filters.push({services: [filterService]}); | ||
|
||
var filterName = document.getElementById('name').value; | ||
if (filterName) | ||
options.filters.push({name: filterName}); | ||
|
||
var filterNamePrefix = document.getElementById('namePrefix').value; | ||
if (filterNamePrefix) | ||
options.filters.push({namePrefix: filterNamePrefix}); | ||
|
||
try { | ||
log('Requesting Bluetooth Device...'); | ||
var device = window.navigator.bluetooth.requestDevice(options); | ||
|
||
log('Found a device!'); | ||
log('> Name: ' + device.name); | ||
log('> Id: ' + device.id); | ||
log('> Device Class: ' + device.deviceClass); | ||
log('> Vendor Id Source: ' + device.vendorIDSource); | ||
log('> Vendor Id: ' + device.vendorID); | ||
log('> Product Id: ' + device.productID); | ||
log('> Product Version: ' + device.productVersion); | ||
log('> Appearance: ' + device.adData.appearance); | ||
log('> Tx Power: ' + device.adData.txPower + ' dBm'); | ||
log('> RSSI: ' + device.adData.rssi + ' dBm'); | ||
} catch(err) { | ||
log(err); | ||
} | ||
} | ||
|
||
function clear() { | ||
document.getElementById("log").textContent = ""; | ||
} | ||
|
||
function log(line) { | ||
document.getElementById("log").textContent += line + '\n'; | ||
} | ||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.