pub(crate) fn new(
        uuid: Uuid,
        local_name: Option<String>,
        manager: AdapterManager<Self>,
        event_receiver: Receiver<CBPeripheralEvent>,
        message_sender: Sender<CoreBluetoothMessage>,
    ) -> Self {
        // Since we're building the object, we have an active advertisement.
        // Build properties now.
        let properties = Arc::new(Mutex::from(PeripheralProperties {
            // Rumble required ONLY a BDAddr, not something you can get from
            // MacOS, so we make it up for now. This sucks.
            address: uuid_to_bdaddr(&uuid.to_string()),
            address_type: None,
            local_name,
            tx_power_level: None,
            manufacturer_data: HashMap::new(),
            service_data: HashMap::new(),
            services: Vec::new(),
            discovery_count: 1,
        }));
        let notification_senders = Arc::new(Mutex::new(Vec::new()));
        let ns_clone = notification_senders.clone();
        let p_clone = properties.clone();
        let m_clone = manager.clone();
        task::spawn(async move {
            let mut event_receiver = event_receiver;
            loop {
                match event_receiver.next().await {
                    Some(CBPeripheralEvent::Notification(uuid, data)) => {
                        util::send_notification(
                            &ns_clone,
                            &ValueNotification { uuid, value: data },
                        );
                    }
                    Some(CBPeripheralEvent::ManufacturerData(manufacturer_id, data)) => {
                        let mut properties = p_clone.lock().unwrap();
                        properties
                            .manufacturer_data
                            .insert(manufacturer_id, data.clone());
                        m_clone.emit(CentralEvent::ManufacturerDataAdvertisement {
                            address: properties.address,
                            manufacturer_data: properties.manufacturer_data.clone(),
                        });
                    }
                    Some(CBPeripheralEvent::ServiceData(service_data)) => {
                        let mut properties = p_clone.lock().unwrap();
                        properties.service_data.extend(service_data.clone());

                        m_clone.emit(CentralEvent::ServiceDataAdvertisement {
                            address: properties.address,
                            service_data,
                        });
                    }
                    Some(CBPeripheralEvent::Services(services)) => {
                        let mut properties = p_clone.lock().unwrap();
                        properties.services = services.clone();

                        m_clone.emit(CentralEvent::ServicesAdvertisement {
                            address: properties.address,
                            services,
                        });
                    }
                    Some(CBPeripheralEvent::Disconnected) => (),
                    None => {
                        error!("Event receiver died, breaking out of corebluetooth device loop.");
                        break;
                    }
                }
            }
        });
        Self {
            properties,
            manager,
            characteristics: Arc::new(Mutex::new(BTreeSet::new())),
            notification_senders,
            uuid,
            message_sender,
        }
    }
// This calls tokio::task::spawn, so it must be called from the context of a Tokio Runtime.
This calls tokio::task::spawn, so it must be called from the context of a Tokio Runtime.
This calls tokio :: task :: spawn so it must be called from the context of a Tokio Runtime .
pub(crate) fn new(
        uuid: Uuid,
        local_name: Option<String>,
        manager: AdapterManager<Self>,
        event_receiver: Receiver<CBPeripheralEvent>,
        message_sender: Sender<CoreBluetoothMessage>,
    ) -> Self {
        // Since we're building the object, we have an active advertisement.
        // Build properties now.
        let properties = Arc::new(Mutex::from(PeripheralProperties {
            // Rumble required ONLY a BDAddr, not something you can get from
            // MacOS, so we make it up for now. This sucks.
            address: uuid_to_bdaddr(&uuid.to_string()),
            address_type: None,
            local_name,
            tx_power_level: None,
            manufacturer_data: HashMap::new(),
            service_data: HashMap::new(),
            services: Vec::new(),
            discovery_count: 1,
        }));
        let notification_senders = Arc::new(Mutex::new(Vec::new()));
        let ns_clone = notification_senders.clone();
        let p_clone = properties.clone();
        let m_clone = manager.clone();
        task::spawn(async move {
            let mut event_receiver = event_receiver;
            loop {
                match event_receiver.next().await {
                    Some(CBPeripheralEvent::Notification(uuid, data)) => {
                        util::send_notification(
                            &ns_clone,
                            &ValueNotification { uuid, value: data },
                        );
                    }
                    Some(CBPeripheralEvent::ManufacturerData(manufacturer_id, data)) => {
                        let mut properties = p_clone.lock().unwrap();
                        properties
                            .manufacturer_data
                            .insert(manufacturer_id, data.clone());
                        m_clone.emit(CentralEvent::ManufacturerDataAdvertisement {
                            address: properties.address,
                            manufacturer_data: properties.manufacturer_data.clone(),
                        });
                    }
                    Some(CBPeripheralEvent::ServiceData(service_data)) => {
                        let mut properties = p_clone.lock().unwrap();
                        properties.service_data.extend(service_data.clone());
                        m_clone.emit(CentralEvent::ServiceDataAdvertisement {
                            address: properties.address,
                            service_data,
                        });
                    }
                    Some(CBPeripheralEvent::Services(services)) => {
                        let mut properties = p_clone.lock().unwrap();
                        properties.services = services.clone();
                        m_clone.emit(CentralEvent::ServicesAdvertisement {
                            address: properties.address,
                            services,
                        });
                    }
                    Some(CBPeripheralEvent::Disconnected) => (),
                    None => {
                        error!("Event receiver died, breaking out of corebluetooth device loop.");
                        break;
                    }
                }
            }
        });
        Self {
            properties,
            manager,
            characteristics: Arc::new(Mutex::new(BTreeSet::new())),
            notification_senders,
            uuid,
            message_sender,
        }
    }
