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

Ranging in background #61

Open AkshayNG opened 4 years ago

AkshayNG commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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 commented 3 years ago

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

PetrosPoll commented 3 years ago

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 commented 3 years ago

Good luck. could never get it to work as a service for more than 2-3 hrs. Especially IOS, power saving features turns things off.

Also had a hard time getting it approved with app stores. Both Android and Apple are leary about using BLE for contact tracing.

Regards,

Jason G Jakob @.*** Cel: 1- 519-841-8617

On Thu, Mar 11, 2021 at 5:19 AM Peter Pollakis @.***> wrote:

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!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/alann-maulana/flutter_beacon/issues/61#issuecomment-796628828, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM7GZZAQVKX27JZOKR2HBVLTDCKJPANCNFSM4NV2V4CA .

PetrosPoll commented 3 years ago

What you mean exactly ?

Do you have some code which works to help me please ?

Thank you!

On Fri, 12 Mar 2021 at 06:38, Jason Jakob @.***> wrote:

Good luck. could never get it to work as a service for more than 2-3 hrs. Especially IOS, power saving features turns things off.

Also had a hard time getting it approved with app stores. Both Android and Apple are leary about using BLE for contact tracing.

Regards,

Jason G Jakob @.*** Cel: 1- 519-841-8617

On Thu, Mar 11, 2021 at 5:19 AM Peter Pollakis @.***> wrote:

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!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub < https://github.com/alann-maulana/flutter_beacon/issues/61#issuecomment-796628828 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/AM7GZZAQVKX27JZOKR2HBVLTDCKJPANCNFSM4NV2V4CA

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/alann-maulana/flutter_beacon/issues/61#issuecomment-797228799, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJXG3ADHX7N2XUTTR2AGF2DTDGLDPANCNFSM4NV2V4CA .

jjakob666 commented 3 years ago

It will only range when the app is open.

We abandoned the app due to no viable solution for our particular need to range at all times when the phone is powered on.

Regards,

Jason G Jakob @.*** Cel: 1- 519-841-8617

On Fri, Mar 12, 2021 at 1:57 AM Peter Pollakis @.***> wrote:

What you mean exactly ?

Do you have some code which works to help me please ?

Thank you!

On Fri, 12 Mar 2021 at 06:38, Jason Jakob @.***> wrote:

Good luck. could never get it to work as a service for more than 2-3 hrs. Especially IOS, power saving features turns things off.

Also had a hard time getting it approved with app stores. Both Android and Apple are leary about using BLE for contact tracing.

Regards,

Jason G Jakob @.*** Cel: 1- 519-841-8617

On Thu, Mar 11, 2021 at 5:19 AM Peter Pollakis @.***> wrote:

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!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <

https://github.com/alann-maulana/flutter_beacon/issues/61#issuecomment-796628828

, or unsubscribe <

https://github.com/notifications/unsubscribe-auth/AM7GZZAQVKX27JZOKR2HBVLTDCKJPANCNFSM4NV2V4CA

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub < https://github.com/alann-maulana/flutter_beacon/issues/61#issuecomment-797228799 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/AJXG3ADHX7N2XUTTR2AGF2DTDGLDPANCNFSM4NV2V4CA

.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/alann-maulana/flutter_beacon/issues/61#issuecomment-797279876, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM7GZZBULY5G4V747TLPDQLTDG3MRANCNFSM4NV2V4CA .

PetrosPoll commented 3 years ago

Ok I understand but I don't have problem.

Can you share with me a example ?

On Sun, 14 Mar 2021 at 06:09, Jason Jakob @.***> wrote:

It will only range when the app is open.

We abandoned the app due to no viable solution for our particular need to range at all times when the phone is powered on.

Regards,

Jason G Jakob @.*** Cel: 1- 519-841-8617

On Fri, Mar 12, 2021 at 1:57 AM Peter Pollakis @.***> wrote:

What you mean exactly ?

Do you have some code which works to help me please ?

Thank you!

On Fri, 12 Mar 2021 at 06:38, Jason Jakob @.***> wrote:

Good luck. could never get it to work as a service for more than 2-3 hrs. Especially IOS, power saving features turns things off.

Also had a hard time getting it approved with app stores. Both Android and Apple are leary about using BLE for contact tracing.

Regards,

Jason G Jakob @.*** Cel: 1- 519-841-8617

On Thu, Mar 11, 2021 at 5:19 AM Peter Pollakis @.***> wrote:

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!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <

https://github.com/alann-maulana/flutter_beacon/issues/61#issuecomment-796628828

, or unsubscribe <

https://github.com/notifications/unsubscribe-auth/AM7GZZAQVKX27JZOKR2HBVLTDCKJPANCNFSM4NV2V4CA

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub <

https://github.com/alann-maulana/flutter_beacon/issues/61#issuecomment-797228799

, or unsubscribe <

https://github.com/notifications/unsubscribe-auth/AJXG3ADHX7N2XUTTR2AGF2DTDGLDPANCNFSM4NV2V4CA

.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub < https://github.com/alann-maulana/flutter_beacon/issues/61#issuecomment-797279876 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/AM7GZZBULY5G4V747TLPDQLTDG3MRANCNFSM4NV2V4CA

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/alann-maulana/flutter_beacon/issues/61#issuecomment-798829487, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJXG3AHXCDBFGZ4IJZE3UHLTDQZF3ANCNFSM4NV2V4CA .

alann-maulana commented 3 years ago

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 commented 3 years ago

That is correct. No workaround has been found to solve this problem

On Tue., Apr. 27, 2021, 12:38 a.m. Alann Maulana, @.***> wrote:

It's been a while for an issue. With recently release fluter_beacon: 0.4.0 https://pub.dev/packages/flutter_beacon/versions/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.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/alann-maulana/flutter_beacon/issues/61#issuecomment-827306911, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM7GZZGC4Z2Z4BVDHDDMTXLTKY5VJANCNFSM4NV2V4CA .