From 1c7de8b855072070a1ae932ec86f8f1ac52493da Mon Sep 17 00:00:00 2001 From: Li Zhineng Date: Tue, 10 Jun 2025 22:28:14 +0800 Subject: send wifi credentials command --- index.html | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'index.html') diff --git a/index.html b/index.html index b04539b..7090f31 100644 --- a/index.html +++ b/index.html @@ -41,6 +41,13 @@ // 2. Send the first bind command await sendBindCommand(writeChar) + await delay(500) + + // 3. Send Wi-Fi credentials + const ssid = '' + const password = '' + await sendWifiCredentialsCommand(writeChar, ssid, password) + await delay(500) } function handleDeviceResponse(event) { @@ -70,6 +77,56 @@ await sendRawData(writeChar, handshakePacket) } + async function sendWifiCredentialsCommand(writeChar, ssid, password) { + const encoder = new TextEncoder() + const ssidBytes = encoder.encode(ssid) + const passwordBytes = encoder.encode(password) + + const ssidLenSize = 1 + const ssidSize = ssidBytes.length + const passwordLenSize = 1 + const passwordSize = passwordBytes.length + const plaintextPayload = new Uint8Array(ssidLenSize + ssidSize + passwordLenSize + passwordSize) + + let offset = 0 + + plaintextPayload[offset] = ssidSize + offset++ + + plaintextPayload.set(ssidBytes, offset) + offset += ssidSize + + plaintextPayload[offset] = passwordSize + offset++ + + plaintextPayload.set(passwordBytes, offset) + + const packet1 = new Uint8Array([ + // Header (4 bytes) + 0x02, // Sequence Number: 2 + 0x12, // Packet Info: (Packet 1 of 2) + 0x00, // Command ID: 21, Enc-Flag: 0 (Big Endian) + 0x15, + ...plaintextPayload.slice(0, 16) + ]) + + const packet2 = new Uint8Array([ + // Header (4 bytes) + 0x03, // Sequence Number: 3 + 0x22, // Packet Info: (Packet 2 of 2) + 0x00, // Command ID: 21, Enc-Flag: 0 (Big Endian) + 0x15, + ...plaintextPayload.slice(16) + ]) + + console.log('Sending Wi-Fi credentials packet 1/2...') + await sendRawData(writeChar, packet1) + await delay(100) + + console.log('Sending Wi-Fi credentials packet 2/2...') + await sendRawData(writeChar, packet2) + } + async function sendRawData(characteristic, data) { const chunkSize = 20 for (let i = 0; i < data.length; i += chunkSize) { -- cgit v1.2.3