国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

iBeacon使用藍牙連接范圍精確到1-3米

2019-10-21 21:36:26
字體:
來源:轉載
供稿:網友

最近再寫一個項目,需要自動簽到。用的就是iBeacon,剛開始的時候比較懵比,不知道iBeacon是用來干啥的。因為iBeacon就是一個小盒盒,還是密封好的,就感覺自己懵了。然后上網查資料,才知道iBeacon就是一個小型的基站,手機打開藍牙之后,如果你在這個基站的范圍之內,會自動匹配上。你對iBeacon不需要做任何的操作,因為里面有電池,說是可以使用5年左右。

以上就是大概的情況,接下來介紹的是代碼展示部分。

首先,在你的主清單中AndroidManifest.xml中添加權限:

<uses-permission android:name="android.permission.VIBRATE" /><uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

權限添加完畢之后,接下來就是代碼部分了

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("設備有危險!");         playVibator(MainActivity.this, 1000);        } else {         textView.setText("設備正常!");//在1米范圍內        }      }     }   }  };  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,這里就不貼布局文件的代碼了。

希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蓝田县| 福海县| 屏东市| 黔东| 乌海市| 醴陵市| 松桃| 万安县| 家居| 满洲里市| 沙雅县| 交城县| 如皋市| 宁都县| 宝清县| 城固县| 浪卡子县| 涪陵区| 黎川县| 中超| 张家界市| 壶关县| 寻乌县| 鹿泉市| 万年县| 光泽县| 武胜县| 张家港市| 台东市| 潞西市| 罗城| 米泉市| 文昌市| 邛崃市| 福鼎市| 资源县| 讷河市| 潮安县| 周至县| 周至县| 浦北县|