flutter_blue_elves
一个 Flutter 插件,包含 Android 和/或 iOS 的平台特定实现代码,用于连接和控制蓝牙 BLE 设备。
安装
本项目是一个 Flutter 插件,因此您可以通过在 pubspec.yaml 中添加依赖项来使用它。
dependencies:
flutter:
sdk: flutter
flutter_blue_elves: ^0.1.0
$ flutter pub get
用法
在需要使用的地方导入插件模块。
import 'package:flutter_blue_elves/flutter_blue_elves.dart';
检查蓝牙功能是否正常。
///Android:
FlutterBlueElves.instance.androidCheckBlueLackWhat().then((values) {
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
}
}
打开蓝牙功能,这是蓝牙所必需的。仅适用于 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 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");
});
扫描未连接的蓝牙设备。
///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();
发现设备的蓝牙服务。
///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();
与设备通信,在连接状态下工作。
///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);
连接&断开设备
///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();
销毁设备对象
///if you never use this Device Object,call destroy(),witch can save space
device.destroy();
许可证
MIT © PineappleOilPrince