Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1760404
  • 博文数量: 1493
  • 博客积分: 38
  • 博客等级: 民兵
  • 技术积分: 5834
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-19 17:28
文章分类

全部博文(1493)

文章存档

2016年(11)

2015年(38)

2014年(137)

2013年(253)

2012年(1054)

2011年(1)

分类:

2012-12-14 14:03:13

原文地址:指纹点定位其一 作者:做早起的鸟

指纹点定位是通过wifi室内定位的一种实现方法。其中重要的一部分是匹配算法。这里粘了部分我写的代码,算是做个记录吧!

程序实现了一个指纹点算法。功能简单,首先需要提供三个定位AP的信号强度,然后根据这三个信号强度,在指纹点数据表中查找。在查找过程中,查找的AP的信号强度是在一个范围内的,即给定的AP的信号强度,加减α。这个α是实验得出的,可以改变。对每一个AP,在数据库指纹点表格中查找,AP_NAME = APRSSI_AVG [β-α,β+α]。这样对每一个AP查找的结果可能有多个指纹点,将这些指纹点记录下来,并记录次数。经过三次查找之后,得到的指纹点的记录次数为3的为定位点。

程序实现为GetPosition.apk


 

点击(此处)折叠或打开

  1. @Override
  2.      public void onCreate(Bundle savedInstanceState) {
  3.         super.onCreate(savedInstanceState);
  4.         setContentView(R.layout.activity_get_position);
  5.         service = new DbService(this);//实例化服务层DbService对象

  6.         
  7.         insert = (Button)findViewById(R.id.insert);
  8.         position = (Button)findViewById(R.id.position);
  9.         showfinger = (Button)findViewById(R.id.showfinger);
  10.         pos_record = (Button)findViewById(R.id.pos_record);
  11.         
  12.         insert.setOnClickListener(this);
  13.         insert.setContentDescription("insert button"); //用来判断哪个button按下

  14.         position.setOnClickListener(this);
  15.         position.setContentDescription("position button"); //用来判断哪个button按下

  16.         showfinger.setOnClickListener(this);
  17.         showfinger.setContentDescription("showfinger button"); //用来判断哪个button按下

  18.         pos_record.setOnClickListener(this);
  19.         pos_record.setContentDescription("pos_record button"); //用来判断哪个button按下

  20.         
  21.         //如果不存在指纹点表格就创建        

  22.         String sql = "SELECT name FROM sqlite_master WHERE type='table' AND name= 'fingerprint'" ;
  23.         Cursor cs = service.scanSQL(sql);
  24.         if(!(cs.moveToNext())){
  25.             System.out.println("------there is no that table-------");
  26.             //创建指纹点数据表格

  27.             String sql1 = "CREATE TABLE fingerprint(_id INTEGER PRIMARY KEY,Position TEXT," +
  28.                     "AP_Name TEXT, RSSI_AVG INTEGER)";
  29.             service.execsql(sql1);
  30.         }
  31.         
  32.         //如果不存在位置记录表格就创建        

  33.         sql = "SELECT name FROM sqlite_master WHERE type='table' AND name= 'pos_result'" ;
  34.         cs = service.scanSQL(sql);
  35.         if(!(cs.moveToNext())){
  36.             System.out.println("------there is no that table-------");
  37.             //创建指纹点数据表格

  38.             String sql1 = "CREATE TABLE pos_result(_id INTEGER PRIMARY KEY,Position TEXT," +
  39.                     "Pos_NO TEXT, Time TEXT)";
  40.             service.execsql(sql1);
  41.         }
  42.         
  43.         //初始化preferences和editor

  44.         preferences = getSharedPreferences("PositionNum", MODE_PRIVATE);
  45.         editor = preferences.edit();
  46.                 
  47.         positions = new HashMap<String,Integer>(); //初始化positions

  48.         
  49.         handler = new Handler();
  50.         
  51.         
  52.     }

  53.     @Override
  54.     public boolean onCreateOptionsMenu(Menu menu) {
  55.         getMenuInflater().inflate(R.menu.activity_get_position, menu);
  56.         return true;
  57.     }

  58.     @Override
  59.     public void onClick(View v) {
  60.         // TODO Auto-generated method stub

  61.         
  62.         CharSequence str = v.getContentDescription();
  63.         System.out.println(str);
  64.         if(str.toString().equals("insert button")){ //如果是插入指纹点button按下

  65.             System.out.println("insert button is down");            
  66.             dialogshow();                                                                                                                        
  67.         }
  68.         
  69.         if(str.toString().equals("position button")){//如果是定位指纹点按下

  70.             System.out.println("position button is down");
  71.             getposition(); //调用定位方法来实现定位

  72.         }
  73.         
  74.         if(str.toString().equals("showfinger button")){ //如果是查看指纹点button被按下

  75.             System.out.println("showfinger button is down");
  76.             showFingerPrint();
  77.         }
  78.         
  79.         if(str.toString().equals("pos_record button")){ //查看路径记录

  80.             System.out.println("pos_record button is down");
  81.             //showFingerPrint();

  82.         }
  83.     }


  84.     private void dialogshow(){ //添加指纹点的对话框

  85.         View view = LayoutInflater.from(this).inflate(R.layout.insert_layout,null);
  86.         ed1 = (EditText)view.findViewById(R.id.posname);
  87.         ed2 = (EditText)view.findViewById(R.id.ap1);
  88.         ed3 = (EditText)view.findViewById(R.id.ap2);
  89.         ed4 = (EditText)view.findViewById(R.id.ap3);
  90.         
  91.         
  92.         
  93.         posnum = preferences.getInt("posNum", 0);    //获取数据

  94.         posnum++;
  95.         if(posnum<10)
  96.             PosName = "Position"+'0'+posnum;        
  97.         else
  98.             PosName = "Position"+posnum;
  99.         System.out.println(PosName);
  100.         ed1.setText(PosName);
  101.         ed1.clearFocus();        //失去焦点


  102.         new AlertDialog.Builder(this)
  103.             .setTitle("添加指纹点数据")
  104.             .setIcon(android.R.drawable.ic_menu_manage)
  105.             .setView(view)
  106.             .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  107.                 
  108.                 @Override
  109.                 public void onClick(DialogInterface dialog, int which) {
  110.                     // TODO Auto-generated method stub

  111.                     editor.putInt("posNum", posnum); //将修改后的posnum存起来

  112.                     editor.commit();        //提交修改                        

  113.                     
  114.                     // 构建ContentValues对象,并将要插入的数据以键值对的形势进行保存

  115.                     ContentValues contact = new ContentValues();
  116.                                                             
  117.                     int rssi_avg;
  118.                     contact.put("Position", PosName);
  119.                     contact.put("AP_Name", "AP001");
  120.                     Editable a = ed2.getText();
  121.                     System.out.println(a.toString());
  122.                     rssi_avg = Integer.valueOf(a.toString());
  123.                     contact.put("RSSI_AVG", rssi_avg);
  124.                     long ret = service.InsertInfo(contact,"fingerprint");//调用service的方法完成添加操作            

  125.                     if(ret== -1){
  126.                         System.out.println("添加"+"Position_Result"+"表格失败");
  127.                     }
  128.                     
  129.                     contact.put("Position", PosName);
  130.                     contact.put("AP_Name", "AP002");
  131.                     a = ed3.getText();
  132.                     System.out.println(a.toString());
  133.                     rssi_avg = Integer.valueOf(a.toString());
  134.                     contact.put("RSSI_AVG", rssi_avg);
  135.                     ret = service.InsertInfo(contact,"fingerprint");//调用service的方法完成添加操作            

  136.                     if(ret== -1){
  137.                         System.out.println("添加"+"Position_Result"+"表格失败");
  138.                     }
  139.                     
  140.                     contact.put("Position", PosName);
  141.                     contact.put("AP_Name", "AP003");
  142.                     a = ed4.getText();
  143.                     System.out.println(a.toString());
  144.                     rssi_avg = Integer.valueOf(a.toString());
  145.                     contact.put("RSSI_AVG", rssi_avg);
  146.                     ret = service.InsertInfo(contact,"fingerprint");//调用service的方法完成添加操作            

  147.                     if(ret== -1){
  148.                         System.out.println("添加"+"Position_Result"+"表格失败");
  149.                     }
  150.                     
  151.                 }
  152.             })
  153.             .setNegativeButton("取消", new DialogInterface.OnClickListener() {
  154.                 
  155.                 @Override
  156.                 public void onClick(DialogInterface dialog, int which) {
  157.                     // TODO Auto-generated method stub

  158.                     
  159.                 }
  160.             })
  161.             .create().show();    
  162.     }
  163.     
  164.     public void showFingerPrint(){        //显示指纹点

  165.         LinearLayout linearlayout = new LinearLayout(this);
  166.         linearlayout.setOrientation(LinearLayout.VERTICAL);
  167.         
  168.         ListView lv = new ListView(this);
  169.         View view = LayoutInflater.from(this).inflate(R.layout.fingerprint_title,null);
  170.         lv.addHeaderView(view);
  171.         
  172.         Cursor c = service.getContacts("fingerprint");//调用服务层的方法获取所有存放的联系人数据

  173.         
  174.         //使用返回的游标Cursor对象构建ListView的数据适配器

  175.         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.fingerprint_item,c,
  176.                 new String[]{"Position","AP_Name","RSSI_AVG"},
  177.                     new int[]{R.id.print_posname,R.id.print_ap,R.id.print_level});
  178.                  
  179.         lv.setAdapter(adapter);                 //设置ListView的数据适配器

  180.         linearlayout.removeAllViews();
  181.         linearlayout.addView(lv);
  182.         
  183.         new AlertDialog.Builder(this)
  184.             .setTitle("添加指纹点数据")
  185.             .setIcon(android.R.drawable.ic_menu_manage)
  186.             .setView(linearlayout)
  187.             .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  188.                 
  189.                 @Override
  190.                 public void onClick(DialogInterface dialog, int which) {
  191.                     // TODO Auto-generated method stub

  192.                                         
  193.                 }
  194.             })
  195.             .create().show();    
  196.     }


  197.     public void getposition(){
  198.         
  199.                                 
  200.         /////////////////////////////////////////////////

  201.         View view = LayoutInflater.from(this).inflate(R.layout.getposition,null);
  202.         ed5 = (EditText)view.findViewById(R.id.getap1);
  203.         ed6 = (EditText)view.findViewById(R.id.getap2);
  204.         ed7 = (EditText)view.findViewById(R.id.getap3);
  205.                 
  206.         new AlertDialog.Builder(this)
  207.             .setTitle("输入实测AP数据")
  208.             .setIcon(android.R.drawable.ic_menu_manage)
  209.             .setView(view)
  210.             .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  211.                                 
  212.                 @Override
  213.                 public void onClick(DialogInterface dialog, int which) {
  214.                     // TODO Auto-generated method stub

  215.                     new getpositionThread().start();
  216.                 }
  217.             })
  218.             .create().show();                    
  219.     }
  220.     
  221.     /**
  222.     *下面的方法用来实现定位算法
  223.     *定位AP的信号强度是通过对话框输入的
  224.     *对每一个AP的查找范围是±2
  225.     *算法是先根据每个AP的信号强度,在设定的±2范围内进行查找,找到的指纹点进行计数。计数最多的,为指纹点
  226.     *方法先对第一个AP进行查找,找到position存放在map中
  227.     *然后对第二个进行查找,将结果存放在map中,如果已经存在就加1,如果不存在就添加
  228.     *然后对第三个,第四个进行相同的操作。
  229.     *最后,map中计数最大的pos就对应定位点
  230.     **/
  231.     public class getpositionThread extends Thread{
  232.         public void run(){
  233.             String str;
  234.             Editable level = ed5.getText();
  235.             System.out.println(level.toString());

  236.             String sql = "SELECT Position FROM fingerprint WHERE RSSI_AVG BETWEEN "+level
  237.             +"-2 AND "+level+"+2 AND AP_Name = 'AP001'";
  238.             System.out.println(sql);
  239.             Cursor cs = service.scanSQL(sql);
  240.             System.out.println("the number is :"+ cs.getCount());
  241.             while(cs.moveToNext()){
  242.                  str = cs.getString(0);
  243.                 System.out.println(str);
  244.                 positions.put(str, 1); //对第一个定位AP进行添加操作


  245.             }
  246.             
  247.             //对第二个定位AP进行处理

  248.             level = ed6.getText();
  249.             
  250.             sql = "SELECT Position FROM fingerprint WHERE RSSI_AVG BETWEEN "+level
  251.             +"-2 AND "+level+"+2 AND AP_Name = 'AP002'";
  252.             System.out.println(sql);
  253.             cs = service.scanSQL(sql);
  254.             System.out.println("the number is :"+ cs.getCount());
  255.             while(cs.moveToNext()){
  256.                 str = cs.getString(0);
  257.                 System.out.println(str);
  258.                 //positions.put(str, positions.get(str)+1);

  259.                 if(positions.get(str) == null){
  260.                     positions.put(str, 1);
  261.                 }else
  262.                     positions.put(str, positions.get(str)+1);
  263.                                                                                         
  264.             }
  265.             
  266.             //对第三个定位AP进行处理

  267.             level = ed7.getText();
  268.             
  269.             sql = "SELECT Position FROM fingerprint WHERE RSSI_AVG BETWEEN "+level
  270.             +"-2 AND "+level+"+2 AND AP_Name = 'AP003'";
  271.             System.out.println(sql);
  272.             cs = service.scanSQL(sql);
  273.             System.out.println("the number is :"+ cs.getCount());
  274.             while(cs.moveToNext()){
  275.                 str = cs.getString(0);
  276.                 System.out.println(str);
  277.                 if(positions.get(str) == null){
  278.                     positions.put(str, 1);
  279.                 }else
  280.                     positions.put(str, positions.get(str)+1);
  281.                                                                                         
  282.             }
  283.             
  284.             handler.post(new Runnable01());
  285.             
  286.         }
  287.     }
  288.     
  289.     //handler.post方法调用之后执行

  290.     public class Runnable01 implements Runnable{

  291.         @Override
  292.         public void run() {
  293.             // TODO Auto-generated method stub

  294.             position_result = new Bundle();
  295.             
  296.             String result =new String();
  297.             System.out.println("the number of positions is :"+positions.size());
  298.             Object[] array = positions.keySet().toArray();
  299.             for(int i = 0;i< positions.size();i++){
  300.                 System.out.println("指纹点的权值记录结果为:"+array[i]+" : "+positions.get(array[i]));
  301.                 result += "指纹点的权值记录结果为:"+array[i]+" : "+positions.get(array[i])+"\n";
  302.                 
  303.             }
  304.             result += "--------------------------------------------\n";
  305.             result += "the result is : \n";
  306.             
  307.             String str;
  308.             if(positions.containsValue(3)){                
  309.                 for(int i = 0;i< positions.size();i++){
  310.                     str = (String)array[i];
  311.                     if(positions.get(str)==3){
  312.                         System.out.println("指纹点的权值记录结果为:"+str+" : 3");
  313.                         result +="指纹点"+str+"的权值为: 3\n";
  314.                         position_num++;
  315.                         position_result.putInt("number", position_num);
  316.                         position_result.putString(" "+position_num, str);
  317.                     }
  318.                 }
  319.             }else{
  320.                 if(positions.containsValue(2) ){                    
  321.                     for(int i = 0;i< positions.size();i++){
  322.                         str = (String)array[i];
  323.                         if(positions.get(array[i])==2){
  324.                             System.out.println("指纹点的权值记录结果为:"+str+" : 2");
  325.                             result +="指纹点"+array[i]+"的权值为: 2\n";
  326.                             position_num++;
  327.                             position_result.putInt("number", position_num);
  328.                             position_result.putString(" "+position_num, str);
  329.                         }
  330.                     }
  331.                 }else
  332.                     result += "未找到指纹点";
  333.             }
  334.                                                 
  335.             result += "the sum is :" +position_result.getInt("number");

  336.                                     
  337.             TextView tv = new TextView(GetPosition.this);
  338.             tv.setText(result);    
  339.             ScrollView scrollview = new ScrollView(GetPosition.this);
  340.             scrollview.addView(tv);
  341.             new AlertDialog.Builder(GetPosition.this)
  342.             .setTitle("添加指纹点数据")
  343.             .setIcon(android.R.drawable.ic_menu_manage)
  344.             .setMessage("AP001 :" +ed5.getText() +" ; AP002 :"+ed6.getText()+" ; AP003 :"+ed7.getText()+"\n")
  345.             .setView(scrollview)
  346.             .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  347.                 
  348.                 @Override
  349.                 public void onClick(DialogInterface dialog, int which) {
  350.                     // TODO Auto-generated method stub

  351.                     position_result.clear();
  352.                     position_num =0;
  353.                     positions.clear();
  354.                 }
  355.             })
  356.             .create().show();
  357.             
  358.         }        
  359.     }

程序里面用到了数据库方面的知识。在事件监听方面我用了Activity本身作为事件监听器,在响应方法里面判断是那个Button产生的事件,这种实现方式,感觉不怎么好。呵呵,代码就贴这些吧

 

阅读(339) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~