指纹点定位是通过wifi室内定位的一种实现方法。其中重要的一部分是匹配算法。这里粘了部分我写的代码,算是做个记录吧!
程序实现了一个指纹点算法。功能简单,首先需要提供三个定位AP的信号强度,然后根据这三个信号强度,在指纹点数据表中查找。在查找过程中,查找的AP的信号强度是在一个范围内的,即给定的AP的信号强度,加减α。这个α是实验得出的,可以改变。对每一个AP,在数据库指纹点表格中查找,AP_NAME = AP,RSSI_AVG [β-α,β+α]。这样对每一个AP查找的结果可能有多个指纹点,将这些指纹点记录下来,并记录次数。经过三次查找之后,得到的指纹点的记录次数为3的为定位点。
程序实现为GetPosition.apk
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_get_position);
- service = new DbService(this);//实例化服务层DbService对象
-
- insert = (Button)findViewById(R.id.insert);
- position = (Button)findViewById(R.id.position);
- showfinger = (Button)findViewById(R.id.showfinger);
- pos_record = (Button)findViewById(R.id.pos_record);
-
- insert.setOnClickListener(this);
- insert.setContentDescription("insert button"); //用来判断哪个button按下
- position.setOnClickListener(this);
- position.setContentDescription("position button"); //用来判断哪个button按下
- showfinger.setOnClickListener(this);
- showfinger.setContentDescription("showfinger button"); //用来判断哪个button按下
- pos_record.setOnClickListener(this);
- pos_record.setContentDescription("pos_record button"); //用来判断哪个button按下
-
- //如果不存在指纹点表格就创建
- String sql = "SELECT name FROM sqlite_master WHERE type='table' AND name= 'fingerprint'" ;
- Cursor cs = service.scanSQL(sql);
- if(!(cs.moveToNext())){
- System.out.println("------there is no that table-------");
- //创建指纹点数据表格
- String sql1 = "CREATE TABLE fingerprint(_id INTEGER PRIMARY KEY,Position TEXT," +
- "AP_Name TEXT, RSSI_AVG INTEGER)";
- service.execsql(sql1);
- }
-
- //如果不存在位置记录表格就创建
- sql = "SELECT name FROM sqlite_master WHERE type='table' AND name= 'pos_result'" ;
- cs = service.scanSQL(sql);
- if(!(cs.moveToNext())){
- System.out.println("------there is no that table-------");
- //创建指纹点数据表格
- String sql1 = "CREATE TABLE pos_result(_id INTEGER PRIMARY KEY,Position TEXT," +
- "Pos_NO TEXT, Time TEXT)";
- service.execsql(sql1);
- }
-
- //初始化preferences和editor
- preferences = getSharedPreferences("PositionNum", MODE_PRIVATE);
- editor = preferences.edit();
-
- positions = new HashMap<String,Integer>(); //初始化positions
-
- handler = new Handler();
-
-
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_get_position, menu);
- return true;
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
-
- CharSequence str = v.getContentDescription();
- System.out.println(str);
- if(str.toString().equals("insert button")){ //如果是插入指纹点button按下
- System.out.println("insert button is down");
- dialogshow();
- }
-
- if(str.toString().equals("position button")){//如果是定位指纹点按下
- System.out.println("position button is down");
- getposition(); //调用定位方法来实现定位
- }
-
- if(str.toString().equals("showfinger button")){ //如果是查看指纹点button被按下
- System.out.println("showfinger button is down");
- showFingerPrint();
- }
-
- if(str.toString().equals("pos_record button")){ //查看路径记录
- System.out.println("pos_record button is down");
- //showFingerPrint();
- }
- }
- private void dialogshow(){ //添加指纹点的对话框
- View view = LayoutInflater.from(this).inflate(R.layout.insert_layout,null);
- ed1 = (EditText)view.findViewById(R.id.posname);
- ed2 = (EditText)view.findViewById(R.id.ap1);
- ed3 = (EditText)view.findViewById(R.id.ap2);
- ed4 = (EditText)view.findViewById(R.id.ap3);
-
-
-
- posnum = preferences.getInt("posNum", 0); //获取数据
- posnum++;
- if(posnum<10)
- PosName = "Position"+'0'+posnum;
- else
- PosName = "Position"+posnum;
- System.out.println(PosName);
- ed1.setText(PosName);
- ed1.clearFocus(); //失去焦点
- new AlertDialog.Builder(this)
- .setTitle("添加指纹点数据")
- .setIcon(android.R.drawable.ic_menu_manage)
- .setView(view)
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- editor.putInt("posNum", posnum); //将修改后的posnum存起来
- editor.commit(); //提交修改
-
- // 构建ContentValues对象,并将要插入的数据以键值对的形势进行保存
- ContentValues contact = new ContentValues();
-
- int rssi_avg;
- contact.put("Position", PosName);
- contact.put("AP_Name", "AP001");
- Editable a = ed2.getText();
- System.out.println(a.toString());
- rssi_avg = Integer.valueOf(a.toString());
- contact.put("RSSI_AVG", rssi_avg);
- long ret = service.InsertInfo(contact,"fingerprint");//调用service的方法完成添加操作
- if(ret== -1){
- System.out.println("添加"+"Position_Result"+"表格失败");
- }
-
- contact.put("Position", PosName);
- contact.put("AP_Name", "AP002");
- a = ed3.getText();
- System.out.println(a.toString());
- rssi_avg = Integer.valueOf(a.toString());
- contact.put("RSSI_AVG", rssi_avg);
- ret = service.InsertInfo(contact,"fingerprint");//调用service的方法完成添加操作
- if(ret== -1){
- System.out.println("添加"+"Position_Result"+"表格失败");
- }
-
- contact.put("Position", PosName);
- contact.put("AP_Name", "AP003");
- a = ed4.getText();
- System.out.println(a.toString());
- rssi_avg = Integer.valueOf(a.toString());
- contact.put("RSSI_AVG", rssi_avg);
- ret = service.InsertInfo(contact,"fingerprint");//调用service的方法完成添加操作
- if(ret== -1){
- System.out.println("添加"+"Position_Result"+"表格失败");
- }
-
- }
- })
- .setNegativeButton("取消", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
-
- }
- })
- .create().show();
- }
-
- public void showFingerPrint(){ //显示指纹点
- LinearLayout linearlayout = new LinearLayout(this);
- linearlayout.setOrientation(LinearLayout.VERTICAL);
-
- ListView lv = new ListView(this);
- View view = LayoutInflater.from(this).inflate(R.layout.fingerprint_title,null);
- lv.addHeaderView(view);
-
- Cursor c = service.getContacts("fingerprint");//调用服务层的方法获取所有存放的联系人数据
-
- //使用返回的游标Cursor对象构建ListView的数据适配器
- SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.fingerprint_item,c,
- new String[]{"Position","AP_Name","RSSI_AVG"},
- new int[]{R.id.print_posname,R.id.print_ap,R.id.print_level});
-
- lv.setAdapter(adapter); //设置ListView的数据适配器
- linearlayout.removeAllViews();
- linearlayout.addView(lv);
-
- new AlertDialog.Builder(this)
- .setTitle("添加指纹点数据")
- .setIcon(android.R.drawable.ic_menu_manage)
- .setView(linearlayout)
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
-
- }
- })
- .create().show();
- }
- public void getposition(){
-
-
- /////////////////////////////////////////////////
- View view = LayoutInflater.from(this).inflate(R.layout.getposition,null);
- ed5 = (EditText)view.findViewById(R.id.getap1);
- ed6 = (EditText)view.findViewById(R.id.getap2);
- ed7 = (EditText)view.findViewById(R.id.getap3);
-
- new AlertDialog.Builder(this)
- .setTitle("输入实测AP数据")
- .setIcon(android.R.drawable.ic_menu_manage)
- .setView(view)
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- new getpositionThread().start();
- }
- })
- .create().show();
- }
-
- /**
- *下面的方法用来实现定位算法
- *定位AP的信号强度是通过对话框输入的
- *对每一个AP的查找范围是±2
- *算法是先根据每个AP的信号强度,在设定的±2范围内进行查找,找到的指纹点进行计数。计数最多的,为指纹点
- *方法先对第一个AP进行查找,找到position存放在map中
- *然后对第二个进行查找,将结果存放在map中,如果已经存在就加1,如果不存在就添加
- *然后对第三个,第四个进行相同的操作。
- *最后,map中计数最大的pos就对应定位点
- **/
- public class getpositionThread extends Thread{
- public void run(){
- String str;
- Editable level = ed5.getText();
- System.out.println(level.toString());
- String sql = "SELECT Position FROM fingerprint WHERE RSSI_AVG BETWEEN "+level
- +"-2 AND "+level+"+2 AND AP_Name = 'AP001'";
- System.out.println(sql);
- Cursor cs = service.scanSQL(sql);
- System.out.println("the number is :"+ cs.getCount());
- while(cs.moveToNext()){
- str = cs.getString(0);
- System.out.println(str);
- positions.put(str, 1); //对第一个定位AP进行添加操作
- }
-
- //对第二个定位AP进行处理
- level = ed6.getText();
-
- sql = "SELECT Position FROM fingerprint WHERE RSSI_AVG BETWEEN "+level
- +"-2 AND "+level+"+2 AND AP_Name = 'AP002'";
- System.out.println(sql);
- cs = service.scanSQL(sql);
- System.out.println("the number is :"+ cs.getCount());
- while(cs.moveToNext()){
- str = cs.getString(0);
- System.out.println(str);
- //positions.put(str, positions.get(str)+1);
- if(positions.get(str) == null){
- positions.put(str, 1);
- }else
- positions.put(str, positions.get(str)+1);
-
- }
-
- //对第三个定位AP进行处理
- level = ed7.getText();
-
- sql = "SELECT Position FROM fingerprint WHERE RSSI_AVG BETWEEN "+level
- +"-2 AND "+level+"+2 AND AP_Name = 'AP003'";
- System.out.println(sql);
- cs = service.scanSQL(sql);
- System.out.println("the number is :"+ cs.getCount());
- while(cs.moveToNext()){
- str = cs.getString(0);
- System.out.println(str);
- if(positions.get(str) == null){
- positions.put(str, 1);
- }else
- positions.put(str, positions.get(str)+1);
-
- }
-
- handler.post(new Runnable01());
-
- }
- }
-
- //handler.post方法调用之后执行
- public class Runnable01 implements Runnable{
- @Override
- public void run() {
- // TODO Auto-generated method stub
- position_result = new Bundle();
-
- String result =new String();
- System.out.println("the number of positions is :"+positions.size());
- Object[] array = positions.keySet().toArray();
- for(int i = 0;i< positions.size();i++){
- System.out.println("指纹点的权值记录结果为:"+array[i]+" : "+positions.get(array[i]));
- result += "指纹点的权值记录结果为:"+array[i]+" : "+positions.get(array[i])+"\n";
-
- }
- result += "--------------------------------------------\n";
- result += "the result is : \n";
-
- String str;
- if(positions.containsValue(3)){
- for(int i = 0;i< positions.size();i++){
- str = (String)array[i];
- if(positions.get(str)==3){
- System.out.println("指纹点的权值记录结果为:"+str+" : 3");
- result +="指纹点"+str+"的权值为: 3\n";
- position_num++;
- position_result.putInt("number", position_num);
- position_result.putString(" "+position_num, str);
- }
- }
- }else{
- if(positions.containsValue(2) ){
- for(int i = 0;i< positions.size();i++){
- str = (String)array[i];
- if(positions.get(array[i])==2){
- System.out.println("指纹点的权值记录结果为:"+str+" : 2");
- result +="指纹点"+array[i]+"的权值为: 2\n";
- position_num++;
- position_result.putInt("number", position_num);
- position_result.putString(" "+position_num, str);
- }
- }
- }else
- result += "未找到指纹点";
- }
-
- result += "the sum is :" +position_result.getInt("number");
-
- TextView tv = new TextView(GetPosition.this);
- tv.setText(result);
- ScrollView scrollview = new ScrollView(GetPosition.this);
- scrollview.addView(tv);
- new AlertDialog.Builder(GetPosition.this)
- .setTitle("添加指纹点数据")
- .setIcon(android.R.drawable.ic_menu_manage)
- .setMessage("AP001 :" +ed5.getText() +" ; AP002 :"+ed6.getText()+" ; AP003 :"+ed7.getText()+"\n")
- .setView(scrollview)
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- position_result.clear();
- position_num =0;
- positions.clear();
- }
- })
- .create().show();
-
- }
- }
程序里面用到了数据库方面的知识。在事件监听方面我用了Activity本身作为事件监听器,在响应方法里面判断是那个Button产生的事件,这种实现方式,感觉不怎么好。呵呵,代码就贴这些吧
阅读(339) | 评论(0) | 转发(0) |