搜索插件、代码仓与 APP 分类

插件详情v1.1.1

win_ble

使用 WinBle 插件在 Flutter Windows 和纯 Dart 项目中启用蓝牙低功耗(仅限 Windows)

Windows
近 30 天下载
13.7万
喜欢数
39
Pub 分数
150

安装插件

flutter pub add win_ble

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

win_ble 包为针对 Windows 和纯 Dart 项目的 Flutter 应用程序提供了蓝牙低功耗 (BLE) 功能。专为 Windows 设计,它提供了一个通往原生 BLE 功能的桥梁,使开发者能够发现设备、连接外设、读取和写入特征值,并管理 BLE 操作。该插件非常适合需要在 Windows 平台上与 BLE 设备进行低功耗无线通信的应用程序。它通过平台通道利用 Windows 蓝牙 API,确保在 Flutter 生态系统中实现可靠性能。

适合场景

  • 连接到BLE传感器
  • 从蓝牙信标读取数据
  • 控制蓝牙低功耗(BLE)物联网设备
  • 开发仅限 Windows 的健康监控应用
  • 实现基于接近度的功能

主要特点

  • 仅限 Windows 的 BLE 支持
  • 原生 Windows 蓝牙 API 集成
  • 设备发现和连接管理
  • 特性读写操作
  • 纯 Dart 兼容性

适合对象

  • Windows 桌面应用程序开发者
  • 物联网设备集成商
  • 健康与健身应用开发者
  • 以蓝牙为中心的实用工具
  • 具有 Windows 特定 BLE 需求的跨平台应用程序

使用建议

  • 仅限 Windows 平台
  • 需要 Windows 10 或更高版本
  • 不支持 Android/iOS
  • 依赖系统级蓝牙权限
  • 可能需要为某些硬件进行额外配置

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

WinBle

win_ble version

Use the WinBle plugin to enable Bluetooth Low Energy in Flutter Windows and pure Dart projects (Windows only)

Usage

First initialize WinBle, to initialize on Flutter Windows, get server path using await WinServer.path, and for pure dart projects ( Windows only ) Download BleServer.exe file and place in the same folder, checkout example_dart for more details

  // To initialize on Flutter Windows
  await WinBle.initialize(serverPath: await WinServer.path());

  // For pure dart projects
  await WinBle.initialize(serverPath: "Path of BLEServer.exe file");

Dispose WinBle after using

 WinBle.dispose();

To Start Scan

 WinBle.startScanning();

 StreamSubscription? scanStream = WinBle.scanStream.listen((event) {
  // Get Devices Here
 });

To Stop Scan

  WinBle.stopScanning();

  scanStream?.cancel();

To Connect

 // To Connect
 await WinBle.connect(address);

 // Get Connection Updates Here
 StreamSubscription  _connectionStream =
    WinBle.connectionStreamOf(device.address).listen((event) {
    // event will be a boolean , in which
    // true => Connected
    // false => Disconnected
 });

To Disconnect

  await WinBle.disconnect(address);

To get MaxMtuSize

  await WinBle.getMaxMtuSize(address);

Handle Bluetooth radio

  // To get rdaio status
  WinBle.getBluetoothState();

  // To get updates of radio state
  WinBle.bleState.listen((BleState state) {
    // Get BleState (On, Off, Unknown, Disabled, Unsupported)
  });

  // To turn on radio
  WinBle.updateBluetoothState(true);

  // To turn off radio
  WinBle.updateBluetoothState(false);

Pairing Options

  // To Pair
  await WinBle.pair(address);

  // To UnPair
  await WinBle.unPair(address);

  // To Check if Device can be Paired
  bool canPair = await WinBle.canPair(address);

  // To Check if Device is Already Paired
  bool isPaired = await WinBle.isPaired(address);

Rest All Methods are

  // To Get Services
  var services = await WinBle.discoverServices(address);

  // To Get Characteristic
  List<BleCharacteristic> bleCharacteristics = await WinBle.discoverCharacteristics(address: address, serviceId: serviceID);

  // To Read Characteristic
  List<int> data = await WinBle.read(address: address, serviceId: serviceID, characteristicId: charID);

  // To Write Characteristic
  await WinBle.write( address: address, service: serviceID,  characteristic: charID,  data: data, writeWithResponse: writeWithResponse);

  // To Start Subscription
  await WinBle.subscribeToCharacteristic(address: address, serviceId: serviceID, characteristicId: charID);

  // To Stop Subscription
  await WinBle.unSubscribeFromCharacteristic(address: address, serviceId: serviceID, characteristicId: charID);

  // Get Characteristic Value Updates Here
   StreamSubscription _characteristicValueStream = WinBle.characteristicValueStream.listen((event) {
     // Here We will Receive All Characteristic Events
   });

Note

Requires Windows version >= 10.0.15014

If windows release build opens cmd while running app, try this

Edit your /windows/runner/main.cpp file, this is a known flutter issue

if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()){
CreateAndAttachConsole();
}
// Add this Code
// <------- From Here --------- >
else{
    STARTUPINFO si = {0};
    si.cb = sizeof(si);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;

    PROCESS_INFORMATION pi = {0};
    WCHAR lpszCmd[MAX_PATH] = L"cmd.exe";
    if (::CreateProcess(NULL, lpszCmd, NULL, NULL, FALSE, CREATE_NEW_CONSOLE | CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
    {
      do
      {
        if (::AttachConsole(pi.dwProcessId))
        {
          ::TerminateProcess(pi.hProcess, 0);
          break;
        }
      } while (ERROR_INVALID_HANDLE == GetLastError());
      ::CloseHandle(pi.hProcess);
      ::CloseHandle(pi.hThread);
    }
}
// <------- UpTo Here --------- >

Additional information

Thanks to noble-winrt for initial BleServer Code

This is Just The Initial Version feel free to Contribute or Report any Bug!

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

所在榜单