Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions cancel_gatt_connect/device_code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

function onInit() {
// Put into a known state.
digitalWrite(LED, 0);

NRF.setServices({
'0b30acec-193e-11eb-adc1-0242ac120002': {
'0b30afd0-193e-11eb-adc1-0242ac120002': {
value: [17],
broadcast: false,
readable: true,
writable: false,
notify: false,
description: 'Single read-only characteristic',
}
}
});


NRF.on('disconnect', (reason) => {
// Provide feedback that device is no longer connected.
digitalWrite(LED, 0);
});

NRF.on('connect', (addr) => {
// Provide feedback that device is connected.
// TODO: Maybe only do this for some devices when external power is
// available. For example, this will turn on the backlight on the
// Pixl.js screen, which might not be desirable when powered by the
// CR2032 coin cell battery.
digitalWrite(LED, 1);
});
}
91 changes: 91 additions & 0 deletions cancel_gatt_connect/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<!DOCTYPE html>
<!--
Copyright 2025 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<html>

<head>
<meta charset="UTF-8">
<title>Cancel GATT connect</title>
<script src="../shared/shared.js"></script>
<script src="web_app.js"></script>
<link rel="stylesheet" href="../css/main.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body onload="init()">
<h1>Cancel GATT connect</h1>
<p>This is a simple test to check if gatt.connect() can be cancelled by gatt.disconnect().
</p>
<div id="bluetooth_available" style="visibility: visible;">
<h2>Step 1</h2>
<div class="container">
<div class="flex">
<div class="inRow">
Press the button to load the code to the Espruino IDE.
From there, flash any Bluetooth capable Espruino device.
<br>
<br>
View <a href="device_code.js" title="Device source code">source</a>.
</div>
<div class="inRow">
<button id="btn_load_code" type="button" onclick="loadEspruinoDeviceCode()">Load Device Code</button>
</div>
</div>
</div>

<h2>Step 2</h2>
<div>
<div class="flex">
<div class="inRow">
Press the button to start pairing.
</div>
<div class="inRow">
<button id="btn_start_pairing" type="button" onclick="startPairing()">Start Pairing</button>
</div>
</div>
</div>

<h2>Step 3</h2>
<div>
<div class="flex">
<div class="inRow">
Unplug the Espruino Pixl.js from power source and click the button.
</div>
<div class="inRow">
<button id="btn_connect_then_cancel" type="button" onclick="connectThenCancel()">Start connecting then cancel</button>
</div>
</div>
</div>

<p>Status: <span id='test_result'></span></p>
<pre id='status'></pre>
</div>
<!-- Only one of the three below will be visible. -->
<div id="bluetooth_none" class="no-bluetooth" style="visibility: hidden;">
Web Bluetooth is not supported by this browser.
</div>
<div id="bluetooth_insecure" class="no-bluetooth" style="visibility: hidden;">
Bluetooth requires a secure context (HTTPS). For development purposes it
does support HTTP, but only the on the loopback adapter (i.e. "localhost").
</div>
<div id="bluetooth_unavailable" class="no-bluetooth" style="visibility: hidden;">
Web Bluetooth is supported by this browser but this device does not have
Bluetooth capabilities.
</div>
</body>

</html>
100 changes: 100 additions & 0 deletions cancel_gatt_connect/web_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Espruino devices publish a UART service by default.
const nordicUARTService = '6e400001-b5a3-f393-e0a9-e50e24dcca9e';

let device = undefined;

/**
* Load the device code to the Espruino IDE.
*/
function loadEspruinoDeviceCode() {
fetch('device_code.js').then(response => response.text()).then(data => {
let url = 'http://www.espruino.com/webide?code=' + encodeURIComponent(data);
window.open(url, '_window');
});
}

async function startPairing() {
clearStatus();
logInfo('Starting test');

$('btn_load_code').disabled = true;
$('btn_start_pairing').disabled = true;
$('btn_connect_then_cancel').disabled = true;

try {
const options = {
filters: [
{ services: [nordicUARTService] }
],
};

device = await navigator.bluetooth.requestDevice(options);
logInfo(`Paired to Bluetooh device with name: ${device.name}`);
} catch (error) {
logError(`Unexpected failure: ${error}`);
}

$('btn_load_code').disabled = false;
$('btn_start_pairing').disabled = false;
$('btn_connect_then_cancel').disabled = false;
}

async function connectThenCancel() {
$('btn_load_code').disabled = true;
$('btn_start_pairing').disabled = true;
$('btn_connect_then_cancel').disabled = true;

logInfo('Try to connect then cancel');
try {
setTimeout(() => {
device.gatt.disconnect();
}, 100);
await device.gatt.connect();
logError('connect() promise was not rejected');
} catch (e) {
if (e.name !== 'AbortError') {
logError(`connect() promise was rejected with unexpected error: ${e}`);
}
logInfo('connect() promise was rejected with Abort error');
}

$('btn_load_code').disabled = false;
$('btn_start_pairing').disabled = false;
$('btn_connect_then_cancel').disabled = false;
testDone();
}

async function init() {
if (!isBluetoothSupported()) {
console.log('Bluetooth not supported.');
$('bluetooth_available').style.display = 'none';
if (window.isSecureContext) {
$('bluetooth_none').style.visibility = 'visible';
} else {
$('bluetooth_insecure').style.visibility = 'visible';
}
return;
}

const available = await navigator.bluetooth.getAvailability();
if (!available) {
$('bluetooth_available').style.display = 'none';
$('bluetooth_unavailable').style.visibility = 'visible';
}
}
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ <h2>Tests</h2>
<li><a href="get_devices">Test navigator.bluetooth.getDevices</a></li>
<li><a href="watch_advertisements">Test BluetoothDevice.watchAdvertisements</a></li>
<li><a href="forget_device">Test BluetoothDevice.forget</a></li>
<li><a href="cancel_gatt_connect">Test cancelling a GATT connection</a></li>
</ol>
</body>

Expand Down