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

插件详情v0.1.0

bluetooth

Flutter 的蓝牙插件

近 30 天下载
70
喜欢数
37
Pub 分数
25

安装插件

flutter pub add bluetooth

插件介绍

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

Flutter 的蓝牙包实现了跨平台蓝牙功能,使应用程序能够发现、连接并与其他蓝牙设备通信。它提供了一个简单的 API,用于扫描附近的设备、建立连接以及发送和接收数据。该插件专为需要与传感器、可穿戴设备或物联网设备交互的应用程序开发而设计,支持 Android 和 iOS 上的核心蓝牙操作。该插件由 Flutter 社区维护,并可通过 pub.dev 无缝集成到 Flutter 项目中。

适合场景

  • 连接到蓝牙传感器
  • 与可穿戴设备配对
  • 在移动设备和蓝牙硬件之间传输数据
  • 构建物联网控制应用程序
  • 启用设备到设备通信

主要特点

  • 设备发现和扫描
  • 蓝牙连接管理
  • 数据传输和接收
  • 跨平台支持(Android 和 iOS)
  • 简单直观的 API 设计

适合对象

  • 物联网应用开发者
  • 可穿戴技术创作者
  • 硬件集成专家
  • 使用蓝牙外设的移动应用开发者
  • 原型设计蓝牙功能

使用建议

  • 需要设备级别的蓝牙权限
  • 仅限基本蓝牙操作(无BLE特定功能)
  • 平台特定的行为可能有所不同
  • 没有针对所有边缘情况的内置错误处理
  • 依赖外部库的维护

原始 README

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

pub package

Introduction

Type-safe, minimalistic Buetooth plugin for Flutter, a fork from orignal "FlutterBlue" plugin.

Cross-Platform Bluetooth LE

This plugin aims to offer the most from both platforms (iOS and Android).

Using the FlutterBlue instance, you can scan for and connect to nearby devices (BluetoothDevice). Once connected to a device, the BluetoothDevice object can discover services (BluetoothService), characteristics (BluetoothCharacteristic), and descriptors (BluetoothDescriptor). The BluetoothDevice object is then used to directly interact with characteristics and descriptors.

Usage

Obtain an instance
FlutterBlue flutterBlue = FlutterBlue.instance;
Scan for devices
/// Start scanning
var scanSubscription = flutterBlue.scan().listen((scanResult) {
    // do something with scan result
});

/// Stop scanning
scanSubscription.cancel();
Connect to a device
/// Create a connection to the device
var deviceConnection = flutterBlue.connect(device).listen((s) {
    if(s == BluetoothDeviceState.connected) {
        // device is connected, do something
    }
});

/// Disconnect from device
deviceConnection.cancel();
Discover services
List<BluetoothService> services = await device.discoverServices();
services.forEach((service) {
    // do something with service
});
Read and write characteristics
// Reads all characteristics
var characteristics = service.characteristics;
for(BluetoothCharacteristic c in characteristics) {
    List<int> value = await device.readCharacteristic(c);
    print(value);
}

// Writes to a characteristic
await device.writeCharacteristic(c, [0x12, 0x34])
Read and write descriptors
// Reads all descriptors
var descriptors = characteristic.descriptors;
for(BluetoothDescriptor d in descriptors) {
    List<int> value = await device.readDescriptor(d);
    print(value);
}

// Writes to a descriptor
await device.writeDescriptor(d, [0x12, 0x34])
Set notifications
await device.setNotifyValue(characteristic, true);
device.onValueChanged(characteristic).listen((value) {
    // do something with new value
});

Reference

FlutterBlue API
Android iOS Description
scan :white_check_mark: :white_check_mark: Starts a scan for Bluetooth Low Energy devices.
connect :white_check_mark: :white_check_mark: Establishes a connection to the Bluetooth Device.
state :white_check_mark: :white_check_mark: Gets the current state of the Bluetooth Adapter.
onStateChanged :white_check_mark: :white_check_mark: Stream of state changes for the Bluetooth Adapter.
BluetoothDevice API
Android iOS Description
discoverServices :white_check_mark: :white_check_mark: Discovers services offered by the remote device as well as their characteristics and descriptors.
services :white_check_mark: :white_check_mark: Gets a list of services. Requires that discoverServices() has completed.
readCharacteristic :white_check_mark: :white_check_mark: Retrieves the value of a specified characteristic.
readDescriptor :white_check_mark: :white_check_mark: Retrieves the value of a specified descriptor.
writeCharacteristic :white_check_mark: :white_check_mark: Writes the value of a characteristic.
writeDescriptor :white_check_mark: :white_check_mark: Writes the value of a descriptor.
setNotifyValue :white_check_mark: :white_check_mark: Sets notifications or indications on the specified characteristic.
onValueChanged :white_check_mark: :white_check_mark: Notifies when the characteristic's value has changed.
state :white_check_mark: :white_check_mark: Gets the current state of the Bluetooth Device.
onStateChanged :white_check_mark: :white_check_mark: Notifies of state changes for the Bluetooth Device.

Troubleshooting

Scanning for service UUID's doesn't return any results

Make sure the device is advertising which service UUID's it supports. This is found in the advertisement packet as UUID 16 bit complete list or UUID 128 bit complete list.

所属分类

依赖

依赖清单 5 项

源码仓库

使用此插件的项目