pub ( crate ) fn new ( uuid : Uuid , local_name : Option < String > , manager : AdapterManager < Self > , event_receiver : Receiver < CBPeripheralEvent > , message_sender : Sender < CoreBluetoothMessage > , ) -> Self { // Since we're building the object, we have an active advertisement. // Build properties now. let properties = Arc :: new ( Mutex :: from ( PeripheralProperties { // Rumble required ONLY a BDAddr, not something you can get from // MacOS, so we make it up for now. This sucks. address : uuid_to_bdaddr ( & uuid . to_string ( ) ) , address_type : None , local_name , tx_power_level : None , manufacturer_data : HashMap :: new ( ) , service_data : HashMap :: new ( ) , services : Vec :: new ( ) , discovery_count : 1 , } ) ) ; let notification_senders = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ; let ns_clone = notification_senders . clone ( ) ; let p_clone = properties . clone ( ) ; let m_clone = manager . clone ( ) ; task :: spawn ( async move { let mut event_receiver = event_receiver ; loop { match event_receiver . next ( ) . await { Some ( CBPeripheralEvent :: Notification ( uuid , data ) ) => { util :: send_notification ( & ns_clone , & ValueNotification { uuid , value : data } , ) ; } Some ( CBPeripheralEvent :: ManufacturerData ( manufacturer_id , data ) ) => { let mut properties = p_clone . lock ( ) . unwrap ( ) ; properties . manufacturer_data . insert ( manufacturer_id , data . clone ( ) ) ; m_clone . emit ( CentralEvent :: ManufacturerDataAdvertisement { address : properties . address , manufacturer_data : properties . manufacturer_data . clone ( ) , } ) ; } Some ( CBPeripheralEvent :: ServiceData ( service_data ) ) => { let mut properties = p_clone . lock ( ) . unwrap ( ) ; properties . service_data . extend ( service_data . clone ( ) ) ; m_clone . emit ( CentralEvent :: ServiceDataAdvertisement { address : properties . address , service_data , } ) ; } Some ( CBPeripheralEvent :: Services ( services ) ) => { let mut properties = p_clone . lock ( ) . unwrap ( ) ; properties . services = services . clone ( ) ; m_clone . emit ( CentralEvent :: ServicesAdvertisement { address : properties . address , services , } ) ; } Some ( CBPeripheralEvent :: Disconnected ) => ( ) , None => { error ! ( " " ) ; break ; } } } } ) ; Self { properties , manager , characteristics : Arc :: new ( Mutex :: new ( BTreeSet :: new ( ) ) ) , notification_senders , uuid , message_sender , } }
This calls tokio::task::spawn, so it must be called from the context of a Tokio Runtime.
This calls tokio :: task :: spawn so it must be called from the context of a Tokio Runtime .
uuid
Uuid
local_name
Option<String>
manager
AdapterManager<Self>
event_receiver
Receiver<CBPeripheralEvent>
message_sender
Sender<CoreBluetoothMessage>