一个 Flutter 包,可在 Web 应用程序中访问 Web Bluetooth API,允许通过浏览器直接与蓝牙低功耗(BLE)设备进行交互。它可无缝集成到现有的 Flutter 项目中,包括面向 Web 和其他平台的项目。专为开发基于 Web 的物联网解决方案或设备通信应用的开发者设计,支持发现、连接和与 BLE 外设的数据交换。该包在不同环境之间保持兼容性,持续维护。
适合场景
从网页应用连接到BLE设备
使用实时传感器数据构建物联网仪表板
启用基于网络的设备控制界面
在混合应用中集成蓝牙功能
创建跨平台蓝牙体验
主要特点
Web Bluetooth API 的网页访问
支持设备发现和配对
来自BLE设备的实时数据流
与 Flutter 的跨平台兼容性
与现有 Flutter 项目的简单集成
适合对象
使用BLE设备的Web开发人员
物联网应用创建者
需要蓝牙支持的混合应用开发者
原型设计蓝牙功能的网页特性
需要最少原生代码的项目
使用建议
需要用户权限以访问蓝牙
仅限支持 Web Bluetooth API 的浏览器
不支持非Web平台
依赖于浏览器特定的实现细节
可能需要为安全上下文(HTTPS)进行额外设置
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
Introduction
Flutter web bluetooth is a FlutterDart plugin to add support for the
web bluetooth api.
The library doesn't require Flutter at all giving you the option to use it with projects like
AngularDart.
It is also written in such a way that it will play nicely with Dart native projects.
So if you have a cross-platform app and want to add web support you don't have to use conditional exports to exclude
the library in native releases.
The web bluetooth is still a draft, because of this it is not (yet) supported in every browser.
Check canIUse.com for information about browser support.
Some parts of the api aren't available in every version of the browser.
Requirements
The device and browser you're testing on needs to support the web api. For Chrome on Linux this is hidden behind a flag.
I've created a patch file which you can use to launch Google Chrome, from flutter run with the required flag enabled.
Check the chrome-experimental-launch folder's README for more information.
You will also need a secure context or else the api will not be available. So either https, or localhost.
Usage
First check if the current browser supports the web bluetooth api.
// The bluetooth api exists in this user agent.final supported = FlutterWebBluetooth.instance.isBluetoothApiSupported;
After this we will need to check if bluetooth is available
// A stream that says if a bluetooth adapter is available to the browser.final available = FlutterWebBluetooth.instance.isAvailable;
Now request a device from the browser.
You must beforehand specify which services you want to connect to! If you do not request
access to a specific service then you won't be able to discover the service!
Some services are also part of a so called blocklist. This is different per browsers and these services are just off
limit. The Web Bluetooth Community Group has a list
of all block services and characteristics.
// Define the services you want to communicate with here!final requestOptions = RequestOptionsBuilder.acceptAllDevices(optionalServices: [
BluetoothDefaultServiceUUIDS.deviceInformation.uuid
]);
try {
final device = awaitFlutterWebBluetooth.instance.requestDevice(requestOptions);
} onUserCancelledDialogError {
// The user cancelled the dialog
} onDeviceNotFoundError {
// There is no device in range for the options defined above
}
Now that you have a device, you can go through the services and then find the characteristics you want to read.
await device.connect();
final services = await device.discoverServices();
final service = services.firstWhere((service) => service.uuid == BluetoothDefaultServiceUUIDS.deviceInformation.uuid);
// Now get the characteristicfinal characteristic = await service.getCharacteristic(BluetoothDefaultCharacteristicUUIDS.manufacturerNameString.uuid);
final value = characteristic.readValue();
// Now we have a [ByteData] object with the manufacturer name in it.
device.disconnect();
Once you have paired to a device you can communicate with it again without the user needing to pair to it again. Get a
reference back to all the connected devices by listening to the devices stream.
Note: This feature will only work if the "Experimental Web Platform features" flag is enabled.
// A stream with a [Set] of all the devices that the user has paired, and given permission for.FlutterWebBluetooth.instance.devices;
You can also overwrite the logger that his library uses. This allows you to decide where log messages go. See
logging on pub.dev for more info on how to intercept these
messages.