iBeacon使用藍(lán)牙連接范圍精確到1-3米
最近再寫一個項目,需要自動簽到。用的就是iBeacon,剛開始的時候比較懵比,不知道iBeacon是用來干啥的。因為iBeacon就是一個小盒盒,還是密封好的,就感覺自己懵了。然后上網(wǎng)查資料,才知道iBeacon就是一個小型的基站,手機打開藍(lán)牙之后,如果你在這個基站的范圍之內(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)主動連接和被動連接的藍(lán)牙聊天功能
- Android開發(fā)實現(xiàn)實時檢測藍(lán)牙連接狀態(tài)的方法【附源碼下載】
- Android 掃描附近的藍(lán)牙設(shè)備并連接藍(lán)牙音響的示例
- Android 藍(lán)牙連接 ESC/POS 熱敏打印機打印實例(ESC/POS指令篇)
- Android 藍(lán)牙連接 ESC/POS 熱敏打印機打印實例(藍(lán)牙連接篇)
- Android手機通過藍(lán)牙連接佳博打印機的實例代碼
- Android系統(tǒng)中的藍(lán)牙連接程序編寫實例教程
相關(guān)文章
android調(diào)用原生圖片裁剪后圖片尺寸縮放的解決方法
這篇文章主要為大家詳細(xì)介紹了android調(diào)用原生圖片裁剪后圖片尺寸縮放的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
詳解用RxJava實現(xiàn)事件總線(Event Bus)
本篇文章主要介紹了用RxJava實現(xiàn)事件總線(Event Bus),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Android編程之Button控件配合Toast控件用法分析
這篇文章主要介紹了Android編程之Button控件配合Toast控件用法,結(jié)合實例形式分析了Button控件及Toast控件的功能及具體使用技巧,需要的朋友可以參考下2015-12-12
Android AutoWrapTextView中英文排版問題的解決方法
這篇文章主要給大家介紹了關(guān)于Android AutoWrapTextView中英文排版問題的解決方法,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-05-05
詳解Android的MVVM框架 - 數(shù)據(jù)綁定
這篇文章主要介紹了詳解Android的MVVM框架 - 數(shù)據(jù)綁定,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
Android開發(fā)使用HttpURLConnection進(jìn)行網(wǎng)絡(luò)編程詳解【附源碼下載】
這篇文章主要介紹了Android開發(fā)使用HttpURLConnection進(jìn)行網(wǎng)絡(luò)編程的方法,結(jié)合實例形式分析了Android基于HttpURLConnection實現(xiàn)顯示圖片與文本功能,涉及Android布局、文本解析、數(shù)據(jù)傳輸、權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01

