Skip to content

Commit cd01219

Browse files
committed
updates
1 parent 0103251 commit cd01219

3 files changed

Lines changed: 74 additions & 6 deletions

File tree

src/content/docs/client/obd/ble.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,35 @@ If the defaults don't work, use a BLE scanner app (like nRF Connect) to discover
4545

4646
## Creating a Transport
4747

48+
### From a Discovered Device (recommended)
49+
50+
Use `BleObdDeviceScanner` to find adapters, then pass the selected device:
51+
52+
```csharp
53+
IBleManager bleManager = /* from DI or Shiny setup */;
54+
55+
var scanner = new BleObdDeviceScanner(bleManager, new BleObdConfiguration
56+
{
57+
DeviceNameFilter = "OBDII" // optional: matches any device name containing "OBDII"
58+
});
59+
60+
var cts = new CancellationTokenSource();
61+
var devices = new List<ObdDiscoveredDevice>();
62+
63+
await scanner.Scan(device =>
64+
{
65+
devices.Add(device);
66+
Console.WriteLine($"Found: {device.Name} ({device.Id})");
67+
}, cts.Token);
68+
69+
// Connect to a selected device
70+
var transport = new BleObdTransport(devices[0], new BleObdConfiguration());
71+
var connection = new ObdConnection(transport);
72+
await connection.Connect();
73+
```
74+
75+
`BleObdDeviceScanner` deduplicates by peripheral UUID — each device is reported once. Cancel the token to stop scanning.
76+
4877
### Auto-Scan
4978

5079
Let the transport scan for the first device matching your filter:
@@ -75,6 +104,27 @@ var connection = new ObdConnection(transport);
75104
await connection.Connect(); // connects to known peripheral, initializes
76105
```
77106

107+
## MAUI Setup
108+
109+
Register BLE and the scanner in your `MauiProgram.cs`:
110+
111+
```csharp
112+
using Shiny;
113+
using Shiny.Obd;
114+
using Shiny.Obd.Ble;
115+
116+
var builder = MauiApp.CreateBuilder();
117+
builder.UseMauiApp<App>();
118+
119+
builder.Services.AddBluetoothLE();
120+
builder.Services.AddSingleton(new BleObdConfiguration { DeviceNameFilter = "OBD" });
121+
builder.Services.AddSingleton<IObdDeviceScanner, BleObdDeviceScanner>();
122+
```
123+
124+
:::note
125+
In Shiny.BluetoothLE v4, use `services.AddBluetoothLE()` (namespace `Shiny`). There is no `UseShiny()` or `AddBle()` call needed.
126+
:::
127+
78128
## Full Example
79129

80130
```csharp
@@ -143,7 +193,7 @@ public class DashboardData
143193

144194
The BLE transport:
145195

146-
1. **Scans** for a peripheral matching the configured service UUID and optional device name filter (or uses a pre-provided peripheral)
196+
1. **Scans** for a peripheral matching the configured service UUID and optional device name filter (or uses a pre-provided peripheral / discovered device)
147197
2. **Connects** using Shiny's task-based `ConnectAsync`
148198
3. **Subscribes** to notifications on the read characteristic via `NotifyCharacteristic`
149199
4. **Sends commands** by writing bytes to the write characteristic via `WriteCharacteristicAsync`

src/content/docs/client/obd/index.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Shiny.Obd is a .NET library for communicating with vehicles through OBD-II (On-B
99
- **Command-object pattern** — OBD commands are objects, not methods. Pass built-in commands or create your own for custom PIDs.
1010
- **Generic return types** — each command declares its return type (`int`, `double`, `string`, `TimeSpan`, etc.) with compile-time safety.
1111
- **Pluggable transports**`IObdTransport` abstracts the communication channel. Ships with BLE; add WiFi or USB later.
12+
- **Device discovery**`IObdDeviceScanner` finds available adapters before connecting. BLE scanner included.
1213
- **Adapter auto-detection** — detects ELM327 vs OBDLink (STN) adapters via ATI and runs the appropriate initialization sequence.
1314
- **Task-based async** — fully async/await throughout, no Reactive Extensions required in consuming code.
1415
- **9 standard commands included** — speed, RPM, coolant temp, throttle, fuel level, engine load, intake air temp, runtime, and VIN.
@@ -32,13 +33,21 @@ using Shiny.Obd;
3233
using Shiny.Obd.Ble;
3334
using Shiny.Obd.Commands;
3435

35-
// Create BLE transport
36-
var transport = new BleObdTransport(bleManager, new BleObdConfiguration
36+
// Scan for an adapter
37+
var scanner = new BleObdDeviceScanner(bleManager, new BleObdConfiguration
3738
{
3839
DeviceNameFilter = "OBDLink"
3940
});
41+
var cts = new CancellationTokenSource();
42+
ObdDiscoveredDevice? selected = null;
43+
await scanner.Scan(device =>
44+
{
45+
selected = device;
46+
cts.Cancel();
47+
}, cts.Token);
4048

41-
// Create connection (auto-detects adapter type)
49+
// Connect using the discovered device
50+
var transport = new BleObdTransport(selected!, new BleObdConfiguration());
4251
var connection = new ObdConnection(transport);
4352
await connection.Connect();
4453

@@ -65,8 +74,8 @@ var vin = await connection.Execute(StandardCommands.Vin); // string
6574
└──────────────────────┬──────────────────────────┘
6675
6776
┌──────────────────────▼──────────────────────────┐
68-
IObdTransport
69-
Pluggable transport layer
77+
IObdDeviceScanner ──▶ IObdTransport │
78+
Discover adapters Pluggable transport layer
7079
│ ┌────────────────┐ ┌────────┐ ┌─────────┐ │
7180
│ │ BleObdTransport│ │ WiFi │ │ USB │ │
7281
│ │ (Shiny BLE) │ │(future)│ │(future) │ │

src/content/docs/client/obd/transports.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ title: Custom Transports
44

55
The `IObdTransport` interface abstracts the communication channel between your app and the OBD adapter. The library ships with BLE, but you can implement WiFi, USB, or any other transport.
66

7+
For transports that support discovery (BLE, WiFi), implement `IObdDeviceScanner` as well:
8+
9+
```csharp
10+
public interface IObdDeviceScanner
11+
{
12+
Task Scan(Action<ObdDiscoveredDevice> onDeviceFound, CancellationToken ct = default);
13+
}
14+
```
15+
716
## IObdTransport Interface
817

918
```csharp

0 commit comments

Comments
 (0)