簡體   English   中英

無法使用UWP API將大字節數組寫入BLE設備-例如,將值寫入為異步

[英]Cannot write large byte array to a BLE device using UWP APIs - e.g. Write value as Async

我正在使用Windows 10內部版本14393.2156。 藍牙適配器的LMP版本是6.X(藍牙版本4.0)。 我無法寫入長度為350的字節數組數據。但是,我可以寫入長度為60左右的字節數組數據,並從BLE設備獲得所需的數據。 當我寫大長度的字節數組(例如350)時,出現Windows異常:“異常:指定的服務器無法執行請求的操作。(HRESULT異常:0x8007003A)”。 以下是代碼:

private async Task CoreWrite(byte[] data)
    {
        var writeBuffer = CryptographicBuffer.CreateFromByteArray(data);
        var result = await _txCharacteristic.WriteValueAsync(writeBuffer);
        if (result != GattCommunicationStatus.Success)
        {
            throw new IOException($"Failed to write to bluetooth device. Status: {nameof(result)}");
        }
    }

請注意,設備已經配對。 是否有任何有效負載限制可能會影響藍牙4.0和4.2規范中有效負載長度的限制。 或者,您建議更高版本的Windows 10和更新的Bluetooth LMP 8.X應該有助於解決該問題。 感謝任何建議或幫助。

非常感謝。

令人驚訝地,我們發現該特征的屬性數據長度被限制為244個字節。 因此,我無法寫入超過244個字節的任何數據。 但是,一次執行244個字節的多次寫入確實可以解決此問題。 我可以看到BLE設備的預期響應。

例:

int offset = 0;
int AttributeDataLen = 244;
while (offset < data.Length)
{
   int length = data.Length - offset;
   if (length > AttributeDataLen)
   {
      length = AttributeDataLen;
   }

   byte[] subset = new byte[length];

   Array.Copy(data, offset, subset, 0, length);
   offset += length;

   var writeBuffer = CryptographicBuffer.CreateFromByteArray(subset);
   var result = await _txCharacteristic.WriteValueAsync(writeBuffer);
   if (result != GattCommunicationStatus.Success)
   {
      throw new IOException(
        $"Failed to write to bluetooth device. Status: {nameof(result)}");
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM