Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ranging in background #61

Open
AkshayNG opened this issue Jun 6, 2020 · 14 comments
Open

Ranging in background #61

AkshayNG opened this issue Jun 6, 2020 · 14 comments

Comments

@AkshayNG
Copy link

AkshayNG commented Jun 6, 2020

Hi @alann-maulana, thanks for the library to make iBeacon detection easy and so our life :)

I'm trying to range the iBeacon as:

class _MyHomePageState extends State<MyHomePage> {

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

 void initializeScanning() async {
    try {
      await flutterBeacon.initializeAndCheckScanning;
      startRanging();
    } catch(e) {
      print(e);
    }
  }
 
 void startRanging()
 {
   final regions = <Region>[
      Region(identifier: Platform.isIOS ? this.identifier : null, 
                  proximityUUID: this.uuid)
  ];

  _streamRanging = flutterBeacon.ranging(regions).listen((RangingResult result) { 
      //Do some calculations for distance on detected beacon
      //Display the result in ListView
      //Fire local notification
  });
 }

}

I've tested this with 2 android devices. Its working fine when the app is in foreground, but I'm not getting notification when app is in background.

I was reading other issues and found your line: "some devices need to add some lines to enable background monitoring directly on Java/Kotlin code within Android project" from #45.

I would like to know those lines. Also, could you please help with the example on how can we detect iBeacon in both background and when app terminated for android. Once I achieve this for android, I'll head in for iOS later.

@AkshayNG AkshayNG changed the title Ranging iBeacon in background Ranging in background Jun 6, 2020
@AkshayNG
Copy link
Author

AkshayNG commented Jun 6, 2020

Update 1:

I tried debugging the code and got one of the two of my devices can range the beacon successfully in background, but another device can't detect in background.

So as @alann-maulana said - "some devices need to add some lines to enable background monitoring .." is correct. What are those magical lines?

@AkshayNG
Copy link
Author

AkshayNG commented Jun 6, 2020

Update 2:

As per background location limit, there is limit on background location for Android 8.0+ (API level 26) can receive location updates only a few times each hour.

@jjakob666
Copy link

BTW here is the function to calculate distance.

`double getRSSIToDistance(int rssi) {
double rssiVal = rssi.toDouble(); //convert to positive double

//MeasuredPower is a constant defined by the manufacturing of the device
//also known as the 1 Meter RSSI
//-69 (for kontakt BLE beacons)
double measuredPowerConstantDeviceManufacturer = -69;

//N constant depends on the Environmental factor. Range 2-4
double nConstantEnvironmental = 2;

double baseConstant= 10;

//calculation to convert rssi value to a distance in meters
//source: estimo - https://iotandelectronics.wordpress.com/2016/10/07/how-to-calculate-distance-from-the-rssi-value-of-the-ble-beacon/
//calc double distance = 10 ^ ((Measured Power - RSSI)/(10 * N))

double partA = measuredPowerConstantDeviceManufacturer - rssiVal;
double partB = baseConstant * nConstantEnvironmental;
double exponent = ( partA / partB );
double distance = pow(baseConstant, exponent);

return double.parse(distance.toStringAsFixed(2));

}`

@AkshayNG
Copy link
Author

AkshayNG commented Jun 14, 2020

@jjakob666 Hi, thanks for distance calculation code, but I'm looking out for the way to keep my app ranging beacons in background as in foreground:

 _streamRanging = flutterBeacon.ranging(regions).listen((RangingResult result) {
      // result.beacons.length > 0 in foreground
      //but result.beacons.length = 0 when app is in background for android v8+, working fine for android v7.
  });

I know I have to write platform-specific code in android module, but not sure what and how. Some of the techies out there suggested to use foreground services but I've no idea of android. I'm from iOS :)

@alann-maulana
Copy link
Owner

Hi @AkshayNG you can start your research by reading this comment from the author of Android Beacon Library at https://stackoverflow.com/a/51372392/3733730 :)

@saqib556612
Copy link

Hi @alann-maulana, thanks for the library to make iBeacon detection easy and so our life :)

I'm trying to range the iBeacon as:

class _MyHomePageState extends State<MyHomePage> {

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

 void initializeScanning() async {
    try {
      await flutterBeacon.initializeAndCheckScanning;
      startRanging();
    } catch(e) {
      print(e);
    }
  }
 
 void startRanging()
 {
   final regions = <Region>[
      Region(identifier: Platform.isIOS ? this.identifier : null, 
                  proximityUUID: this.uuid)
  ];

  _streamRanging = flutterBeacon.ranging(regions).listen((RangingResult result) { 
      //Do some calculations for distance on detected beacon
      //Display the result in ListView
      //Fire local notification
  });
 }

}

I've tested this with 2 android devices. Its working fine when the app is in foreground, but I'm not getting notification when app is in background.

I was reading other issues and found your line: "some devices need to add some lines to enable background monitoring directly on Java/Kotlin code within Android project" from #45.

I would like to know those lines. Also, could you please help with the example on how can we detect iBeacon in both background and when app terminated for android. Once I achieve this for android, I'll head in for iOS later.

I am not detecting the iBeacon can you please provide the code to detect the ibeacon packet that is coming from esp32 Bluetooth low energy device

@DRSchlaubi
Copy link
Contributor

Can someone provide a full example of how to implement this or update the apps actual example

@PetrosPoll
Copy link

Hello can you povide a full source code? I'm looking 3 weeks for an app that receives notifications from a beacon device!

Thank you!

@jjakob666
Copy link

jjakob666 commented Mar 12, 2021 via email

@PetrosPoll
Copy link

PetrosPoll commented Mar 12, 2021 via email

@jjakob666
Copy link

jjakob666 commented Mar 14, 2021 via email

@PetrosPoll
Copy link

PetrosPoll commented Mar 16, 2021 via email

@alann-maulana
Copy link
Owner

It's been a while for an issue. With recently release fluter_beacon: 0.4.0 I have provided a working example for ranging beacons. This ranging even still working while we put it on background mode for a couple of minutes (only). It won't work for a long period because both Android and iOS would terminate its service.

@jjakob666
Copy link

jjakob666 commented Apr 27, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants