iBeacon uses Bluetooth connection range to be accurate to 1 3 meters

  • 2021-10-24 23:39:43
  • OfStack

Write another project recently, and need to sign in automatically. I used iBeacon, which was rather stupid at the beginning. I don't know what iBeacon is used for. Because iBeacon is a small box, which is still sealed, I feel stupid. Then check the information on the Internet, only to know that iBeacon is a small base station. After the mobile phone turns on Bluetooth, if you are within the range of this base station, it will automatically match. You don't need to do any operation on iBeacon, because there is a battery in it, which can be used for about 5 years.

The above is the general situation, and the next part is the code display.

First, add permissions to your main list in AndroidManifest. xml:


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

After the permissions are added, the next step is the code section


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;
     //  Looking for ibeacon
     while (startByte <= 5) {
      if (((int) scanRecord[startByte + 2] & 0xff) == 0x02
         && ((int) scanRecord[startByte + 3] & 0xff) == 0x15) {
        patternFound = true;
        break;
      }
      startByte++;
     }
     //  If we find it, 
     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) {// This refers to iBeacon Exceed 1 After meters, textView Font change 
         textView.setText(" Equipment is in danger! ");
         playVibator(MainActivity.this, 1000);
        } else {
         textView.setText(" Equipment is normal! ");// In 1 Within meters 
        }
      }
     }
   }
  };

  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;
   }
  }
}

The above is all the code display, and there is an TextView in the layout file, so the code of the layout file will not be posted here.


Related articles: