指纹定位蓝牙打点工具

  最近在研究指纹定位,实验室只有自己在做这个方向,只好自己写了一个IBeacon蓝牙指纹打点工具。不是专门搞Android的,代码有点粗糙。使用的时候要先把IBeacon的minor设置成0-5。如果蓝牙节点比较多自己修改下代码。输入坐标和你想要的扫描时间即可以记录附近IBeacon的RSSI并记录到文本文件中。代码下方有代码下载链接。

  顺便无聊测试了下ArrayList和LinkedList读写速度的区别。没想到LinkedList的添加写入速度居然比ArrayList快10倍。

代码块

public class MainActivity extends AppCompatActivity {

    private static int beacon_number =6;
    private String hexScanRecord = "error";
    private int major = -999;
    private int minor =-999;
    private int get_rssi = -999;
    private String get_uuid = "error";
    private BluetoothAdapter mBluetoothAdapter;
    private static final int REQUEST_ENABLE_BT = 2;
    private double dist = 9999;
    private int txPower = -59;
    private beacon[] myIbeacon = new beacon[beacon_number+1];//有5個 beacon
    private Button btn_get_position;
    private Button averageButton;
    private Button tButton;
    private EditText xtext;
    private EditText ytext;
    private EditText ztext;
    private int T;  //蓝牙扫描周期
    private boolean averageRssiIsEnable=false; //均值记录
    private boolean loopEnable=false;
    private boolean detailEnable=false;
    private int loop;
    private int []toataltime={0,0,0,0,0,0,0};
    private int RSSI[]={0,0,0,0,0,0,0};
    private int num=20; //循环记录次数;
    private List list = new LinkedList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xtext=(EditText)findViewById(R.id.editText);
        ytext=(EditText)findViewById(R.id.editText2);
        ztext=(EditText)findViewById(R.id.editText3);
        btn_get_position = (Button)findViewById(R.id.button);
        averageButton=(Button)findViewById(R.id.button2);
        tButton=(Button)findViewById(R.id.button3);

        btn_get_position.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                detailEnable=true;
                T=  Integer.parseInt(ztext.getText().toString());
                Log.v("=====>", "扫描周期:"+T);
                find_beacon();

            }
        });
        averageButton.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                averageRssiIsEnable=true;
                T=  Integer.parseInt(ztext.getText().toString());
               // logAverage=false;
                find_beacon();
             //   Log.v("=====>", "Start btn");
            }
        });
        tButton.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                Log.v("=====>", "loop start");
                averageRssiIsEnable=true;
                loopEnable=true;
                loop=1;
                T=  Integer.parseInt(ztext.getText().toString());
                find_beacon();
            }
        });
    }

    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
           // Log.v("=====>", "Start OnLeScan");
            int startByte = 2;
            boolean patternFound = false;
            while (startByte <= 5) {
                if (    ((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon
                        ((int) scanRecord[startByte + 3] & 0xff) == 0x15) { //Identifies correct data length
                    patternFound = true;
                    hexScanRecord = bytesToHex(scanRecord);
                    break;
                }
                startByte++;
            }
            if (patternFound) {
                //Convert to hex String
                byte[] uuidBytes = new byte[16];
                System.arraycopy(scanRecord, startByte+4, uuidBytes, 0, 16);
                String hexString = bytesToHex(uuidBytes);
                //Here is your UUID
                String uuid =  hexString.substring(0,8) + "-" +
                        hexString.substring(8,12) + "-" +
                        hexString.substring(12,16) + "-" +
                        hexString.substring(16,20) + "-" +
                        hexString.substring(20,32);
                //Here is your Major value
                major = (scanRecord[startByte+20] & 0xff) * 0x100 + (scanRecord[startByte+21] & 0xff);
                //Here is your Minor value
                minor = (scanRecord[startByte+22] & 0xff) * 0x100 + (scanRecord[startByte+23] & 0xff);
                get_uuid = uuid;
                get_rssi = rssi;
                myIbeacon[minor] = new beacon(uuid ,major , minor ,txPower , rssi ,dist); //beacon类有什么用还没想好,留着应该会有用
                if (detailEnable==true) {
                    list.add(minor+"\t"+rssi);
                    //write(minor, myIbeacon[minor].get_rssi(),"RSSI");//RSSI写入TXT文件
                    Log.v("=====>", "rssi:"+minor+":"+rssi);
                }
                else  {
                        RSSI[minor] = RSSI[minor] + myIbeacon[minor].get_rssi();
                        toataltime[minor]++;
                    }
            }
        }
    };
    //写入TXT
    public void write(int minor,int rssi,String name){
        String Rssi = Environment.getExternalStorageDirectory()
                + File.separator + name+minor+".txt";
        File file = new File(Rssi);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter pw = new FileWriter(file, true);
            pw.write(xtext.getText().toString()+"\t"+ytext.getText().toString()+"\t"+minor+"\t"+rssi+"\n");
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void write(List list){
        Iterator it = list.iterator();
        String Rssi = Environment.getExternalStorageDirectory()
                + File.separator+"rssi.txt";
        File file = new File(Rssi);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter pw = new FileWriter(file, true);
            while(it.hasNext()) {
                pw.write(xtext.getText().toString()+"\t"+ytext.getText().toString()+"\t"+it.next()+"\n");
            }
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * bytesToHex method
     * Found on the internet
     * http://stackoverflow.com/a/9855338
     */
    static final char[] hexArray = "0123456789ABCDEF".toCharArray();
    private static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for ( int j = 0; j < bytes.length; j++ ) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    public void find_beacon()
    {
        //final BluetoothAdapter mBluetoothAdapter;
        BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();
        mBluetoothAdapter.startLeScan(mLeScanCallback);
        /**
         * Checks if Bluetooth is enabled on device
         * Use this within and Activity
         */
        if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT);
        }
        /**
         * Stop after T seconds
         */
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                mBluetoothAdapter.stopLeScan(mLeScanCallback);
                if (detailEnable=true){
                    write(list);
                    detailEnable=false;
                    list.clear();
                    Toast.makeText(getApplicationContext(), R.string.logCompleted,
                            Toast.LENGTH_SHORT).show();
                }
                if (averageRssiIsEnable==true) {
                    for (int i=2;i<6;i++) {   //我的Ibeacon minor 设置的2-5,所以这里i从2开始
                        try {
                            RSSI[i] = RSSI[i] / toataltime[i];
                            write(i, RSSI[i], "aveRSSI");//RSSI写入TXT文件
                            toataltime[i] = 0;
                            RSSI[i] = 0;
                        }
                        catch (Exception ex){

                        }
                    }
                    averageRssiIsEnable=false;
                    Toast.makeText(getApplicationContext(), R.string.logCompleted,
                            Toast.LENGTH_SHORT).show();
                }
                if (loopEnable==true&(loop<20)){
                    loop++;
                    find_beacon();
                    Toast.makeText(getApplicationContext(), "第"+loop+"次记录",
                            Toast.LENGTH_SHORT).show();
                    if (loop==19){
                        loopEnable=false;
                    }
                }
            }
        }, (T) );
    }
}

CSDN下载链接IBeacon蓝牙指纹定位打点工具

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值