https://pub.dev/packages/flutter_reactive_ble
flutter_reactive_ble | Flutter Package
Reactive Bluetooth Low Energy (BLE) plugin that can communicate with multiple devices
pub.dev
제가 개발하는 IoT 앱의 주요 기능 중에 하나가 블루투스 입니다.
블루투스 관련 개발은 안드로이드, iOS도 프레임워크 동작이 다르고, 실제 결과도 조금씩 달라서 굉장히 까다롭습니다.
각 OS별로 안정되게 구현된 블루투스 관련 로직을 플러터로 구현하면서 생각보다 꽤 많이 고생했습니다.
제가 설치해보고 테스트해본 라이브러리는 다음과 같습니다.
1. flutter_reactive_ble 5.0.2
2. flutter_ble_lib 2.3.2
3. flutter_blue 0.8.0
모두 장단점이 확실히 있습니다. 개발하시는 분의 스타일에 따라 가면 될 것 같습니다.
블루투스 모듈은 크게 검색, 연결, 데이터 쓰기, 데이터 읽기, 연결 종료 로 이루어 집니다.
위의 내용은 순차적으로 이루어지며, 순차적으로 잘 이루어 졌을 때는 모든 라이브러리가 큰 문제가 없었습니다.
하지만 제가 개발하는 앱은 검색, 재검색, 상시 연결 종료 등의 복잡한 기능을 가져서
결과적으로 유명한(LIKES, POPULARITY가 높은) 블루투스 라이브러리들로 만족할 수 없었습니다.
위 라이브러리들은 안드로이드에서는 모두 정상적으로 동작되나, iOS에서는 문제가 있었습니다.
flutter_reactive_ble 경우는
"스캔 후 재스캔 시에 스캔 중지가 필요한데, 스캔 중지의 기능이 따로 없어서 앱이 죽어버립니다"
혹시나 해서 실제 라이브러리 내부를 보면 스캔 시, 이전 스캔이 있으면 스캔 중지를 해야하는 로직이 없습니다.
따로 스캔 중지를 개발자가 할 수 있는 방법도 없어서... 결국 사용을 포기하였습니다.
아래는 flutter_reactive_ble 를 사용 방법 입니다.
설치 방법
dependencies:
flutter_reactive_ble: ^5.0.2
초기화 (싱글턴이라 최초 초기화 후 계속 공유)
final flutterReactiveBle = FlutterReactiveBle();
스캔 (검색)
flutterReactiveBle.scanForDevices(withServices: [serviceId], scanMode: ScanMode.lowLatency).listen((device) {
//결과 처리용 코드
}, onError: () {
//결과 처리용 코드
});
블루투스 연결 상태에 대한 대기
_ble.statusStream.listen((status) {
//code for handling status update
});
블루투스 연결
flutterReactiveBle.connectToDevice(
id: foundDeviceId,
servicesWithCharacteristicsToDiscover: {serviceId: [char1, char2]},
connectionTimeout: const Duration(seconds: 2),
).listen((connectionState) {
// Handle connection state updates
}, onError: (Object error) {
// Handle a possible error
});
블루투스 읽기
final characteristic = QualifiedCharacteristic(serviceId: serviceUuid, characteristicId: characteristicUuid, deviceId: foundDeviceId);
final response = await flutterReactiveBle.readCharacteristic(characteristic);
블루투스 쓰기
final characteristic = QualifiedCharacteristic(serviceId: serviceUuid, characteristicId: characteristicUuid, deviceId: foundDeviceId);
await flutterReactiveBle.writeCharacteristicWithResponse(characteristic, value: [0x00]);
'Flutter(플러터) > Bluetooth' 카테고리의 다른 글
Flutter 블루투스 기본 세팅 (0) | 2021.12.27 |
---|---|
Flutter 블루투스 - flutter_blue (0) | 2021.12.27 |
Flutter 블루투스 - flutter_ble_lib (0) | 2021.12.27 |
댓글