溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2020-09-13 20:11:04 來源:腳本之家 閱讀:146 作者:來瓶94年的爽歪歪 欄目:移動開發(fā)

最近再寫一個項目,需要自動簽到。用的就是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í)有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI