Update WebBluetooth to use Promises

This commit is contained in:
Mátyás Mustoha 2016-09-15 10:44:17 +02:00 committed by Attila Dusnoki
parent fb52bb7c8d
commit e05a839d25
32 changed files with 684 additions and 458 deletions

View file

@ -10,26 +10,30 @@
clear();
var options = {filters: [{services: ['battery_service']}], optionalServices: []};
try {
log('Requesting Bluetooth Device...');
var bluetooth = window.navigator.bluetooth;
var device = bluetooth.requestDevice(options);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(options)
.then(device => {
log('Connecting to GATT Server on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
log('Getting Battery Service...');
var service = server.getPrimaryService('battery_service');
return server.getPrimaryService('battery_service');
})
.then(service => {
log('Getting Battery Level Characteristic...');
var characteristic = service.getCharacteristic('battery_level');
return service.getCharacteristic('battery_level');
})
.then(characteristic => {
log('Reading Battery Level...');
var value = asciiToDecimal(characteristic.readValue());
log('> Battery Level is ' + value + '%');
} catch(err) {
return characteristic.readValue();
})
.then(value => {
log('> Battery Level is ' + asciiToDecimal(value) + '%');
})
.catch(err => {
log(err);
}
});
}
</script>
</body>

View file

@ -20,26 +20,30 @@
if (filterNamePrefix)
options.filters[0].namePrefix = filterNamePrefix;
try {
log('Requesting Bluetooth Device...');
var bluetooth = window.navigator.bluetooth;
var device = bluetooth.requestDevice(options);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(options)
.then(device => {
log('Connecting to GATT Server on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
log('Getting Battery Service...');
var service = server.getPrimaryService('battery_service');
return server.getPrimaryService('battery_service');
})
.then(service => {
log('Getting Battery Level Characteristic...');
var characteristic = service.getCharacteristic('battery_level');
return service.getCharacteristic('battery_level');
})
.then(characteristic => {
log('Reading Battery Level...');
var value = asciiToDecimal(characteristic.readValue());
log('> Battery Level is ' + value + '%');
} catch(err) {
return characteristic.readValue();
})
.then(value => {
log('> Battery Level is ' + asciiToDecimal(value) + '%');
})
.catch(err => {
log(err);
}
});
}
</script>
</body>

View file

@ -22,19 +22,21 @@
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('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]})
.then(device => {
log('Connecting to GATT Server on device...');
return device.gatt.connect();
})
.then(server => {
log('Getting Primary Service...');
var primaryService = server.getPrimaryService(serviceUuid);
return server.getPrimaryService(serviceUuid);
})
.then(service => {
log('Getting Characteristic...');
var characteristic = primaryService.getCharacteristic(characteristicUuid);
return service.getCharacteristic(characteristicUuid);
})
.then(characteristic => {
log('Characteristic found!');
log('> Characteristic service: ' + characteristic.service.uuid);
log('> Characteristic UUID: ' + characteristic.uuid);
@ -47,11 +49,14 @@
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) {
return characteristic.readValue();
})
.then(value => {
log('> Characteristic value: ' + asciiToDecimal(value));
})
.catch(err => {
log(err);
}
});
}
</script>
</body>

View file

@ -32,32 +32,42 @@
function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]});
var bt_server;
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]})
.then(device => {
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
bt_server = server;
log('Getting Primary Service "heart_rate"...');
var primaryService = server.getPrimaryService('heart_rate');
return server.getPrimaryService('heart_rate');
})
.then(service => {
log('Getting Characteristic "' + testCases[testNumber].characteristic + '"...');
var characteristic = primaryService.getCharacteristic(testCases[testNumber].characteristic);
return service.getCharacteristic(testCases[testNumber].characteristic);
})
.then(characteristic => {
log('Characteristic found!');
if (testCases[testNumber].mustDisconnect) {
log('Disconnecting from server...');
device.gatt.disconnect();
bt_server.disconnect();
}
log('Reading the value of the Characteristic...');
characteristic.readValue();
log('> Characteristic value: ' + asciiToDecimal(characteristic.value));
} catch(err) {
return characteristic.readValue();
})
.then(value => {
log('> Characteristic value: ' + asciiToDecimal(value));
})
.catch(err => {
log(err);
}
});
}
populate(testCases);

