Skip to content

Commit

Permalink
Add discovery session
Browse files Browse the repository at this point in the history
  • Loading branch information
dati91 committed May 26, 2016
1 parent 2572a7d commit 89f26f4
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions components/net/bluetooth_thread.rs
Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -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 {
Expand Down Expand Up @@ -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<BluetoothDevice> = devices.into_iter()
.filter(|d| matches_filters(d, options.get_filters()))
.collect();
Expand Down

0 comments on commit 89f26f4

Please sign in to comment.