MCPcopy Create free account
hub / github.com/deviceplug/btleplug / connect

Method connect

src/winrtble/peripheral.rs:393–442  ·  view source on GitHub ↗

Creates a connection to the device. This is a synchronous operation; if this method returns Ok there has been successful connection. Note that peripherals allow only one connection at a time. Operations that attempt to communicate with a device will fail until it is connected.

(&self)

Source from the content-addressed store, hash-verified

391 /// Ok there has been successful connection. Note that peripherals allow only one connection at
392 /// a time. Operations that attempt to communicate with a device will fail until it is connected.
393 async fn connect(&self) -> Result<()> {
394 let adapter_clone = self.shared.adapter.clone();
395 let address = self.shared.address;
396
397 let connection_status_changed = Box::new({
398 let shared_clone = Arc::downgrade(&self.shared);
399 move |is_connected| {
400 if let Some(shared) = shared_clone.upgrade() {
401 shared.connected.store(is_connected, Ordering::Relaxed);
402 }
403
404 if !is_connected {
405 if let Some(adapter) = adapter_clone.upgrade() {
406 adapter.emit(CentralEvent::DeviceDisconnected(address.into()));
407 }
408 }
409 }
410 });
411
412 let max_pdu_size_changed = Box::new({
413 let shared_clone = Arc::downgrade(&self.shared);
414 move |mtu| {
415 if let Some(shared) = shared_clone.upgrade() {
416 shared.mtu.store(mtu, Ordering::Relaxed);
417 }
418 }
419 });
420
421 let device = BLEDevice::new(
422 self.shared.address,
423 connection_status_changed,
424 max_pdu_size_changed,
425 )
426 .await?;
427
428 device.connect().await?;
429 // Query the system-cached device name (GAP name) and update local_name
430 if let Ok(name) = device.name() {
431 let name_str = name.to_string();
432 if !name_str.is_empty() {
433 let mut local_name_guard = self.shared.local_name.write().unwrap();
434 *local_name_guard = Some(name_str);
435 }
436 }
437 let mut d = self.shared.device.lock().await;
438 *d = Some(device);
439 self.shared.connected.store(true, Ordering::Relaxed);
440 self.emit_event(CentralEvent::DeviceConnected(self.shared.address.into()));
441 Ok(())
442 }
443
444 /// Terminates a connection to the device. This is a synchronous operation.
445 async fn disconnect(&self) -> Result<()> {

Callers

nothing calls this directly

Calls 4

emitMethod · 0.80
nameMethod · 0.80
writeMethod · 0.45
emit_eventMethod · 0.45

Tested by

no test coverage detected