View file

@ -34,28 +34,36 @@
function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice({filters: [{services: [0x1234]}]});
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
var bt_server;
log('Getting Primary Service "Test Service"...');
var primaryService = server.getPrimaryService(0x1234);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice({filters: [{services: [0x1234]}]})
.then(device => {
log('Connecting to GATT Server on device...');
return device.gatt.connect();
})
.then(server => {
bt_server = server;
log('Getting Primary Service...');
return server.getPrimaryService(0x1234);
})
.then(service => {
log('Getting Characteristic "' + testCases[testNumber].characteristic + '"...');
var characteristic = primaryService.getCharacteristic(testCases[testNumber].characteristic);
return service.getCharacteristic(testCases[testNumber].characteristic);
})
.then(characteristic => {
log('Characteristic found!');
log('Reading the old value of the Characteristic...');
characteristic.readValue();
return characteristic.readValue();
})
.then(value => {
log('> Characteristic value: ' + asciiToDecimal(characteristic.value));
if (testCases[testNumber].mustDisconnect) {
log('Disconnecting from server...');
device.gatt.disconnect();
bt_server.disconnect();
}
if (testNumber !== 1) {
@ -64,14 +72,18 @@
log('Writing the value of the Characteristic with a 513 long array...');
}
characteristic.writeValue(testCases[testNumber].valueToWrite);
return characteristic.writeValue(testCases[testNumber].valueToWrite);
})
.then(_ => {
log('Reading the new value of the Characteristic...');
characteristic.readValue();
log('> Characteristic value: ' + asciiToDecimal(characteristic.value));
} catch(err) {
return characteristic.readValue();
})
.then(value => {
log('> Characteristic value: ' + asciiToDecimal(value));
})
.catch(err => {
log(err);
}
});
}
populate(testCases);

View file

@ -27,30 +27,36 @@
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('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]})
.then(device => {
log('Connecting to GATT Server on device...');
return device.gatt.connect();
})
.then(server => {
log('Getting Primary Service...');
var primaryService = server.getPrimaryService(serviceUuid);
return server.getPrimaryService(serviceUuid);
})
.then(service => {
log('Getting Characteristic...');
var characteristic = primaryService.getCharacteristic(characteristicUuid);
return service.getCharacteristic(characteristicUuid);
})
.then(characteristic => {
log('Getting Descriptor...');
var descriptor = characteristic.getDescriptor(descriptorUuid);
return characteristic.getDescriptor(descriptorUuid);
})
.then(descriptor => {
log('Descriptor found!');
log('> Descriptor characteristic: ' + descriptor.characteristic.uuid);
log('> Descriptor UUID: ' + descriptor.uuid);
descriptor.readValue();
log('> Descriptor value: ' + asciiToDecimal(descriptor.value));
} catch(err) {
return descriptor.readValue();
})
.then(value => {
log('> Descriptor value: ' + asciiToDecimal(value));
})
.catch(err => {
log(err);
}
});
}
</script>
</body>

View file

@ -22,33 +22,46 @@
function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]});
var bt_server;
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]})
.then(device => {
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
bt_server = server;
log('Getting Primary Service "heart_rate"...');
var primaryService = server.getPrimaryService('heart_rate');
return server.getPrimaryService('heart_rate');
})
.then(service => {
log('Getting Characteristic "heart_rate_measurement"...');
var characteristic = primaryService.getCharacteristic('heart_rate_measurement');
return service.getCharacteristic('heart_rate_measurement');
})
.then(characteristic => {
log('Getting Descriptor "' + testCases[testNumber].descriptor + '"...');
var descriptor = characteristic.getDescriptor(testCases[testNumber].descriptor);
return characteristic.getDescriptor(testCases[testNumber].descriptor);
})
.then(descriptor => {
log('Descriptor found!');
if (testCases[testNumber].mustDisconnect) {
log('Disconecting from GATTserver');
device.gatt.disconnect();
bt_server.disconnect();
}
log("Reading descriptor's value...");
descriptor.readValue();
log('> Descriptor value: ' + asciiToDecimal(descriptor.value));
} catch(err) {
return descriptor.readValue();
})
.then(value => {
log('> Descriptor value: ' + asciiToDecimal(value));
})
.catch(err => {
log(err);
}
});
}
populate(testCases);

