flutter_blue_elves는 Android 및 iOS에서 블루투스 저전력(BLE) 장치의 연결 및 제어를 가능하게 하는 Flutter 플러그인입니다. BLE 주변기기 스캔, 연결 및 통신을 위한 간단한 인터페이스를 제공합니다. IoT 기기, 웨어러블 또는 비콘과 상호작용하는 앱을 개발하는 개발자를 위해 설계되었으며, Flutter 애플리케이션에 BLE 기능을 쉽게 통합할 수 있도록 합니다. 이 패키지는 서비스 및 특성 탐색, 데이터 읽기/쓰기, 실시간 알림과 같은 기본적인 BLE 작업을 지원합니다.
사용 사례
스마트 홈 장치 연결
웨어러블 피트니스 트래커 제어
사물인터넷 장치에서 센서 데이터 읽기
비콘 기반 위치 서비스 구현
건강 앱에서 장치 쌍 연결 활성화
주요 기능
크로스 플랫폼 BLE 지원 (Android 및 iOS)
실시간 데이터 알림
서비스 및 특성 탐색
간단한 연결 및 분리 처리
읽기/쓰기 작업 지원
적합한 대상
IoT 애플리케이션 개발자
웨어러블 기술 프로젝트
헬스 및 피트니스 앱 개발자
비콘 기반 실내 위치 결정 시스템
저에너지 장치 통신을 요구하는 모바일 앱
고려 사항
매니페스트 파일에서 블루투스 권한이 필요합니다
BLE 전용 장치에 한정
백그라운드 작업은 플랫폼별 구성이 필요할 수 있습니다
내장된 UI 컴포넌트 없음
하위 플랫폼 BLE 스택의 안정성에 따라 달라집니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
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.
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
}elseif(value==IosBluetoothState.resetting){
///Bluetooth is resetting
}elseif(value==IosBluetoothState.unSupport){
///Bluetooth not support
}elseif(value==IosBluetoothState.unAuthorized){
///No give bluetooth permission
}elseif(value==IosBluetoothState.poweredOff){
///bluetooth powerOff
}else{
///bluetooth is ok
}
}
Turn on bluetooth function what bluetooth need.Just for android.
///apply location permissionFlutterBlueElves.instance.androidApplyLocationPermission((isOk) {
print(isOk ? "User agrees to grant location permission" : "User does not agree to grant location permission");
});
///turn on location functionFlutterBlueElves.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 permissionFlutterBlueElves.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 functionFlutterBlueElves.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 timeoutFlutterBlueElves.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 objectDevice 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 scanFlutterBlueElves.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 resultsFlutterBlueElves.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
}
})