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

套件詳情v0.1.9

flutter_blue_elves

一個用於藍牙 BLE 設備連接與控制的 Flutter 外掛程式。

AndroidiOS
近 30 天下載
105
喜歡數
28
Pub 分數
130

安裝套件

flutter pub add flutter_blue_elves

套件介紹

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

flutter_blue_elves 是一個 Flutter 插件,可在 Android 和 iOS 上實現藍牙低功耗(BLE)設備的連接與控制。它提供了一個簡單的介面,用於掃描、連接和與 BLE 外設通訊。專為開發與物聯網裝置、可穿戴裝置或信標互動的應用程式之開發者設計,該插件簡化了將 BLE 功能整合到 Flutter 應用程式中的過程。該套件支援基本的 BLE 操作,例如服務與特徵發現、資料讀寫以及即時通知。

適用情境

  • 連接到智慧家庭裝置
  • 控制可穿戴健身追蹤器
  • 從物聯網設備讀取傳感器數據
  • 基於信標的位置服務實現
  • 在健康應用中啟用設備配對

主要特色

  • 跨平台藍牙低功耗支援(Android 與 iOS)
  • 即時數據通知
  • 服務和特徵發現
  • 簡單的連接和斷開處理
  • 支援讀寫作業

適合對象

  • 物聯網應用開發者
  • 可穿戴技術項目
  • 健康與健身應用開發者
  • 基於信標的室內定位系統
  • 需要低耗能設備通訊的行動應用程式

使用建議

  • 需要在清單檔案中添加藍牙權限
  • 僅限BLE設備
  • 後台操作可能需要平台特定的設定
  • 無內建 UI 元件
  • 取決於底層平台藍牙堆疊的穩定性

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。


Flutter Bluetooth


flutter_blue_elves

A flutter plugin witch includes platform-specific implementation code for Android 18+ and/or iOS 10+ to connect and control bluetooth ble device.

Table of Contents

Install

This project is a flutter plugin,so you use it by add dependencies in your pubspec.yaml.

dependencies:
  flutter:
    sdk: flutter
  flutter_blue_elves: ^0.1.9
$ flutter pub get
Example

The example include all api usages.In ios,you have to add bluetooth permission tag in info file. img

Usage

Import plugin module where you need use.

import 'package:flutter_blue_elves/flutter_blue_elves.dart';
Check bluetooth function is Ok.
Effect Need version
Bluetooth permission 1. Scan for Bluetooth devices;
2. Connect with Bluetooth devices;
3. Communicate with Bluetooth devices
Android 31+
Location permission 1. Scan for Bluetooth devices;
2. Scan bluetooth device with physical location of the device;
ALL
location function Same as Location permission ALL
Bluetooth function Use Bluetooth ALL
///Android:
FlutterBlueElves.instance.androidCheckBlueLackWhat().then((values) {
if(values.contains(AndroidBluetoothLack.bluetoothPermission)){
///no bluetooth permission,if your target is android 12
}
if(values.contains(AndroidBluetoothLack.locationPermission)){
///no location permission
}
if(values.contains(AndroidBluetoothLack.locationFunction)){
///location powerOff
}
if(values.contains(AndroidBluetoothLack.bluetoothFunction)){
///bluetooth powerOff
}
});

