Android——蓝牙利用RSSI进行距离测算

 From:http://blog.csdn.net/lhc1105/article/details/54585632

算法:

[java]  view plain  copy
 
  1. /** 
  2.  * 功能:根据rssi计算距离 
  3.  * Created by liuhuichao on 2017/1/17. 
  4.  */  
  5.   
  6. public class RssiUtil {  
  7.     //A和n的值,需要根据实际环境进行检测得出  
  8.     private static final double A_Value=50;/**A - 发射端和接收端相隔1米时的信号强度*/  
  9.     private static final double n_Value=2.5;/** n - 环境衰减因子*/  
  10.   
  11.     /** 
  12.      * 根据Rssi获得返回的距离,返回数据单位为m 
  13.      * @param rssi 
  14.      * @return 
  15.      */  
  16.     public static double getDistance(int rssi){  
  17.         int iRssi = Math.abs(rssi);  
  18.         double power = (iRssi-A_Value)/(10*n_Value);  
  19.         return Math.pow(10,power);  
  20.     }  
  21. }  

扫描蓝牙过程中获得信号强度:

[java]  view plain  copy
 
  1. /*监听扫描过程中的变化*/  
  2.    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {  
  3.        @Override  
  4.        public void onReceive(Context context, Intent intent) {  
  5.            String action = intent.getAction();  
  6.            // When discovery finds a device  
  7.            if (BluetoothDevice.ACTION_FOUND.equals(action))  
  8.            {  
  9.                // Get the BluetoothDevice object from the Intent  
  10.                // 通过EXTRA_DEVICE附加域来得到一个BluetoothDevice设备  
  11.                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
  12.   
  13.   
  14.   
  15.                // If it's already paired, skip it, because it's been listed already  
  16.                // 如果这个设备是不曾配对过的,添加到list列表  
  17.               /* if (device.getBondState() != BluetoothDevice.BOND_BONDED) 
  18.                {*/  
  19.                    //信号强度  
  20.                    int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);  
显示时候再根据距离做个排序:
[java]  view plain  copy
 
  1. Map map=new HashMap();  
  2.                    map.put("deviceName",deviceName);  
  3.                    map.put("deviceAddress",deviceAddress);  
  4.                    map.put("bluetooth_status",distance);  
  5.                    deviceList.add(map);  
  6.                    Collections.sort(deviceList,new Comparator() {  
  7.                        public int compare(Object a, Object b) {  
  8.                            Map one=(HashMap)a;  
  9.                            Map two=(HashMap)b;  
  10.                            if((((double)one.get("bluetooth_status"))-((double)two.get("bluetooth_status")))>0){  
  11.                                return 1;  
  12.                            }else if((((double)one.get("bluetooth_status"))-((double)two.get("bluetooth_status")))==0){  
  13.                                return 0;  
  14.                            }else{  
  15.                                return -1;  
  16.                            }  
  17.   
  18.   
  19.                        }  
  20.                    });  
  21.                   listItemAdapter.notifyDataSetChanged();  

你可能感兴趣的:(04,Android有关)