Home > OS >  Bluetooth LE - ScanResult returns the same timestamp for the same device
Bluetooth LE - ScanResult returns the same timestamp for the same device

Time:10-12

I want to get the advertisement milliseconds of the devices by subtracting the two timestamps of the same device. But both timestamps are the same value. Could someone help?

 override fun onScanResult(callbackType: Int, result: ScanResult) {
    
               val timeStampFirst = result.timestampNanos
    
                val tag = deviceMap.computeIfAbsent(result.device.address) {
                    val newTag = BleTag(result.device.name ?: "Unbekannt", result.device.address, result.rssi , result.scanRecord?.bytes, result.timestampNanos,"0 Sek.")
                    deviceList.add(newTag)
                    newTag
                }
                tag.name = result.device.name ?: "Unbekannt"
                tag.rssi = result.rssi
                tag.advertisementData = result.scanRecord?.bytes
                tag.timeStampFirst = result.timestampNanos
    
                deviceList.forEach {
                    if(it.mac == result.device.address){
                       val timeStampSecond = result.timestampNanos
               val advertisementMillis = timeStampSecond - timeStampFirst
                       Log.e("Advertisement ms", advertisementMillis)   
                    }
                }
    
    
                Log.e(resultTime.toString(),"")
    
                deviceList.sortBy {
                    result.rssi
                }
    
                recyclerView.adapter?.notifyDataSetChanged()
                //menu.findItem(R.id.count).title = "Geräte: "   deviceList.size
    
                super.onScanResult(callbackType, result)
            }

     

CodePudding user response:

To show the problem and the solution I have commented out all source code that is not necessary for the calculation of the time interval between two consecutive advertisement.

 override fun onScanResult(callbackType: Int, result: ScanResult) {
               val timeStampFirst = result.timestampNanos
                // val tag = deviceMap.computeIfAbsent(result.device.address) {
                //    val newTag = BleTag(result.device.name ?: "Unbekannt", result.device.address, result.rssi , result.scanRecord?.bytes, result.timestampNanos,"0 Sek.")
                //    deviceList.add(newTag)
                //    newTag
                // }
                // tag.name = result.device.name ?: "Unbekannt"
                // tag.rssi = result.rssi
                // tag.advertisementData = result.scanRecord?.bytes
                // tag.timeStampFirst = result.timestampNanos
    
                // deviceList.forEach {
                //     if(it.mac == result.device.address){
                       val timeStampSecond = result.timestampNanos
                // will always be ZERO
               val advertisementMillis = timeStampSecond - timeStampFirst
                       Log.e("Advertisement ms", advertisementMillis)
                //     }
                // }
    
    
                // Log.e(resultTime.toString(),"")
    
                // deviceList.sortBy {
                //     result.rssi
                // }
    
                // recyclerView.adapter?.notifyDataSetChanged()
                //menu.findItem(R.id.count).title = "Geräte: "   deviceList.size
    
                // super.onScanResult(callbackType, result)
            }

As you use for both timestamps the value of the current advertisement the result will always be 0.

But the object from the deviceList does not help either, because it already contains the data from the current advertisement.

In pseudo code you would have to do the following to find the interval between two consecutive advertisement:

onNewAdvertismentReceived
    If Broadcaster seen before
        Retrieve timestamp from the advertisment beforehand
        Calculate the interval from both timestamps(old,new)
    Else
        First time Broadcaster seen
        Not possible to calculate interval
    Fi
  • Related