Peripheral
Overview
Section titled “Overview”IPeripheral represents a BLE device discovered during scanning. It provides connection management and GATT operations.
| Property | Type | Description |
|---|---|---|
Uuid | string | Unique identifier for this peripheral |
Name | string? | Local name (may be null) |
Mtu | int | Current MTU size |
Status | ConnectionState | Current connection state |
Connecting
Section titled “Connecting”IPeripheral peripheral; // from scan result
// Fire and forget - connects when in rangeperipheral.Connect();
// Async - waits for connection to establishawait peripheral.ConnectAsync(cancelToken: cts.Token, timeout: TimeSpan.FromSeconds(10));Auto Connect (Android)
Section titled “Auto Connect (Android)”On Android, AutoConnect controls whether the system should automatically connect when the peripheral comes into range.
peripheral.Connect(new ConnectionConfig(AutoConnect: true));Disconnecting
Section titled “Disconnecting”peripheral.CancelConnection();
// or asyncawait peripheral.DisconnectAsync();Always call CancelConnection() when you’re done with a peripheral. Not doing so will leave the connection open and drain the device battery.
Monitoring Connection Status
Section titled “Monitoring Connection Status”peripheral .WhenStatusChanged() .Subscribe(state => { // ConnectionState: Connecting, Connected, Disconnecting, Disconnected });
// Convenience extensionsperipheral.WhenConnected().Subscribe(p => { /* connected */ });peripheral.WhenDisconnected().Subscribe(p => { /* disconnected */ });Connection Failures
Section titled “Connection Failures”peripheral .WhenConnectionFailed() .Subscribe(ex => { // BleException with details about the failure });MTU Negotiation
Section titled “MTU Negotiation”MTU (Maximum Transmission Unit) determines the maximum data size per packet. The default is typically 20 bytes.
// Check if MTU requests are supportedif (peripheral.CanRequestMtu()){ var newMtu = await peripheral.TryRequestMtuAsync(512); Console.WriteLine($"Negotiated MTU: {newMtu}");}Pairing
Section titled “Pairing”// Check if pairing is availableif (peripheral.IsPairingRequestsAvailable()){ var result = await peripheral .TryPairingRequest() .ToTask();
if (result == true) Console.WriteLine("Paired successfully");}
// Check current pairing statusvar status = peripheral.TryGetPairingStatus();// PairingState: NotPaired, PairedReading RSSI
Section titled “Reading RSSI”var rssi = await peripheral.ReadRssiAsync();Console.WriteLine($"RSSI: {rssi} dBm");