반응형

1. Android 권한 설정

2022.07.04 - [Flutter] - [BLE]Android 권한 설정 참조

 

2. flutter_reactive_ble 라이브러리 설치

2022.07.01 - [Flutter] - [BLE]flutter_reactive_ble 라이브러리 의 1.SDK Install 참조

 

3. 검색하고자하는 서비스 UUID 정의

//Service UUID
Uuid _UART_UUID = Uuid.parse("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");

모든 BLE 장치를 검색하는 경우 정의하지 않아도 된다.

 

3. flutter_reactive_ble 라이브러리 초기화 및 변수 선언

final flutterReactiveBle = FlutterReactiveBle(); //flutter_reactive_ble 라이브러리 초기화
List<DiscoveredDevice> _foundBleUARTDevices = []; //검색된 BLE_UART장치 List를 저장
bool _scanning = false; //SCAN 작업 진행 상태 저장
bool _connected = false;  //연결 상태 저장

4. Scan시작 함수 정의

Future<void> _startScan() async {
    if (Platform.isAndroid) {
      if(await checkIfPermissionGranted()) {
        if(_scanning==true) {
          _stopScan();
        }
        _foundBleUARTDevices = [];
        _scanning = true;
        setState(() {});
        _scanStream = flutterReactiveBle.scanForDevices(
          withServices: [_UART_UUID]).listen((device) {
          //withServices: []).listen((device) {
            if (_foundBleUARTDevices.every((element) => element.id != device.id)) {
              if(device.name.contains('ACRO')) {
                _foundBleUARTDevices.add(device);
                print("${device.name}\n");
                setState(() {});
              }
            }
          }, onError: (Object error) {
            print("${_logTexts}ERROR while scanning:$error \n");
          }
        );
      } else {
        await showNoPermissionDialog();
        openAppSettings();
      }
    }
    return Future<void>.value();
}

[전체 소스]

main.dart
0.01MB

 

 

 

 

반응형

+ Recent posts