iBeacon使用藍牙連接范圍精確到1-3米
最近再寫一個項目,需要自動簽到。用的就是iBeacon,剛開始的時候比較懵比,不知道iBeacon是用來干啥的。因為iBeacon就是一個小盒盒,還是密封好的,就感覺自己懵了。然后上網(wǎng)查資料,才知道iBeacon就是一個小型的基站,手機打開藍牙之后,如果你在這個基站的范圍之內(nèi),會自動匹配上。你對iBeacon不需要做任何的操作,因為里面有電池,說是可以使用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è)備有危險!");
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;
}
}
}
以上就是全部代碼展示,布局文件里面就是一個TextView,這里就不貼布局文件的代碼了。
希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android檢測IBeacon熱點的方法
- android獲取ibeacon列表的方法
- Android提高之BLE開發(fā)Android手機搜索iBeacon基站
- android實現(xiàn)主動連接和被動連接的藍牙聊天功能
- Android開發(fā)實現(xiàn)實時檢測藍牙連接狀態(tài)的方法【附源碼下載】
- Android 掃描附近的藍牙設(shè)備并連接藍牙音響的示例
- Android 藍牙連接 ESC/POS 熱敏打印機打印實例(ESC/POS指令篇)
- Android 藍牙連接 ESC/POS 熱敏打印機打印實例(藍牙連接篇)
- Android手機通過藍牙連接佳博打印機的實例代碼
- Android系統(tǒng)中的藍牙連接程序編寫實例教程
相關(guān)文章
Android 實現(xiàn)帶頭部文字輸入框的自定義控件
這篇文章主要介紹了Android 實現(xiàn)帶頭部文字輸入框的自定義控件,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04
Android實現(xiàn)機房座位預(yù)約系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)機房座位預(yù)約系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
Android SQLite數(shù)據(jù)庫進行查詢優(yōu)化的方法
這篇文章主要給大家介紹了關(guān)于Android SQLite數(shù)據(jù)庫進行查詢優(yōu)化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
Android編程實現(xiàn)網(wǎng)絡(luò)圖片查看器和網(wǎng)頁源碼查看器實例
這篇文章主要介紹了Android編程實現(xiàn)網(wǎng)絡(luò)圖片查看器和網(wǎng)頁源碼查看器,結(jié)合實例形式分析了Android針對網(wǎng)絡(luò)圖片及網(wǎng)頁的相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-01-01
android開發(fā) eclipse alt+”/”自動提示失效的解決方法
最近在學(xué)習(xí)android開發(fā)布局這塊。第一次學(xué)習(xí),很多代碼不熟悉。所以自動提示對我來說很重要。但悲催的就是這個自動提示失效。今天在網(wǎng)上搜索了一下解決辦法,主要有一下幾種方法2014-05-05

