This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

HOW TO GET ADVERTISING INTERVAL FROM ANDROID APP

Hi,

I'm developing an Android App. I want to measure and display advertising interval of any scanned peripherals like NRF Connect does.

I researched Android BLE Scanner Compat library. And I found out, there is one function in Android library helps me do that:  

getPeriodicAdvertisingInterval 

However, it requires Android 8.0 or above

I need to have this function on Android 6.0

Looking for your help.

Thank you

  • Hello!

    Periodic advertising interval is something very different. It's a new feature in Bluetooth 5. You may read more about it in the link here: https://devzone.nordicsemi.com/b/blog/posts/bluetooth-5-advertisements-extended-and-periodic-a

    To calculate the approximate device advertising interval in nRF Connect we take the timestampNanos value and compare it with a previous one, from the same device, first comparing the advertising payload (nRF Connect calculates intervals for each payload separately). Then we use a bit complicated formula to get the approximation:

    /**
    	 * Sets the advertising interval for this kind of packets if the current one is higher than the new one.
    	 * @param intervalNanos the interval between packets.
    	 */
    	public void setIntervalIfLower(final long intervalNanos) {
    		if (intervalNanos <= 0L)
    			return;
    
    		if (this.intervalNanos == 0L)
    			this.intervalNanos = intervalNanos;
    		else if (intervalNanos < this.intervalNanos * 0.7 && count < 10)
    			this.intervalNanos = intervalNanos;
    		else if (intervalNanos < this.intervalNanos + 3000000) {
    			final int limitedCount = Math.min(count, 10);
    			this.intervalNanos = (((this.intervalNanos * (limitedCount - 1) + intervalNanos)) / limitedCount);
    		} else if (intervalNanos < this.intervalNanos * 1.4) {
    			this.intervalNanos = (((this.intervalNanos * (29) + intervalNanos)) / 30);
    		}
    	}

    *count* is the total number of packets received from this device.

    The logic behind this formula is as follows:

    1. If the advertising interval has not been set before, set it to the timestamp difference. This is when the second packet was received from the device (at least 2 are required to calculate interval).

    2. If the new interval is lower then 0.7 of one calculated before, and we don't have many packets yet, use this one. We must have lost some of the packets. A shorter interval means, that the device in fact advertises faster, but some packets were not received.

    3. If the new interval is no more then 3 ms longer than the calculated one, use weight averaging.

    4. If the new interval is longer then 1.4 times the old one, also use weight averaging, but make the old one reeeealy strong (29 times more important).

    5. Ignore intervals longer than that. Longer interval means that some packets were just lost, but if the device used to advertise faster, it still probably does.

    As you see, it's very approximate approximation and may not work for all devices. For example, fails when the device changes frequency, changes payload, etc. The algorithm deserves updating, it was created years ago, but somehow works, and there are not many complains about it.

    Hope that helped!

    BR, Aleksander

  • Thanks so much for you help.

    I'll implement this, if I have further concern, I will ask then

  • I am doing research on iOS about this too.
    But I can not find any similar method to get time stamp on iOS BLE lib 

    - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral
         advertisementData:(NSDictionary *)advertisementData
                      RSSI:(NSNumber *)RSSI
    {
        // Can I get time stamp or advertising interval here??
        // My App run on iOS 11 
    }

    Thank you

  • I guess you may just get the current timestamp in this didDiscoverPeripheral method.

       let timestamp = Date().timeIntervalSince1970

Related