指纹定位蓝牙打点工具
来源:互联网 发布:三体bug 知乎 编辑:程序博客网 时间:2024/06/05 05:30
最近在研究指纹定位,实验室只有自己在做这个方向,只好自己写了一个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蓝牙指纹定位打点工具
阅读全文
0 0
- 指纹定位蓝牙打点工具
- 室内定位指纹算法
- Koa 请求日志打点工具
- Android定位sdk稳定打点实践
- 指纹工具类
- 构建Wifi指纹定位库~~
- whatweb WEB指纹探测工具
- python exp指纹录入工具
- 蓝牙BLE室内定位
- 位置指纹(LF)定位技术简介-室内定位
- 指纹
- 指纹算法在Wi-Fi定位中的应用与改进
- TensorFlow6: 基于WiFi指纹的室内定位(autoencoder)
- 基于WiFi 的CSI和RSS混合指纹室内定位
- 蓝牙开发工具
- 蓝牙开发工具
- 蓝牙工具类readme
- 打点生活
- Android实现界面左右滑动切换
- hadoop-2.7.4-nodemanager无法启动问题解决方案
- HDU BillBoard
- PHP算法
- 文章标题
- 指纹定位蓝牙打点工具
- 十一个行为型模式8:状态模式-State Pattern【学习难度:★★★☆☆,使用频率:★★★☆☆】
- 李宏毅课程markdown笔记附图
- ROM定制入门
- 第三周项目一(1) 顺序表的基本运算
- 近期文章参考
- 【模拟赛】qbxt金秋冲刺训练营杯NOIP模拟赛第一场总结
- Jedis与Jedis连接池
- 数据结构 创建栈 压栈 删除栈顶元素 清空栈 遍历栈 例子