View file

@ -22,31 +22,44 @@
function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice({filters: [{services: [0x1234]}]});
var bt_server;
var bt_descriptor;
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice({filters: [{services: [0x1234]}]})
.then(device => {
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
bt_server = server;
log('Getting Primary Service "Test Service"...');
var primaryService = server.getPrimaryService(0x1234);
return server.getPrimaryService(0x1234);
})
.then(service => {
log('Getting Characteristic "Test Characteristic (0x2345)"...');
var characteristic = primaryService.getCharacteristic(0x2345);
return service.getCharacteristic(0x2345);
})
.then(characteristic => {
log('Characteristic found!');
log('Getting Descriptor "' + testCases[testNumber].descriptor + '"...');
var descriptor = characteristic.getDescriptor(testCases[testNumber].descriptor);
return characteristic.getDescriptor(testCases[testNumber].descriptor);
})
.then(descriptor => {
bt_descriptor = descriptor;
log('Reading the old value of the Descriptor...');
descriptor.readValue();
log('> Descriptor value: ' + asciiToDecimal(descriptor.value));
return descriptor.readValue();
})
.then(value => {
log('> Descriptor value: ' + asciiToDecimal(value));
if (testCases[testNumber].mustDisconnect) {
log('Disconnecting from server...');
device.gatt.disconnect();
bt_server.disconnect();
}
if (testNumber !== 1) {
@ -55,14 +68,18 @@
log('Writing the value of the Descriptor with a 513 long array...');
}
descriptor.writeValue(testCases[testNumber].valueToWrite);
return bt_descriptor.writeValue(testCases[testNumber].valueToWrite);
})
.then(_ => {
log('Reading the new value of the Descriptor...');
descriptor.readValue();
log('> Descriptor value: ' + asciiToDecimal(descriptor.value));
} catch(err) {
return bt_descriptor.readValue();
})
.then(value => {
log('> Descriptor value: ' + asciiToDecimal(value));
})
.catch(err => {
log(err);
}
});
}
populate(testCases);

View file

@ -32,15 +32,18 @@
if (filterNamePrefix)
options.filters[0].namePrefix = filterNamePrefix;
try {
clear();
log('Requesting Bluetooth Device...');
bluetoothDevice = window.navigator.bluetooth.requestDevice(options);
clear();
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(options)
.then(device => {
bluetoothDevice = device;
log('Connecting to Bluetooth Device...');
connect();
} catch(err) {
})
.catch(err => {
log(err);
}
});
}
function onDisconnectButtonClick() {
@ -75,12 +78,14 @@
}
function connect() {
try {
log('Result of the connect() method of the GATT Server: ' + bluetoothDevice.gatt.connect());
log('> Bluetooth Device connected: ' + bluetoothDevice.gatt.connected);
} catch(err) {
bluetoothDevice.gatt.connect()
.then(server => {
log('Result of the connect() method of the GATT Server: ' + server);
log('> Bluetooth Device connected: ' + server.connected);
})
.catch(err => {
log(err);
}
});
}
</script>
</body>

View file

@ -28,19 +28,19 @@
if (filterNamePrefix)
options.filters.push({namePrefix: filterNamePrefix});
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice(options);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(options)
.then(device => {
log('Found a device!');
log('> Name: ' + device.name);
log('> Id: ' + device.id);
log('> Appearance: ' + device.adData.appearance);
log('> Tx Power: ' + device.adData.txPower + ' dBm');
log('> RSSI: ' + device.adData.rssi + ' dBm');
} catch(err) {
})
.catch(err => {
log(err);
}
});
}
</script>
</body>

View file

