This calls tokio::task::spawn, so it must be called from the context of a Tokio Runtime.
(
uuid: Uuid,
local_name: Option<String>,
advertisement_name: Option<String>,
manager: Weak<AdapterManager<Self>>,
event_receiver: Receiver<PeripheralEventInter
| 86 | impl Peripheral { |
| 87 | // This calls tokio::task::spawn, so it must be called from the context of a Tokio Runtime. |
| 88 | pub(crate) fn new( |
| 89 | uuid: Uuid, |
| 90 | local_name: Option<String>, |
| 91 | advertisement_name: Option<String>, |
| 92 | manager: Weak<AdapterManager<Self>>, |
| 93 | event_receiver: Receiver<PeripheralEventInternal>, |
| 94 | message_sender: Sender<CoreBluetoothMessage>, |
| 95 | ) -> Self { |
| 96 | // Since we're building the object, we have an active advertisement. |
| 97 | // Build properties now. |
| 98 | let properties = Mutex::from(PeripheralProperties { |
| 99 | address: BDAddr::default(), |
| 100 | address_type: None, |
| 101 | local_name, |
| 102 | advertisement_name, |
| 103 | tx_power_level: None, |
| 104 | rssi: None, |
| 105 | manufacturer_data: HashMap::new(), |
| 106 | service_data: HashMap::new(), |
| 107 | services: Vec::new(), |
| 108 | class: None, |
| 109 | }); |
| 110 | let (notifications_channel, _) = broadcast::channel(16); |
| 111 | |
| 112 | let shared = Arc::new(Shared { |
| 113 | properties, |
| 114 | manager, |
| 115 | services: Mutex::new(BTreeSet::new()), |
| 116 | notifications_channel, |
| 117 | uuid, |
| 118 | message_sender, |
| 119 | mtu: AtomicU16::new(crate::api::DEFAULT_MTU_SIZE), |
| 120 | }); |
| 121 | let shared_clone = shared.clone(); |
| 122 | task::spawn(async move { |
| 123 | let mut event_receiver = event_receiver; |
| 124 | let shared = shared_clone; |
| 125 | |
| 126 | loop { |
| 127 | match event_receiver.next().await { |
| 128 | Some(PeripheralEventInternal::Notification(uuid, service_uuid, data)) => { |
| 129 | let notification = ValueNotification { |
| 130 | uuid, |
| 131 | service_uuid, |
| 132 | value: data, |
| 133 | }; |
| 134 | |
| 135 | // Note: we ignore send errors here which may happen while there are no |
| 136 | // receivers... |
| 137 | let _ = shared.notifications_channel.send(notification); |
| 138 | } |
| 139 | Some(PeripheralEventInternal::ManufacturerData( |
| 140 | manufacturer_id, |
| 141 | data, |
| 142 | rssi, |
| 143 | )) => { |
| 144 | let mut properties = shared.properties.lock().unwrap(); |
| 145 | properties.rssi = Some(rssi); |
nothing calls this directly
no test coverage detected