diff --git a/components/net/bluetooth_thread.rs b/components/net/bluetooth_thread.rs index c8a6e4af31ed..7baaf7da6bca 100644 --- a/components/net/bluetooth_thread.rs +++ b/components/net/bluetooth_thread.rs @@ -4,6 +4,7 @@ use device::bluetooth::BluetoothAdapter; use device::bluetooth::BluetoothDevice; +use device::bluetooth::BluetoothDiscoverySession; use device::bluetooth::BluetoothGATTCharacteristic; use device::bluetooth::BluetoothGATTDescriptor; use device::bluetooth::BluetoothGATTService; @@ -16,6 +17,8 @@ use net_traits::bluetooth_thread::{BluetoothResult, BluetoothServiceMsg, Bluetoo use std::borrow::ToOwned; use std::collections::HashMap; use std::string::String; +use std::thread; +use std::time::Duration; use util::thread::spawn_named; const ADAPTER_ERROR: &'static str = "No adapter found"; @@ -25,6 +28,8 @@ const PRIMARY_SERVICE_ERROR: &'static str = "No primary service found"; const CHARACTERISTIC_ERROR: &'static str = "No characteristic found"; const DESCRIPTOR_ERROR: &'static str = "No descriptor found"; const VALUE_ERROR: &'static str = "No characteristic or descriptor found with that id"; +// The discovery session needs some time to find any nearby devices +const DISCOVERY_TIMEOUT_MS: u64 = 1500; bitflags! { flags Flags: u32 { @@ -355,11 +360,13 @@ impl BluetoothManager { Some(a) => a, None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))), }; - let devices = self.get_and_cache_devices(&mut adapter); - if devices.is_empty() { - return drop(sender.send(Err(String::from(DEVICE_ERROR)))); + if let Some(ref session) = BluetoothDiscoverySession::create_session(adapter.get_object_path()).ok() { + if session.start_discovery().is_ok() { + thread::sleep(Duration::from_millis(DISCOVERY_TIMEOUT_MS)); + } + let _ = session.stop_discovery(); } - + let devices = self.get_and_cache_devices(&mut adapter); let matched_devices: Vec = devices.into_iter() .filter(|d| matches_filters(d, options.get_filters())) .collect();