AltBeacon / android-beacon-library

Allows Android apps to interact with BLE beacons
Apache License 2.0
2.84k stars 836 forks source link

beacon distance value showing negative distance on Motorola phones (moto edge 20 pro & moto g5+) #1115

Open gptshubham595 opened 1 year ago

gptshubham595 commented 1 year ago

Expected behavior

Distance observed via beacon should be positive

Actual behavior

   Distance observed via beacon is negative at any distance even if there's a little shake

Steps to reproduce this behavior

  1. Install altbeacon reference app on mototrola phone and observe the distance shown
  2. shake the phone

Mobile device model and OS version

 Tested on moto edge 20 pro & moto g5+
 OS Version: 12

Android Beacon Library version

altbeacon_version = "2.19.4"

davidgyoung commented 1 year ago

Thanks for this report. Can you please provide a few samples of these two values when you see the negative distance?

  1. beacon.runningAverageRssi
  2. beacon.distance
gptshubham595 commented 1 year ago

Tested on Moto G5 S+

13:37:28 | Distance: | 1.358780418 -- | -- | -- 13:37:28 | Running | -87 13:37:29 | Ranged: | 1 13:37:29 | Distance: | 1.042936817 13:37:29 | Running | -85 13:37:30 | Ranged: | 0 13:37:30 | Ranged: | 0 13:37:31 | Ranged: | 0 13:37:32 | Ranged: | 1 13:37:32 | Distance: | 0.7765247283 13:37:32 | Running | -83 13:37:32 | Ranged: | 1 13:37:32 | Distance: | -0.1508527346 13:37:32 | Running | -70 13:37:33 | Ranged: | 1 13:37:33 | Distance: | -0.1133826044 13:37:33 | Running | -71 13:37:34 | Ranged: | 1 13:37:34 | Distance: | -0.2150709093 13:37:34 | Running | -68 13:37:35 | Ranged: | 0 13:37:35 | Ranged: | 1 13:37:35 | Distance: | 0.9506274682 13:37:35 | Running | -65.66666667 13:37:36 | Ranged: | 0 13:37:37 | Ranged: | 1 13:37:37 | Distance: | -0.2590933289 13:37:37 | Running | -66.33333333 13:37:38 | Ranged: | 0 13:37:39 | Ranged: | 1 13:37:39 | Distance: | -0.2052836873 13:37:39 | Running | -68.33333333 13:37:40 | Ranged: | 1 13:37:40 | Distance: | -0.242427232 13:37:40 | Running | -67 13:37:40 | Ranged: | 0 13:37:41 | Ranged: | 1 13:37:41 | Distance: | -0.242427232 13:37:41 | Running | -67 13:37:42 | Ranged: | 1 13:37:42 | Distance: | -0.226366312 13:37:42 | Running | -67.6 13:37:43 | Ranged: | 0 13:37:44 | Ranged: | 1 13:37:44 | Distance: | -0.1846436131 13:37:44 | Running | -69 13:37:45 | Ranged: | 0 13:37:46 | Ranged: | 1 13:37:46 | Distance: | -0.1508527346 13:37:46 | Running | -70 13:37:46 | Ranged: | 0 13:37:47 | Ranged: | 1 13:37:47 | Distance: | -0.2669835118 13:37:47 | Running | -66 13:37:48 | Ranged: | 0 13:37:49 | Ranged: | 0 13:37:50 | Ranged: | 0 13:37:50 | Ranged: | 1 13:37:50 | Distance: | 0.4189038879 13:37:50 | Running | -60.5 13:37:51 | Ranged: | 0 13:37:52 | Ranged: | 1 13:37:52 | Distance: | 0.2746885648 13:37:52 | Running | -58 13:37:53 | Ranged: | 0
gptshubham595 commented 1 year ago

Also i've noted that the beacon reading (Ranged 0 beacons) even if the beacon advertiser and receiver are in close vicinity can you help how we can read beacons consistently

foregroundScanPeriod = 700 
bacgroundScanPeriod = 700
RunningAverageRssiFilter.setSampleExpirationMiliSeconds(5000L)
RenisDudhat commented 1 year ago

Hi @davidgyoung

I faced the same issue that sometimes getting negative distance on Motorola devices.

Explanation :

In short, The negative distance measured in Motorola devices is because of the negative value of mCoefficient3.

gptshubham595 commented 1 year ago

I found this BeaconManager.setDistanceModelUpdateUrl let me try removing the motorola json values

gptshubham595 commented 1 year ago

It worked, but can you tell how we can get these three coefficient values for different phones ?

coz I know we can get distance via this formulae but for 2nd equation how we can get variables:

