搜尋套件、儲存庫與 APP 分類

套件詳情v1.2.0

flutter_web_bluetooth

一個用於在您的網頁專案中使用 Web Bluetooth API 的程式庫。在 IO 專案中使用時也能良好相容。

Web
近 30 天下載
13.5萬
喜歡數
51
Pub 分數
160

安裝套件

flutter pub add flutter_web_bluetooth

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 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 原文快照,最新內容請造訪原始碼儲存庫。

pub package

Introduction

Flutter web bluetooth is a Flutter Dart 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.

For example:

export 'src/unsupported_library.dart'
    if (dart.library.js_interop) 'src/web_library.dart';

It will (of course) not work when not used in a browser, but it will compile!

The example code in this project is hosted on Github pages. Checkout jeroen1602.github.io/flutter_web_bluetooth/ for a proof of concept.

Limited support

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 = await FlutterWebBluetooth.instance.requestDevice(requestOptions);
} on UserCancelledDialogError {
  // The user cancelled the dialog
} on DeviceNotFoundError {
  // 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 characteristic
final 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.

import 'package:flutter_web_bluetooth/web_bluetooth_logger.dart';

setWebBluetoothLogger(Logger("my logger"));

所屬分類

依賴

依賴清單 3 項

原始碼儲存庫

使用此套件的專案

所在榜單