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
41
42
43
44
45
46
47
48
49
50
use std::collections::HashMap;
#[cfg(target_os = "windows")]
use windows::core::Error;
#[cfg(target_os = "windows")]
use windows::Devices::Bluetooth::GenericAttributeProfile::GattCharacteristic;
use crate::device::{Characteristic, Device};
#[cfg(target_os = "macos")]
use crate::mac::error::Error;


#[cfg(target_os = "macos")]
pub mod mac;
#[cfg(target_os = "windows")]
pub mod win;
pub mod device;
pub fn run<F>(factory: F)
where
    F: Fn() -> Box<dyn Handler>,
{
    #[cfg(target_os = "windows")]
    win::Win::new().run(factory);
    #[cfg(target_os = "macos")]
    mac::Mac::new().run(factory);
}


pub trait Handler {
    /// 蓝牙适配器开启
    fn on_adapter_open(&mut self);
    /// 蓝牙适配器关闭
    fn on_adapter_close(&mut self);
    /// 发现设备
    fn on_discover(&mut self, device: Device);
    /// 获取设备列表
    fn get_device_list(&mut self) ->HashMap<String,Device>;
    /// 连接成功通知
    fn on_connect(&mut self, uuid: String);
    /// 未连接通知
    fn on_unconnect(&mut self, uuid: String);
    /// 设备关闭
    /// * uuid 设备uuid
    fn on_disconnect(&mut self, uuid: String);
    /// 特征发现通知
    fn on_characteristics(&mut self, uuid: String, characteristic: Characteristic);
    /// 开始监听具体接口
    fn listen(&mut self, uuid: String);

    /// 接收具体接口数据
    fn on_data(&mut self, uuid: String, characteristic_uuid: String, data: Vec<u8>);
}