///Ios:
FlutterBlueElves.instance.iosCheckBluetoothState().then((value) {
if(value==IosBluetoothState.unKnown){
///Bluetooth is not initialized
}else if(value==IosBluetoothState.resetting){
///Bluetooth is resetting
}else if(value==IosBluetoothState.unSupport){
///Bluetooth not support
}else if(value==IosBluetoothState.unAuthorized){
///No give bluetooth permission
}else if(value==IosBluetoothState.poweredOff){
///bluetooth powerOff
}else{
///bluetooth is ok
}
}
Turn on bluetooth function what bluetooth need.Just for android.
///apply location permission
FlutterBlueElves.instance.androidApplyLocationPermission((isOk) {
print(isOk ? "User agrees to grant location permission" : "User does not agree to grant location permission");
});
///turn on location function
FlutterBlueElves.instance.androidOpenLocationService((isOk) {
print(isOk ? "The user agrees to turn on the positioning function" : "The user does not agree to enable the positioning function");
});
///turn on bluetooth permission
FlutterBlueElves.instance.androidApplyBluetoothPermission((isOk) {
print(isOk ? "The user agrees to turn on the Bluetooth permission" : "The user does not agrees to turn on the Bluetooth permission");
});
///turn on bluetooth function
FlutterBlueElves.instance.androidOpenBluetoothService((isOk) {
print(isOk ? "The user agrees to turn on the Bluetooth function" : "The user does not agrees to turn on the Bluetooth function");
});
Scan bluetooth device not connected.
///start scan,you can set scan timeout
FlutterBlueElves.instance.startScan(5000).listen((scanItem) {
///Use the information in the scanned object to filter the devices you want
///if want to connect someone,call scanItem.connect,it will return Device object
Device device = scanItem.connect(connectTimeout: 5000);
///you can use this device to listen bluetooth device's state
device.stateStream.listen((newState){
///newState is DeviceState type,include disconnected,disConnecting, connecting,connected, connectTimeout,initiativeDisConnected,destroyed
}).onDone(() {
///if scan timeout or you stop scan,will into this
});
});

///stop scan
FlutterBlueElves.instance.stopScan();
Obtain devices that cannot be scanned because they are connected by other apps on the phone.
///Get Hide device.
///Because it is not scanned, some device information will be missing compared with the scanned results
FlutterBlueElves.instance.getHideConnectedDevices().then((values) {
  
});
Discovery device's bluetooth service,witch work in connected.
///use this stream to listen discovery result
device.serviceDiscoveryStream.listen((serviceItem) {
///serviceItem type is BleService,is readonly.It include BleCharacteristic and BleDescriptor
});
///to discovery service,witch work in connected
device.discoveryService();
Communicate with the device,witch work in connected.
///use this stream to listen data result
device.deviceSignalResultStream.listen((result) {
///result type is DeviceSignalResult,is readonly.It have DeviceSignalType attributes,witch include characteristicsRead,characteristicsWrite,characteristicsNotify,descriptorRead,descriptorWrite,unKnown.
///In ios you will accept unKnown,because characteristicsRead is as same as characteristicsNotify for ios.So characteristicsRead or characteristicsNotify will return unKnown.
});

///Read data from device by Characteristic.
///to read,witch work in connected
device.readData(serviceUuid,characteristicUuid);

///Write data to device by Characteristic.
///to write,witch work in connected.After my test,i find this isNoResponse is work in ios but not in android.
///In ios if you set isNoResponse,you will not receive data after write,but android will.
device.writeData(serviceUuid,characteristicUuid,isNoResponse,data);

///Read data from device by Descriptor.
device.devicereadDescriptorData(serviceUuid,characteristicUuid);

///Write data to device by Descriptor.
device.writeDescriptorData(serviceUuid,characteristicUuid,data);
Negotiate mtu with the device,witch work in connected and just for android

Ios automatically negotiates mtu with the device, so we don’t need to worry about it. After my test, the maximum value of mtu negotiated by ios is 185, but as long as the device allows it, ios can also write more than 185 bytes of data at one time

///Mtu range is in [23,517]
device.androidRequestMtu(512, (isSuccess, newMtu){
///get the result in this callback
///if isSuccess is true,the newMtu is new
///if isSuccess is false,this newMtu is current
});
Connect&Disconnect Device
///work in disConnected
///This connection method is direct connection, because it has been connected before, so I saved the direct connection object, and I can use this object to connect again
device.connect(5000);
///work in connected
///disconnect device
device.disConnect();
Destroy Device object
///if you never use this Device Object,call destroy(),witch can save space
device.destroy();
Watch Device rssi change
///use this stream to listen rssi change
device.deviceSignalResultStream.listen((newRssi) {
  
});
///start watch rssi
device.startWatchRssi().then((ok){
  if(ok){
    ///start ok
  }else{
    ///start error
  } 
})
///stop watch rssi
device.stopWatchRssi().then((ok){
  if(ok){
    ///stop ok
  }else{
    ///stop error
  }
})

License

MIT © PineappleOilPrince

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。