diff --git a/internal/platform/implementation/windows/bluetooth_classic_socket.cc b/internal/platform/implementation/windows/bluetooth_classic_socket.cc index 71250a57..7a89d01d 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_socket.cc +++ b/internal/platform/implementation/windows/bluetooth_classic_socket.cc @@ -156,12 +156,18 @@ BluetoothSocket::BluetoothInputStream::BluetoothInputStream( ExceptionOr BluetoothSocket::BluetoothInputStream::Read( std::int64_t size) { try { - if (size <= 0 || size > kMaxTransmitPacketSize) { + if (size <= 0) { NEARBY_LOGS(ERROR) << __func__ << ": Invalid transmit packet size: " << size; return {Exception::kIo}; } + if (size > read_buffer_.Capacity()) { + NEARBY_LOGS(WARNING) << __func__ + << ": resize receive buffer to packet size: " << size; + read_buffer_ = Buffer(size); + } + // Init the read buffer. read_buffer_.Length(0); @@ -223,10 +229,11 @@ BluetoothSocket::BluetoothOutputStream::BluetoothOutputStream( Exception BluetoothSocket::BluetoothOutputStream::Write(const ByteArray& data) { try { - if (data.size() > kMaxTransmitPacketSize) { - NEARBY_LOGS(ERROR) << __func__ << ": Transmit packet size " << data.size() - << " is too big."; - return {Exception::kIo}; + if (data.size() > write_buffer_.Capacity()) { + NEARBY_LOGS(WARNING) << __func__ + << ": resize write buffer to packet size: " + << data.size(); + write_buffer_ = Buffer(data.size()); } std::memcpy(write_buffer_.data(), data.data(), data.size()); diff --git a/internal/platform/implementation/windows/bluetooth_classic_socket.h b/internal/platform/implementation/windows/bluetooth_classic_socket.h index 786c2104..9fd92614 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_socket.h +++ b/internal/platform/implementation/windows/bluetooth_classic_socket.h @@ -101,7 +101,7 @@ class BluetoothSocket : public api::BluetoothSocket { IAsyncAction CancelIOAsync(); private: - static constexpr int kMaxTransmitPacketSize = 4096; + static constexpr int kInitialTransmitPacketSize = 4096; class BluetoothInputStream : public InputStream { public: @@ -113,7 +113,7 @@ class BluetoothSocket : public api::BluetoothSocket { private: IInputStream winrt_input_stream_{nullptr}; - Buffer read_buffer_{kMaxTransmitPacketSize}; + Buffer read_buffer_{kInitialTransmitPacketSize}; }; class BluetoothOutputStream : public OutputStream { @@ -128,11 +128,11 @@ class BluetoothSocket : public api::BluetoothSocket { private: IOutputStream winrt_output_stream_{nullptr}; - Buffer write_buffer_{kMaxTransmitPacketSize}; + Buffer write_buffer_{kInitialTransmitPacketSize}; }; bool InternalConnect(HostName connectionHostName, - winrt::hstring connectionServiceName); + winrt::hstring connectionServiceName); winrt::fire_and_forget Listener_ConnectionStatusChanged( winrt::Windows::Devices::Bluetooth::BluetoothDevice device,