iBeacon使用藍(lán)牙連接范圍精確到1-3米
最近再寫一個(gè)項(xiàng)目,需要自動(dòng)簽到。用的就是iBeacon,剛開始的時(shí)候比較懵比,不知道iBeacon是用來干啥的。因?yàn)閕Beacon就是一個(gè)小盒盒,還是密封好的,就感覺自己懵了。然后上網(wǎng)查資料,才知道iBeacon就是一個(gè)小型的基站,手機(jī)打開藍(lán)牙之后,如果你在這個(gè)基站的范圍之內(nèi),會(huì)自動(dòng)匹配上。你對(duì)iBeacon不需要做任何的操作,因?yàn)槔锩嬗须姵?,說是可以使用5年左右。
以上就是大概的情況,接下來介紹的是代碼展示部分。
首先,在你的主清單中AndroidManifest.xml中添加權(quán)限:
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
權(quán)限添加完畢之后,接下來就是代碼部分了
public class MainActivity extends Activity { private BluetoothAdapter bluetoothAdapter; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView1); BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); bluetoothAdapter = manager.getAdapter(); if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) { Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, 1); } bluetoothAdapter.startLeScan(mLeScanCallback); } public void playVibator(Context context, long timeLong) { Vibrator vib = (Vibrator) context .getSystemService(Service.VIBRATOR_SERVICE); vib.vibrate(timeLong); } private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) { int startByte = 2; boolean patternFound = false; // 尋找ibeacon while (startByte <= 5) { if (((int) scanRecord[startByte + 2] & 0xff) == 0x02 && ((int) scanRecord[startByte + 3] & 0xff) == 0x15) { patternFound = true; break; } startByte++; } // 如果找到了的話 if (patternFound) { String ibeaconName = device.getName(); int txPower = (scanRecord[startByte + 24]); if (ibeaconName.equals("E-Beacon_CE6D94")) { System.out.println(calculateAccuracy(txPower, rssi)); if (calculateAccuracy(txPower, rssi) > 1) {//這里是指iBeacon超過1米之后,textView字體變化 textView.setText("設(shè)備有危險(xiǎn)!"); playVibator(MainActivity.this, 1000); } else { textView.setText("設(shè)備正常!");//在1米范圍內(nèi) } } } } }; protected static double calculateAccuracy(int txPower, double rssi) { if (rssi == 0) { return -1.0; // if we cannot determine accuracy, return -1. } double ratio = rssi * 1.0 / txPower; if (ratio < 1.0) { return Math.pow(ratio, 10); } else { double accuracy = (0.89976) * Math.pow(ratio, 7.7095) + 0.111; return accuracy; } } }
以上就是全部代碼展示,布局文件里面就是一個(gè)TextView,這里就不貼布局文件的代碼了。
希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android檢測(cè)IBeacon熱點(diǎn)的方法
- android獲取ibeacon列表的方法
- Android提高之BLE開發(fā)Android手機(jī)搜索iBeacon基站
- android實(shí)現(xiàn)主動(dòng)連接和被動(dòng)連接的藍(lán)牙聊天功能
- Android開發(fā)實(shí)現(xiàn)實(shí)時(shí)檢測(cè)藍(lán)牙連接狀態(tài)的方法【附源碼下載】
- Android 掃描附近的藍(lán)牙設(shè)備并連接藍(lán)牙音響的示例
- Android 藍(lán)牙連接 ESC/POS 熱敏打印機(jī)打印實(shí)例(ESC/POS指令篇)
- Android 藍(lán)牙連接 ESC/POS 熱敏打印機(jī)打印實(shí)例(藍(lán)牙連接篇)
- Android手機(jī)通過藍(lán)牙連接佳博打印機(jī)的實(shí)例代碼
- Android系統(tǒng)中的藍(lán)牙連接程序編寫實(shí)例教程
相關(guān)文章
Android實(shí)現(xiàn)微信聊天語言點(diǎn)擊喇叭動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)微信聊天語言點(diǎn)擊喇叭動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android 實(shí)現(xiàn)帶頭部文字輸入框的自定義控件
這篇文章主要介紹了Android 實(shí)現(xiàn)帶頭部文字輸入框的自定義控件,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04Android實(shí)現(xiàn)機(jī)房座位預(yù)約系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)機(jī)房座位預(yù)約系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Android SQLite數(shù)據(jù)庫(kù)進(jìn)行查詢優(yōu)化的方法
這篇文章主要給大家介紹了關(guān)于Android SQLite數(shù)據(jù)庫(kù)進(jìn)行查詢優(yōu)化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11Android編程實(shí)現(xiàn)網(wǎng)絡(luò)圖片查看器和網(wǎng)頁(yè)源碼查看器實(shí)例
這篇文章主要介紹了Android編程實(shí)現(xiàn)網(wǎng)絡(luò)圖片查看器和網(wǎng)頁(yè)源碼查看器,結(jié)合實(shí)例形式分析了Android針對(duì)網(wǎng)絡(luò)圖片及網(wǎng)頁(yè)的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-01-01android開發(fā) eclipse alt+”/”自動(dòng)提示失效的解決方法
最近在學(xué)習(xí)android開發(fā)布局這塊。第一次學(xué)習(xí),很多代碼不熟悉。所以自動(dòng)提示對(duì)我來說很重要。但悲催的就是這個(gè)自動(dòng)提示失效。今天在網(wǎng)上搜索了一下解決辦法,主要有一下幾種方法2014-05-05