@ -44,19 +44,22 @@
function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice(testCases[testNumber].options);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(testCases[testNumber].options)
.then(device => {
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
log('Getting Primary Service "' + testCases[testNumber].service + '"...');
var primaryService = server.getPrimaryService(testCases[testNumber].service);
return server.getPrimaryService(testCases[testNumber].service);
})
.then(service => {
log('Getting Characteristic "' + testCases[testNumber].characteristic + '"...');
var characteristic = primaryService.getCharacteristic(testCases[testNumber].characteristic);
return service.getCharacteristic(testCases[testNumber].characteristic);
})
.then(characteristic => {
log('Characteristic found!');
log('> Characteristic service: ' + characteristic.service.uuid);
log('> Characteristic UUID: ' + characteristic.uuid);
@ -69,11 +72,14 @@
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) {
return characteristic.readValue();
})
.then(value => {
log('> Characteristic value: ' + asciiToDecimal(value));
})
.catch(err => {
log(err);
}
});
}
populate(testCases);

View file

@ -43,21 +43,25 @@
testCases.push({characteristic: '00002a03-0000-1000-8000-00805f9b34fb', service: 'heart_rate', options: {filters: [{services: ['heart_rate']}], optionalServices: ['cycling_power']} });
//Test 18
testCases.push({characteristic: '00002a25-0000-1000-8000-00805f9b34fb', service: 'heart_rate', options: {filters: [{services: ['heart_rate']}], optionalServices: ['cycling_power']} });
function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice(testCases[testNumber].options);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(testCases[testNumber].options)
.then(device => {
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
log('Getting Primary Service "' + testCases[testNumber].service + '"...');
var primaryService = server.getPrimaryService(testCases[testNumber].service);
return server.getPrimaryService(testCases[testNumber].service);
})
.then(service => {
log('Getting Characteristic "' + testCases[testNumber].characteristic + '"...');
var characteristics = primaryService.getCharacteristics(testCases[testNumber].characteristic);
return service.getCharacteristics(testCases[testNumber].characteristic);
})
.then(characteristics => {
log('> List of Characteristics on the current device:');
for(i = 0; i < characteristics.length; ++i) {
@ -73,12 +77,15 @@
log('> Signed Write: ' + characteristics[i].properties.authenticatedSignedWrites);
log('> Queued Write: ' + characteristics[i].properties.reliableWrite);
log('> Writable Auxiliaries: ' + characteristics[i].properties.writableAuxiliaries);
characteristics[i].readValue();
log('> Characteristic value: ' + asciiToDecimal(characteristics[i].value));
characteristics[i].readValue()
.then(value => {
log('> #' + (i+1) + ' Characteristic value: ' + asciiToDecimal(characteristics[i].value));
});
}
} catch(err) {
})
.catch(err => {
log(err);
}
});
}
populate(testCases);

View file

@ -34,30 +34,38 @@
function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]});
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]})
.then(device => {
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
log('Getting Primary Service "heart_rate"...');
var primaryService = server.getPrimaryService('heart_rate');
return server.getPrimaryService('heart_rate');
})
.then(service => {
log('Getting Characteristic "heart_rate_measurement"...');
var characteristic = primaryService.getCharacteristic('heart_rate_measurement');
return service.getCharacteristic('heart_rate_measurement');
})
.then(characteristic => {
log('Getting Descriptor "' + testCases[testNumber] + '"...');
var descriptor = characteristic.getDescriptor(testCases[testNumber]);
return characteristic.getDescriptor(testCases[testNumber]);
})
.then(descriptor => {
log('Descriptor found!');
log('> Descriptor characteristic: ' + descriptor.characteristic.uuid);
log('> Descriptor UUID: ' + descriptor.uuid);
descriptor.readValue();
log('> Descriptor value: ' + asciiToDecimal(descriptor.value));
} catch(err) {
return descriptor.readValue();
})
.then(value => {
log('> Descriptor value: ' + asciiToDecimal(value));
})
.catch(err => {
log(err);
}
});
}
populate(testCases);

View file

