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

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

算法

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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
  在CODE上查看代码片 派生到我的代码片
  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
  在CODE上查看代码片 派生到我的代码片
  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();  

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值