No description
Find a file
2025-06-18 14:40:56 -07:00
.github/workflows Update build 2025-06-18 14:40:56 -07:00
Btleplug.CmdLine Separate events/scan 2024-02-27 23:36:14 -08:00
Btleplug.Tests Remove test since there is no bluetooth on the build machine 2024-02-27 17:22:16 -08:00
VaettirNet.Btleplug Fix GC issues 2025-06-18 12:37:54 +02:00
.gitignore Initial windows only implementation 2024-02-27 00:17:10 -08:00
btleplug-c.version.txt Update to version 0.0.7 of btleplug-c 2024-02-29 21:30:41 -08:00
Directory.Build.props Make packages build 2024-02-27 02:03:10 -08:00
LICENSE.md Create LICENSE.md 2024-02-27 00:22:21 -08:00
README.md Update build 2025-06-18 14:40:56 -07:00
setup.bat Update to version 0.0.7 of btleplug-c 2024-02-29 21:30:41 -08:00
setup.sh Update to version 0.0.7 of btleplug-c 2024-02-29 21:30:41 -08:00
VaettirNet.Btleplug.sln Make packages build 2024-02-27 02:03:10 -08:00
VaettirNet.Btleplug.sln.DotSettings Initial windows only implementation 2024-02-27 00:17:10 -08:00

VaettirNet.Btleplug

Build Status NuGet

Overview

VaettirNet.Btleplug is a .NET wrapper for BtlePlug, providing a convenient way to work with Bluetooth Low Energy (BLE) devices in .NET applications. This library enables seamless BLE communication across multiple platforms, including Windows, Linux, and macOS.

Features

  • Cross-platform BLE support (Windows, Linux, macOS)
  • Device discovery and connection management
  • Service and characteristic enumeration
  • Read and write operations for BLE characteristics
  • Notification subscription for real-time data updates
  • Simple, intuitive API for BLE communication

Installation

Install the package via NuGet:

dotnet add package VaettirNet.Btleplug

Usage Example

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using VaettirNet.Btleplug;
using VaettirNet.Btleplug.Interop;

// Create a BLE manager
BtleManager.SetLogLevel(BtleLogLevel.Debug);
var manager = BtleManager.Create();

// Scan for devices with a specific service UUID
var serviceUuid = Guid.Parse("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
CancellationTokenSource cts = new();
cts.CancelAfter(TimeSpan.FromSeconds(30));

// Discover and connect to peripherals
await foreach (BtlePeripheral peripheral in manager.GetPeripherals([serviceUuid], includeServices: true, cts.Token))
{
    Console.WriteLine($"Found device: {peripheral.GetId()}");
    
    // Connect to the peripheral
    await peripheral.ConnectAsync();
    bool isConnected = await peripheral.IsConnectedAsync();
    
    // Discover services and characteristics
    foreach (BtleService service in await peripheral.GetServicesAsync())
    {
        Console.WriteLine($"Service: {service.Uuid}");
        
        foreach (BtleCharacteristic characteristic in service.Characteristics)
        {
            Console.WriteLine($"Characteristic: {characteristic.Uuid} with properties: {characteristic.Properties}");
            
            // Register for notifications if supported
            if (characteristic.Properties.HasFlag(CharacteristicProperty.Notify))
            {
                await peripheral.RegisterNotificationCallback(
                    service.Uuid, 
                    characteristic.Uuid, 
                    (p, s, c, data) => {
                        Console.WriteLine($"Notification received: {data.Length} bytes");
                    }
                );
            }
        }
    }
    
    // Don't forget to disconnect and dispose when done
    await peripheral.DisconnectAsync();
    peripheral.Dispose();
}

Platform Support

VaettirNet.Btleplug supports the following platforms:

  • Windows (x64)
  • Linux (x64)
  • macOS (x64, arm64)

The library automatically loads the appropriate native dependencies for your platform.

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.