@ -35,31 +35,39 @@
function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]});
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]})
.then(device => {
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
log('Getting Primary Service "heart_rate"...');
var primaryService = server.getPrimaryService('heart_rate');
return server.getPrimaryService('heart_rate');
})
.then(service => {
log('Getting Characteristic "heart_rate_measurement"...');
var characteristic = primaryService.getCharacteristic('heart_rate_measurement');
return service.getCharacteristic('heart_rate_measurement');
})
.then(characteristic => {
log('Getting Descriptors "' + testCases[testNumber] + '"...');
var descriptors = characteristic.getDescriptors(testCases[testNumber]);
return characteristic.getDescriptors(testCases[testNumber]);
})
.then(descriptors => {
for(i = 0; i < descriptors.length; ++i) {
log('> #' + (i+1));
log('> UUID: ' + descriptors[i].uuid);
descriptors[i].readValue();
log('> Descriptor value: ' + asciiToDecimal(descriptors[i].value));
descriptors[i].readValue()
.then(value => {
log('> #' + (i+1)) + 'Descriptor value: ' + asciiToDecimal(value);
});
}
} catch(err) {
})
.catch(err => {
log(err);
}
});
}
populate(testCases);

View file

@ -37,27 +37,32 @@
testCases.push({ requestedService: 'battery_service', requestedIncludedService: 0xf000ffc0, options: {filters: [{services: ['battery_service']}]} });
//Test 15
testCases.push({ requestedService: 'battery_service', requestedIncludedService: 0x00001530, options: {filters: [{services: ['battery_service']}]} });
function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice(testCases[testNumber].options);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(testCases[testNumber].options)
.then(device => {
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
log('Getting Primary Service "' + testCases[testNumber].requestedService + '"...');
var primaryService = server.getPrimaryService(testCases[testNumber].requestedService);
return server.getPrimaryService(testCases[testNumber].requestedService);
})
.then(service => {
log('Getting Included Service "' + testCases[testNumber].requestedIncludedService + '"...')
var includedService = primaryService.getIncludedService(testCases[testNumber].requestedIncludedService);
return service.getIncludedService(testCases[testNumber].requestedIncludedService);
})
.then(includedService => {
log('Primary Service found on device: ' + includedService.device.name);
log('> UUID: ' + includedService.uuid);
log('> Is primary: ' + includedService.isPrimary);
} catch(err) {
})
.catch(err => {
log(err);
}
});
}
populate(testCases);

View file

@ -37,29 +37,34 @@
testCases.push({ requestedService: 'battery_service', requestedIncludedService: 0xf000ffc0, options: {filters: [{services: ['battery_service']}]} });
//Test 15
testCases.push({ requestedService: 'battery_service', requestedIncludedService: 0x00001530, options: {filters: [{services: ['battery_service']}]} });
function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice(testCases[testNumber].options);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(testCases[testNumber].options)
.then(device => {
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
log('Getting Primary Service "' + testCases[testNumber].requestedService + '"...');
var primaryService = server.getPrimaryService(testCases[testNumber].requestedService);
return server.getPrimaryService(testCases[testNumber].requestedService);
})
.then(service => {
log('Getting Included Service "' + testCases[testNumber].requestedIncludedService + '"...')
var includedServices = primaryService.getIncludedServices(testCases[testNumber].requestedIncludedService);
return service.getIncludedServices(testCases[testNumber].requestedIncludedService);
})
.then(includedServices => {
for(i = 0; i < includedServices.length; ++i) {
log('> #' + (i+1));
log('> UUID: ' + includedServices[i].uuid);
log('> Is primary: ' + includedServices[i].isPrimary);
}
} catch(err) {
})
.catch(err => {
log(err);
}
});
}
populate(testCases);

View file

@ -50,22 +50,25 @@
function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice(testCases[testNumber].options);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(testCases[testNumber].options)
.then(device => {
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
log('Getting Primary Service "' + testCases[testNumber].requestedService + '"...');
var primaryService = server.getPrimaryService(testCases[testNumber].requestedService);
log('Primary Service found on device: ' + primaryService.device.name);
log('> UUID: ' + primaryService.uuid);
log('> Is primary: ' + primaryService.isPrimary);
} catch(err) {
return server.getPrimaryService(testCases[testNumber].requestedService);
})
.then(service => {
log('Primary Service found on device: ' + service.device.name);
log('> UUID: ' + service.uuid);
log('> Is primary: ' + service.isPrimary);
})
.catch(err => {
log(err);
}
});
}
populate(testCases);

View file