txpower = -40   
ratio =(txpower-rssi)/40
distance= math.pow(10,ratio)
print(distance)
davidgyoung commented 1 year ago

Yes, it sounds like the Motorola coefficients should be replaced in the library's built-in and web hosted device map file. As @RenisDudhat described above, there is a certain range of RSSI values input to the formula that will yield negative values and this should not happen.

There are instructions for calculating the constants in the last section of this page: https://altbeacon.github.io/android-beacon-library/distance-calculations.html

If you can take the measurements and come up with a set of coefficients that does not give negative values, we can replace the existing Motorola coefficients with a new one.

gptshubham595 commented 1 year ago

Hey, I tried these two formulas to get the values; if I get the C as -ve, I chose different formulae so that C should be positive or zero always

For Motorola device I got these values

Method 1

A B C
2.33 4.75 -1.85

Method 2 (C as 0)

A B C
1.60433254 5.33443408 0
davidgyoung commented 1 year ago

@gptshubham595 and @RenisDudhat can you do some testing with these values and see how they work on your Motorola devices in practice?

Method 2 (C as 0)

A B C
1.60433254 5.33443408 0

In order to properly put this into the config file we really need to know the actual values reported by android.os.Build for:

        Build.VERSION.RELEASE,
        Build.ID,
        Build.MODEL,
        Build.MANUFACTURER

The CSV file relies on those actual values for matching purposes.

You can use this URL to configure the library with this equation for initial testing: https://android-beacon-library.s3.amazonaws.com/android-distance2.json

I made it the first equation for Motorola which should match that for your device if you device reports it Build.MANUFACTURER as "Motorola"

gptshubham595 commented 1 year ago

I used these values and all worked perfectly fine

davidgyoung commented 1 year ago

@gptshubham595 can you please add code to read these values from your test phone? I can then use the results to add it to the library's built-in android-distance.json file:

    Build.VERSION.RELEASE,
    Build.ID,
    Build.MODEL,
    Build.MANUFACTURER
gptshubham595 commented 1 year ago

Yeah sure you can use these: I found out for Samsung, vivo, oppo, oneplus, moto, redmi

{
  "models":
  [
    {
      "coefficient1": 0.42093,
      "coefficient2": 6.9476,
      "coefficient3": 0.54992,
      "version":"4.4.2",
      "build_number":"KOT49H",
      "model":"Nexus 4",
      "manufacturer":"LGE"
    },
    {
      "coefficient1": 0.42093,
      "coefficient2": 6.9476,
      "coefficient3": 0.54992,
      "version":"4.4.2",
      "build_number":"LPV79",
      "model":"Nexus 5",
      "manufacturer":"LGE",
      "default": true
    },
    {
      "coefficient1": 0.9401940951,
      "coefficient2": 6.170094565,
      "coefficient3": 0.0,
      "version":"5.0.2",
      "build_number":"LXG22.67-7.1",
      "model":"Moto X Pro",
      "manufacturer":"XT1115",
      "default": false
    },
    {
      "coefficient1": 1.60433254,
      "coefficient2": 5.33443408,
      "coefficient3": 0,
      "version":"6.0",
      "build_number":"MPE24.49-18",
      "model":"XT1092",
      "manufacturer":"Motorola",
      "default": false
    },
    {
      "coefficient1": 2.65,
      "coefficient2": 3.91,
      "coefficient3": 1.61,
      "version":"12.0",
      "build_number":"SP1A.210812.016",
      "model":"CPH2401",
      "manufacturer":"Oneplus",
      "default": false
    },
    {
      "coefficient1": 0.000475,
      "coefficient2": 25.65,
      "coefficient3": 5.86,
      "version":"6.0",
      "build_number":"",
      "model":"",
      "manufacturer":"Google",
      "default": false
    },
    {
      "coefficient1": 0.102139,
      "coefficient2": 13.18,
      "coefficient3": 2.38,
      "version":"6.0",
      "build_number":"",
      "model":"",
      "manufacturer":"Oppo",
      "default": false
    },
    {
      "coefficient1":1.660582   ,
      "coefficient2": 6.05,
      "coefficient3": 0.67,
      "version":"6.0",
      "build_number":"",
      "model":"",
      "manufacturer":"Redmi",
      "default": false
    },
    {
      "coefficient1": 1.35070638    ,
      "coefficient2": 7.31428284,
      "coefficient3": 0,
      "version":"6.0",
      "build_number":"",
      "model":"",
      "manufacturer":"samsung",
      "default": false
    },
    {
      "coefficient1": 1.9504739,
      "coefficient2": 5.42879314,
      "coefficient3": 0,
      "version":"6.0",
      "build_number":"",
      "model":"",
      "manufacturer":"Vivo",
      "default": false
    }
  ]
}