alann-maulana / flutter_beacon

An hybrid iBeacon scanner and transmitter SDK for Flutter Android and iOS.
Apache License 2.0
118 stars 147 forks source link

Problem with 'regions' #14

Closed Calou07 closed 5 years ago

Calou07 commented 5 years ago

Hello I have a problem in the detection of beacons. Only one line makes the app to crash just after opening it. This line is : regions.add(Region(identifier: 'com.beacon')); Without it, the app open and runs on my device but it don't shows any beacons.

Here's the whole code :

import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_beacon/flutter_beacon.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  StreamSubscription<RangingResult> _streamRanging;
  final _regionBeacons = <Region, List<Beacon>>{};
  final _beacons = <Beacon>[];

  @override
  void initState() {
    super.initState();

    initBeacon();
  }

  initBeacon() async {
    try {
      await flutterBeacon.initializeScanning;
      print('Beacon scanner initialized');
    } on PlatformException catch (e) {
      print(e);
    }

    final regions = <Region>[];

    if (Platform.isIOS) {
      regions.add(Region(
          identifier: 'Apple Airlocate',
          proximityUUID: 'E2C56DB5-DFFB-48D2-B060-D0F5A71096E0'));
    } else {
      // android platform, it can ranging out of beacon that filter all of Proximity UUID
     regions.add(Region(identifier: 'com.beacon'));
    }

    flutterBeacon.monitoring(regions).listen((MonitoringResult result) {
      // result contains a region, event type and event state
    });

    _streamRanging = flutterBeacon.ranging(regions).listen((result) {
      result.beacons.forEach((b) {
        print(b.major);
        _beacons.sort(_compareParameters);
      });
    });
  }

  int _compareParameters(Beacon a, Beacon b) {
    int compare = a.proximityUUID.compareTo(b.proximityUUID);

    if (compare == 0) {
      compare = a.major.compareTo(b.major);
    }
    return compare;
  }

  @override
  void dispose() {
    if (_streamRanging != null) {
      _streamRanging.cancel();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Test détection balises'),
        ),
        body: _beacons == null
          ? Center(child: CircularProgressIndicator())
            : ListView(
          children: ListTile.divideTiles(
              context: context,
              tiles: _beacons.map((beacon) {
                return ListTile(
                  title: Text(beacon.proximityUUID),
                  subtitle: new Row(
                    mainAxisSize: MainAxisSize.max,
                    children: <Widget>[
                      Flexible(
                          child: Text(
                              'Major: ${beacon.major}\n',
                              style: TextStyle(fontSize: 13.0)),
                          flex: 1,
                          fit: FlexFit.tight),
                      Flexible(
                          child: Text(
                              'Accuracy: ${beacon.accuracy}m\nRSSI: ${beacon.rssi}',
                              style: TextStyle(fontSize: 13.0)),
                          flex: 2,
                          fit: FlexFit.tight)
                    ],
                  ),
                );
              })).toList(),
        ),
      ),
    );
  }
}

My phone is the Oneplus 6 under android 9. What can I do to prevent this to happen ?

alann-maulana commented 5 years ago

Hi @Calou07 ,

Can you check if all the permissions already complied? Starting for Android 6.0, you need to allow access for location services and enabled it. Also you need to turn on bluetooth for scanning.

Maybe you can post the error that happen here.

Thanks

Calou07 commented 5 years ago

Hello @alann-maulana , thank you for your response. The location and the bluetooth are activated and allowed.

The problem is that when I comment the line i said on the previous post, i can dubug it on my phone. When i uncomment it, the app crashes.

The last lines I have on the debug console are :

#37 pc 00000000002792c0  /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+968)

38 pc 0000000000524f94 /system/lib64/libart.so (MterpInvokeVirtual+588)

39 pc 0000000000547894 /system/lib64/libart.so (ExecuteMterpImpl+14228)

40 pc 0000000000c47e16 /system/framework/boot-framework.vdex (com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run+22)

41 pc 0000000000252fc4 /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.99565114+488)

42 pc 0000000000515828 /system/lib64/libart.so (artQuickToInterpreterBridge+1020)

43 pc 000000000055e2fc /system/lib64/libart.so (art_quick_to_interpreter_bridge+92)

44 pc 0000000000c0be40 /system/framework/arm64/boot-framework.oat (offset 0x3d7000) (com.android.internal.os.ZygoteInit.main+3088)

45 pc 000000000055544c /system/lib64/libart.so (art_quick_invoke_static_stub+604)

46 pc 00000000000cf6e8 /system/lib64/libart.so (art::ArtMethod::Invoke(art::Thread, unsigned int, unsigned int, art::JValue, char const)+232)

47 pc 000000000045cc50 /system/lib64/libart.so (art::(anonymous namespace)::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable const&, art::ArtMethod, art::(anonymous namespace)::ArgArray, art::JValue, char const)+104)

48 pc 000000000045c8b0 /system/lib64/libart.so (art::InvokeWithVarArgs(art::ScopedObjectAccessAlreadyRunnable const&, _jobject, _jmethodID, std::__va_list)+424)

49 pc 0000000000361b64 /system/lib64/libart.so (art::JNI::CallStaticVoidMethodV(_JNIEnv, _jclass, _jmethodID*, std::__va_list)+652)

50 pc 00000000000b3d1c /system/lib64/libandroid_runtime.so (_JNIEnv::CallStaticVoidMethod(_jclass, _jmethodID, ...)+120)

51 pc 00000000000b6764 /system/lib64/libandroid_runtime.so (android::AndroidRuntime::start(char const*, android::Vector const&, bool)+952)

52 pc 00000000000022f8 /system/bin/app_process64 (main+1380)

53 pc 00000000000acdac /system/lib64/libc.so (__libc_init+88)

Lost connection to device. Exited (sigterm)

Calou07 commented 5 years ago

I tried with older versions and 0.1.1. works fine on my phone. Newer versions do not work for me.

Thank you for your help, and I hope I also helped you by showing my problem.