Skip to content

Commit 519750a

Browse files
committed
Streamline BLE Connection Workflow
1 parent eab6969 commit 519750a

File tree

2 files changed

+9
-48
lines changed

2 files changed

+9
-48
lines changed

index.html

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,6 @@ <h1>Request Bluetooth Device</h1>
231231
</p>
232232
</div>
233233
</section>
234-
<section class="step">
235-
<div class="step-number"></div>
236-
<div class="step-content">
237-
<h1>Bond Device</h1>
238-
<p>Once you are connected, we need to prompt a bond. Without this CircuitPython boards with
239-
USB won't continue to advertise after a hard reset or powerloss. This button also loads
240-
code.py from the device so click it even if the device has been connected before.</p>
241-
<p>
242-
<button class="purple-button" id="promptBond">Bond Bluetooth Device</button>
243-
</p>
244-
</div>
245-
</section>
246234
</div>
247235
</div>
248236
<div class="popup-modal shadow connect-dialog closable" data-popup-modal="web-connect">

js/workflows/ble.js

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const bleNusCharTXUUID = 'adaf0003-4369-7263-7569-74507974686e';
1515

1616
const BYTES_PER_WRITE = 20;
1717

18-
let btnRequestBluetoothDevice, btnBond, btnReconnect;
18+
let btnRequestBluetoothDevice, btnReconnect;
1919

2020
class BLEWorkflow extends Workflow {
2121
constructor() {
@@ -31,10 +31,9 @@ class BLEWorkflow extends Workflow {
3131
this.partialWrites = true;
3232
this.type = CONNTYPE.Ble;
3333
this.buttonStates = [
34-
{reconnect: false, request: false, bond: false},
35-
{reconnect: false, request: true, bond: false},
36-
{reconnect: true, request: true, bond: false},
37-
{reconnect: false, request: false, bond: true},
34+
{reconnect: false, request: false},
35+
{reconnect: false, request: true},
36+
{reconnect: true, request: true},
3837
];
3938
}
4039

@@ -54,18 +53,15 @@ class BLEWorkflow extends Workflow {
5453
let p = this.connectDialog.open();
5554
let modal = this.connectDialog.getModal();
5655
btnRequestBluetoothDevice = modal.querySelector('#requestBluetoothDevice');
57-
btnBond = modal.querySelector('#promptBond');
5856
btnReconnect = modal.querySelector('#bleReconnect');
5957

6058
// Map the button states to the buttons
6159
this.connectButtons = {
6260
reconnect: btnReconnect,
63-
request: btnRequestBluetoothDevice,
64-
bond: btnBond
61+
request: btnRequestBluetoothDevice
6562
};
6663

6764
btnRequestBluetoothDevice.addEventListener('click', this.onRequestBluetoothDeviceButtonClick.bind(this));
68-
btnBond.addEventListener('click', this.onBond.bind(this));
6965
btnReconnect.addEventListener('click', this.reconnectButtonHandler.bind(this));
7066

7167
// Check if Web Bluetooth is available
@@ -154,11 +150,11 @@ class BLEWorkflow extends Workflow {
154150
abortController.abort();
155151
console.log('Connecting to GATT Server from "' + device.name + '"...');
156152
try {
157-
await device.gatt.connect();
153+
this.bleServer = await device.gatt.connect();
158154
} catch (error) {
159155
await this._showMessage("Failed to connect to device. Try forgetting device from OS bluetooth devices and try again.");
160156
}
161-
if (device.gatt.connected) {
157+
if (this.bleServer && this.bleServer.connected) {
162158
console.log('> Bluetooth device "' + device.name + ' connected.');
163159
await this.switchToDevice(device);
164160
} else {
@@ -188,9 +184,7 @@ class BLEWorkflow extends Workflow {
188184
let device = await this.requestDevice();
189185

190186
console.log('> Requested ' + device.name);
191-
await device.gatt.connect();
192-
193-
await this.switchToDevice(device);
187+
await this.connectToBluetoothDevice(device);
194188
/*}
195189
catch (error) {
196190
console.error(error);
@@ -204,7 +198,7 @@ class BLEWorkflow extends Workflow {
204198
this.bleDevice = device;
205199
this.bleDevice.removeEventListener("gattserverdisconnected", this.onDisconnected.bind(this));
206200
this.bleDevice.addEventListener("gattserverdisconnected", this.onDisconnected.bind(this));
207-
this.bleServer = this.bleDevice.gatt;
201+
//this.bleServer = this.bleDevice.gatt;
208202
console.log("connected", this.bleServer);
209203
let services;
210204

@@ -221,26 +215,11 @@ class BLEWorkflow extends Workflow {
221215
await this.fileHelper.bond();
222216
await this.connectToSerial();
223217

224-
// Enable/Disable UI buttons
225-
this.connectionStep(3);
226-
227218
await this.onConnected();
228219
this.connectDialog.close();
229220
await this.loadEditor();
230221
}
231222

232-
// Bond
233-
async onBond(e) {
234-
try {
235-
console.log("bond");
236-
await this.fileHelper.bond();
237-
console.log("bond done");
238-
} catch (e) {
239-
console.log(e, e.stack);
240-
}
241-
await this.loadEditor();
242-
}
243-
244223
async serialTransmit(msg) {
245224
if (this.rxCharacteristic) {
246225
let encoder = new TextEncoder();
@@ -278,12 +257,6 @@ class BLEWorkflow extends Workflow {
278257
await this.connectToBluetoothDevice(device);
279258
}
280259
}
281-
282-
// Do we have a connection now but still need to connect serial?
283-
if (this.bleDevice && !this.bleServer) {
284-
await this.showBusy(this.bleDevice.gatt.connect());
285-
this.switchToDevice(this.bleDevice);
286-
}
287260
}
288261

289262
updateConnected(connectionState) {

0 commit comments

Comments
 (0)