如何不断获得RSSI无需连接BLE设备? [英] How to continuously get RSSI without connecting to the BLE device?

查看:1564
本文介绍了如何不断获得RSSI无需连接BLE设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要不断地得到一个蓝牙设备的RSSI值做出一些距离RSSI逼近,无需连接。但是,回调方法 BluetoothAdapter 得到RSSI只有一次,当设备扫描。

My application needs to constantly get RSSI value of a bluetooth device to make some distance-rssi approximation, without connecting. However, callback method of the BluetoothAdapter gets the RSSI only once when the device scanned.

// Device scan callback
private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
        runOnUiThread(new Runnable() {
            public void run() {
                ...
            }
        });     
    }   
};

作为一个解决方案,我创建了两个的Runnable 的对象。一个用于启动,另一种是用于停止扫描处理。这两个对象不断打电话给对方,直到我得到我想要的RSSI值。之后,我的处理程序从消息队列中删除。

As a solution, I created two Runnable objects. One is for starting, other is for stopping scan process. These two objects call each other continuously until I get my desired RSSI value. After that my Handler removes them from message queue.

private Runnable startScan = new Runnable() {       
    @Override
    public void run() {
        bluetoothAdapter.startLeScan(leScanCallback);
        scanHandler.postDelayed(stopScan, SCAN_INTERVAL);
    }               
};

private Runnable stopScan = new Runnable() {        
    @Override
    public void run() {         
        bluetoothAdapter.stopLeScan(leScanCallback);                                            
        scanHandler.postDelayed(startScan, STOP_INTERVAL);          
    }
};

这似乎是工作,但我想知道是否有更可行的办法来做到这一点,而无需连接。

This seems to be working but I want to know if there is more viable option to do this without connecting.

推荐答案

如果该BLE灯塔是广告,而不是连接,onLeScan被称为continously。 如果BLE信标可连接,特征取决于设备

If the ble beacon is advertising and not connectable, onLeScan is called continously. If the ble beacon is connectable, the feature depends on the device.

这篇关于如何不断获得RSSI无需连接BLE设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