@ -47,26 +47,30 @@
testCases.push({ requestedService: 'cycling_power', options: {filters: [{services: ['battery_service']}], optionalServices: ['cycling_power']} });
//Test 20
testCases.push({ requestedService: '00001818-0000-1000-8000-00805f9b34fb', options: {filters: [{services: ['battery_service']}], optionalServices: ['cycling_power']} });
function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice(testCases[testNumber].options);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(testCases[testNumber].options)
.then(device => {
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
log('Getting Primary Service "' + testCases[testNumber].requestedService + '"...');
var primaryServices = server.getPrimaryServices(testCases[testNumber].requestedService);
for(i = 0; i < primaryServices.length; ++i) {
return server.getPrimaryServices(testCases[testNumber].requestedService);
})
.then(services => {
for(i = 0; i < services.length; ++i) {
log('> #' + (i+1));
log('> UUID: ' + primaryServices[i].uuid);
log('> Is primary: ' + primaryServices[i].isPrimary);
log('> UUID: ' + services[i].uuid);
log('> Is primary: ' + services[i].isPrimary);
}
} catch(err) {
})
.catch(err => {
log(err);
}
});
}
populate(testCases);

View file

@ -28,27 +28,30 @@
if (filterNamePrefix)
options.filters.push({namePrefix: filterNamePrefix});
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice(options);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(options)
.then(device => {
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
log('Getting Primary Service...');
var primaryService = server.getPrimaryService(filterService);
log('Primary Service found on device: ' + primaryService.device.name);
log('> UUID: ' + primaryService.uuid);
return server.getPrimaryService(filterService);
})
.then(service => {
log('Primary Service found on device: ' + service.device.name);
log('> UUID: ' + service.uuid);
log('Getting Included Services...');
var includedServices = primaryService.getIncludedServices();
return service.getIncludedServices();
})
.then(includedServices => {
log('> Included Services: ' +
includedServices.map(s => s.uuid).join('\n' + ' '.repeat(21)));
} catch(err) {
})
.catch(err => {
log(err);
}
});
}
</script>
</body>

View file

@ -28,22 +28,24 @@
if (filterNamePrefix)
options.filters.push({namePrefix: filterNamePrefix});
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice(options);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(options)
.then(device => {
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
log('Getting Primary Service...');
var primaryService = server.getPrimaryService(filterService);
log('Primary Service found on device: ' + primaryService.device.name);
log('> UUID: ' + primaryService.uuid);
log('> Is primary: ' + primaryService.isPrimary);
} catch(err) {
return server.getPrimaryService(filterService);
})
.then(service => {
log('Primary Service found on device: ' + service.device.name);
log('> UUID: ' + service.uuid);
log('> Is primary: ' + service.isPrimary);
})
.catch(err => {
log(err);
}
});
}
</script>
</body>

View file

@ -28,29 +28,30 @@
if (filterNamePrefix)
options.filters.push({namePrefix: filterNamePrefix});
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice(options);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(options)
.then(device => {
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
return device.gatt.connect();
})
.then(server => {
log('Getting Primary Service...');
var primaryServices;
if (filterService) {
primaryServices = server.getPrimaryServices(filterService);
} else {
primaryServices = server.getPrimaryServices();
}
if (filterService)
return server.getPrimaryServices(filterService);
else
return server.getPrimaryServices();
})
.then(services => {
log('> List of Services on the current device:');
for(i = 0; i < primaryServices.length; ++i) {
for(i = 0; i < services.length; ++i) {
log('> #' + (i+1));
log('> UUID: ' + primaryServices[i].uuid);
log('> Is primary: ' + primaryServices[i].isPrimary);
log('> UUID: ' + services[i].uuid);
log('> Is primary: ' + services[i].isPrimary);
}
} catch(err) {
})
.catch(err => {
log(err);
}
});
}
</script>
</body>

View file

@ -52,19 +52,20 @@
function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice(testCases[testNumber]);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(testCases[testNumber])
.then(device => {
log('Found a device!');
log('> Name: ' + device.name);
log('> Id: ' + device.id);
log('> Appearance: ' + device.adData.appearance);
log('> Tx Power: ' + device.adData.txPower + ' dBm');
log('> RSSI: ' + device.adData.rssi + ' dBm');
} catch(err) {
})
.catch(err => {
log(err);
}
});
}
populate(testCases);