1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use fake_adapter::FakeBluetoothAdapter;
use std::error::Error;
use std::sync::Arc;

#[derive(Debug)]
pub struct FakeBluetoothDiscoverySession {
    adapter: Arc<FakeBluetoothAdapter>,
}

impl FakeBluetoothDiscoverySession {
    pub fn create_session(adapter: Arc<FakeBluetoothAdapter>) -> Result<FakeBluetoothDiscoverySession, Box<Error>> {
        Ok(FakeBluetoothDiscoverySession::new(adapter))
    }

    fn new(adapter: Arc<FakeBluetoothAdapter>) -> FakeBluetoothDiscoverySession {
        FakeBluetoothDiscoverySession {
            adapter: adapter,
        }
    }

    pub fn get_adapter(&self) -> Arc<FakeBluetoothAdapter> {
        self.adapter.clone()
    }

    pub fn start_discovery(&self) -> Result<(), Box<Error>> {
        match self.adapter.get_can_start_discovery() {
            Ok(false) => Err(Box::from("Failed to start discovery session")),
            Ok(true) => Ok(()),
            Err(err) => Err(err),
        }
    }

    pub fn stop_discovery(&self) -> Result<(), Box<Error>> {
        match self.adapter.get_can_stop_discovery() {
            Ok(false) => Err(Box::from("Failed to stop discovery session")),
            Ok(true) => Ok(()),
            Err(err) => Err(err),
        }
    }
}