mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
These are the fixes thus far. Most involve whether we're using a pointer or not.
PiperOrigin-RevId: 401241518
This commit is contained in:
committed by
Copybara-Service
parent
dceb1e216b
commit
eea2a91139
@@ -60,7 +60,9 @@ void BluetoothClassicMedium::OnScanModeChanged(
|
||||
bool radioDiscoverable = bluetooth_adapter_.GetScanMode() ==
|
||||
BluetoothAdapter::ScanMode::kConnectableDiscoverable;
|
||||
|
||||
bluetooth_server_socket_->SetScanMode(radioDiscoverable);
|
||||
if (bluetooth_server_socket_ != nullptr) {
|
||||
bluetooth_server_socket_->SetScanMode(radioDiscoverable);
|
||||
}
|
||||
}
|
||||
|
||||
bool BluetoothClassicMedium::StartDiscovery(
|
||||
@@ -251,14 +253,14 @@ bool BluetoothClassicMedium::CheckSdp(RfcommDeviceService requestedService) {
|
||||
// Do various checks of the SDP record to make sure you are talking to a
|
||||
// device that actually supports the Bluetooth Rfcomm Service
|
||||
// https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.rfcomm.rfcommdeviceservice.getsdprawattributesasync?view=winrt-20348
|
||||
auto attributes = /*await*/ requestedService.GetSdpRawAttributesAsync();
|
||||
if (!attributes.get().HasKey(Constants::SdpServiceNameAttributeId)) {
|
||||
auto attributes = requestedService.GetSdpRawAttributesAsync().get();
|
||||
if (!attributes.HasKey(Constants::SdpServiceNameAttributeId)) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Missing SdpServiceNameAttributeId.";
|
||||
return false;
|
||||
}
|
||||
|
||||
auto attributeReader = DataReader::FromBuffer(
|
||||
attributes.get().Lookup(Constants::SdpServiceNameAttributeId));
|
||||
attributes.Lookup(Constants::SdpServiceNameAttributeId));
|
||||
|
||||
auto attributeType = attributeReader.ReadByte();
|
||||
|
||||
@@ -374,7 +376,7 @@ winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Added(
|
||||
EnterCriticalSection(&critical_section_);
|
||||
|
||||
std::unique_ptr<BluetoothDevice> bluetoothDevice =
|
||||
std::make_unique<BluetoothDevice>(async.GetResults());
|
||||
std::make_unique<BluetoothDevice>(async.get());
|
||||
|
||||
devices_by_id_[deviceInfo.Id()] = std::move(bluetoothDevice);
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
|
||||
BluetoothServerSocket::BluetoothServerSocket(const std::string service_name,
|
||||
const std::string service_uuid)
|
||||
: radio_discoverable_(false),
|
||||
@@ -104,15 +105,16 @@ Exception BluetoothServerSocket::StartListening(bool radioDiscoverable) {
|
||||
RfcommServiceId::FromUuid(winrt::guid(service_uuid_)))
|
||||
.get();
|
||||
|
||||
rfcomm_provider_ = &rfcommProviderRef;
|
||||
rfcomm_provider_ = rfcommProviderRef;
|
||||
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
|
||||
stream_socket_listener_
|
||||
.BindServiceNameAsync(winrt::to_hstring(service_name_.c_str()))
|
||||
.BindServiceNameAsync(
|
||||
winrt::to_hstring(rfcomm_provider_.ServiceId().AsString()),
|
||||
SocketProtectionLevel::PlainSocket)
|
||||
.get();
|
||||
|
||||
// Set the SDP attributes and start Bluetooth advertising
|
||||
InitializeServiceSdpAttributes(*rfcomm_provider_, service_name_);
|
||||
InitializeServiceSdpAttributes(rfcomm_provider_, service_name_);
|
||||
} catch (std::exception exception) {
|
||||
// We will log and eat the exception since the caller
|
||||
// expects nullptr if it fails
|
||||
@@ -133,9 +135,8 @@ Exception BluetoothServerSocket::StartListening(bool radioDiscoverable) {
|
||||
|
||||
Exception BluetoothServerSocket::StartAdvertising() {
|
||||
try {
|
||||
rfcomm_provider_->StartAdvertising(
|
||||
stream_socket_listener_.as<StreamSocketListener>(),
|
||||
radio_discoverable_);
|
||||
rfcomm_provider_.StartAdvertising(stream_socket_listener_,
|
||||
radio_discoverable_);
|
||||
} catch (std::exception exception) {
|
||||
// We will log and eat the exception since the caller
|
||||
// expects nullptr if it fails
|
||||
@@ -151,7 +152,7 @@ Exception BluetoothServerSocket::StartAdvertising() {
|
||||
}
|
||||
|
||||
void BluetoothServerSocket::StopAdvertising() {
|
||||
rfcomm_provider_->StopAdvertising();
|
||||
rfcomm_provider_.StopAdvertising();
|
||||
}
|
||||
|
||||
void BluetoothServerSocket::InitializeServiceSdpAttributes(
|
||||
@@ -180,7 +181,7 @@ Exception BluetoothServerSocket::Close() {
|
||||
EnterCriticalSection(&critical_section_);
|
||||
closed_ = true;
|
||||
bluetooth_sockets_ = {};
|
||||
rfcomm_provider_->StopAdvertising();
|
||||
rfcomm_provider_.StopAdvertising();
|
||||
LeaveCriticalSection(&critical_section_);
|
||||
|
||||
return {Exception::kSuccess};
|
||||
|
||||
@@ -64,6 +64,10 @@ using winrt::Windows::Storage::Streams::DataWriter;
|
||||
// https://docs.microsoft.com/en-us/uwp/api/windows.storage.streams.unicodeencoding?view=winrt-20348
|
||||
using winrt::Windows::Storage::Streams::UnicodeEncoding;
|
||||
|
||||
// Specifies the level of encryption to use on a StreamSocket object.
|
||||
// https://docs.microsoft.com/en-us/uwp/api/windows.networking.sockets.socketprotectionlevel?view=winrt-22000
|
||||
using winrt::Windows::Networking::Sockets::SocketProtectionLevel;
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html.
|
||||
class BluetoothServerSocket : public api::BluetoothServerSocket {
|
||||
public:
|
||||
@@ -90,8 +94,8 @@ class BluetoothServerSocket : public api::BluetoothServerSocket {
|
||||
Exception StartListening(bool radioDiscoverable);
|
||||
|
||||
void SetScanMode(bool radioDiscoverable) {
|
||||
radio_discoverable_ = radioDiscoverable;
|
||||
StopAdvertising();
|
||||
radio_discoverable_ = radioDiscoverable;
|
||||
StartAdvertising();
|
||||
}
|
||||
|
||||
@@ -116,7 +120,7 @@ class BluetoothServerSocket : public api::BluetoothServerSocket {
|
||||
const std::string service_name_;
|
||||
const std::string service_uuid_;
|
||||
|
||||
RfcommServiceProvider* rfcomm_provider_;
|
||||
RfcommServiceProvider rfcomm_provider_;
|
||||
};
|
||||
|
||||
} // namespace windows
|
||||
|
||||
@@ -19,8 +19,13 @@
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
BluetoothSocket::BluetoothSocket() {
|
||||
windows_socket_ = IStreamSocket();
|
||||
|
||||
BluetoothSocket::BluetoothSocket(StreamSocket streamSocket)
|
||||
: windows_socket_(streamSocket) {
|
||||
bluetooth_device_ = std::make_unique<BluetoothDevice>(
|
||||
winrt::Windows::Devices::Bluetooth::BluetoothDevice::FromHostNameAsync(
|
||||
windows_socket_.Information().RemoteHostName())
|
||||
.get());
|
||||
input_stream_ =
|
||||
std::make_unique<BluetoothInputStream>(windows_socket_.InputStream());
|
||||
output_stream_ =
|
||||
@@ -59,8 +64,9 @@ Exception BluetoothSocket::Close() {
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothSocket.html#getRemoteDevice()
|
||||
// Returns valid BluetoothDevice pointer if there is a connection, and
|
||||
// nullptr otherwise.
|
||||
// TODO(b/184975123): replace with real implementation.
|
||||
api::BluetoothDevice* BluetoothSocket::GetRemoteDevice() { return nullptr; }
|
||||
api::BluetoothDevice* BluetoothSocket::GetRemoteDevice() {
|
||||
return bluetooth_device_.get();
|
||||
}
|
||||
|
||||
// Starts an asynchronous operation on a StreamSocket object to connect to a
|
||||
// remote network destination specified by a remote hostname and a remote
|
||||
@@ -81,7 +87,7 @@ ExceptionOr<ByteArray> BluetoothSocket::BluetoothInputStream::Read(
|
||||
std::int64_t size) {
|
||||
Buffer buffer = Buffer(size);
|
||||
|
||||
winrt_stream_.ReadAsync(buffer, size, InputStreamOptions::None);
|
||||
winrt_stream_.ReadAsync(buffer, size, InputStreamOptions::None).get();
|
||||
|
||||
DataReader dataReader = DataReader::FromBuffer(buffer);
|
||||
|
||||
@@ -93,7 +99,7 @@ ExceptionOr<ByteArray> BluetoothSocket::BluetoothInputStream::Read(
|
||||
IAsyncAction BluetoothSocket::CancelIOAsync() {
|
||||
// Cancels pending reads and writes over a StreamSocket object.
|
||||
// https://docs.microsoft.com/en-us/uwp/api/windows.networking.sockets.streamsocket.cancelioasync?view=winrt-20348
|
||||
return windows_socket_.as<StreamSocket>().CancelIOAsync();
|
||||
return windows_socket_.CancelIOAsync();
|
||||
}
|
||||
|
||||
Exception BluetoothSocket::BluetoothInputStream::Close() {
|
||||
@@ -117,7 +123,7 @@ Exception BluetoothSocket::BluetoothOutputStream::Write(const ByteArray& data) {
|
||||
std::memcpy(buffer.data(), data.data(), data.size());
|
||||
|
||||
try {
|
||||
winrt_stream_.WriteAsync(buffer);
|
||||
winrt_stream_.WriteAsync(buffer).get();
|
||||
} catch (std::exception exception) {
|
||||
return {Exception::kFailed};
|
||||
}
|
||||
|
||||
@@ -66,9 +66,7 @@ class BluetoothSocket : public api::BluetoothSocket {
|
||||
public:
|
||||
BluetoothSocket();
|
||||
|
||||
BluetoothSocket(
|
||||
winrt::Windows::Networking::Sockets::StreamSocket streamSocket)
|
||||
: windows_socket_(streamSocket) {}
|
||||
BluetoothSocket(StreamSocket streamSocket);
|
||||
|
||||
~BluetoothSocket() override;
|
||||
|
||||
@@ -130,7 +128,8 @@ class BluetoothSocket : public api::BluetoothSocket {
|
||||
std::unique_ptr<BluetoothInputStream> input_stream_;
|
||||
std::unique_ptr<BluetoothOutputStream> output_stream_;
|
||||
|
||||
IStreamSocket windows_socket_;
|
||||
StreamSocket windows_socket_;
|
||||
std::unique_ptr<BluetoothDevice> bluetooth_device_;
|
||||
};
|
||||
|
||||
} // namespace windows
|
||||
|
||||
Reference in New Issue
Block a user