微信小程序 從服務(wù)中獲取 bin文件 , 進(jìn)行切片,然后發(fā)送到藍(lán)牙中

步驟

1,從服務(wù)器獲取 .bin 文件:使用 wx.request 方法從服務(wù)器下載文件。
2,將文件內(nèi)容切片:將獲取的文件內(nèi)容切割成多個(gè)小塊。
3,連接到藍(lán)牙設(shè)備:確保已連接到目標(biāo)藍(lán)牙設(shè)備。
4,循環(huán)發(fā)送數(shù)據(jù)塊:使用 wx.writeBLECharacteristicValue 方法將每個(gè)數(shù)據(jù)塊發(fā)送到藍(lán)牙設(shè)備。

// 切割文件的函數(shù)  
function splitFile(arrayBuffer, chunkSize) {  
    const chunks = [];  
    let offset = 0;  

    while (offset < arrayBuffer.byteLength) {  
        const chunk = arrayBuffer.slice(offset, offset + chunkSize);  
        chunks.push(chunk);  
        offset += chunkSize;  
    }  

    return chunks;  
}  

// 從服務(wù)器獲取 .bin 文件  
function fetchBinFile(url, deviceId, serviceId, characteristicId) {  
    wx.request({  
        url: url,  
        method: 'GET',  
        responseType: 'arraybuffer', // 設(shè)置響應(yīng)類型為 arraybuffer  
        success: function (res) {  
            if (res.statusCode === 200) {  
                const arrayBuffer = res.data; // 獲取文件內(nèi)容  
                const chunkSize = 20; // 設(shè)置每個(gè)塊的大小(例如:20字節(jié))  
                const chunks = splitFile(arrayBuffer, chunkSize);  
                sendChunksToBluetooth(deviceId, serviceId, characteristicId, chunks);  
            } else {  
                console.error('獲取文件失敗', res);  
            }  
        },  
        fail: function (err) {  
            console.error('請(qǐng)求失敗', err);  
        }  
    });  
}  

// 循環(huán)發(fā)送數(shù)據(jù)塊到藍(lán)牙設(shè)備  
function sendChunksToBluetooth(deviceId, serviceId, characteristicId, chunks) {  
    let index = 0;  

    function sendNextChunk() {  
        if (index < chunks.length) {  
            wx.writeBLECharacteristicValue({  
                deviceId: deviceId,  
                serviceId: serviceId,  
                characteristicId: characteristicId,  
                value: chunks[index], // 傳遞當(dāng)前塊  
                success: function (res) {  
                    console.log(`塊 ${index + 1} 發(fā)送成功`, res);  
                    index++;  
                    sendNextChunk(); // 發(fā)送下一個(gè)塊  
                },  
                fail: function (err) {  
                    console.error(`塊 ${index + 1} 發(fā)送失敗`, err);  
                }  
            });  
        } else {  
            console.log('所有數(shù)據(jù)塊已發(fā)送');  
        }  
    }  

    sendNextChunk(); // 開(kāi)始發(fā)送第一個(gè)塊  
}  

// 示例調(diào)用  
const url = 'https://example.com/path/to/your/file.bin'; // 替換為你的文件 URL  
const deviceId = 'your-device-id'; // 替換為你的設(shè)備 ID  
const serviceId = 'your-service-id'; // 替換為你的服務(wù) ID  
const characteristicId = 'your-characteristic-id'; // 替換為你的特征 ID  

// 從服務(wù)器獲取文件并發(fā)送到藍(lán)牙  
fetchBinFile(url, deviceId, serviceId, characteristicId);

1,切割文件:splitFile 函數(shù)將 ArrayBuffer 切割成指定大小的塊,并返回一個(gè)包含所有塊的數(shù)組。

2,獲取文件:使用 wx.request 方法從服務(wù)器獲取 .bin 文件,設(shè)置 responseType 為 arraybuffer 以獲取二進(jìn)制數(shù)據(jù)。

3,發(fā)送數(shù)據(jù)塊:sendChunksToBluetooth 函數(shù)循環(huán)發(fā)送每個(gè)數(shù)據(jù)塊到藍(lán)牙設(shè)備。使用遞歸調(diào)用 sendNextChunk 方法,確保每個(gè)塊在前一個(gè)塊成功發(fā)送后發(fā)送。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。