SoFunction
Updated on 2025-03-04

Android gets the instance code of nearby Bluetooth devices and calculates distance

Need to use a local Bluetooth adapter

// Get the local Bluetooth adaptermBluetoothAdapter = ();

Determine whether Bluetooth is supported and confirm that the function is turned on.

// Determine whether the phone supports Bluetooth if (mBluetoothAdapter == null) {
  (this, "The device does not support Bluetooth", Toast.LENGTH_SHORT).show();
  finish();
 }
 // Determine whether Bluetooth is turned on if (!()) {
  // A dialog box pops up and prompts the user to open it after it is prompted.  Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  startActivityForResult(intent, 1);
  // No prompts, force open  // ();
 }else {
  // No prompts, force open   ();
 }

Get the Bluetooth device that your phone has already paired

// Get the paired device Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
   .getBondedDevices();
 // Determine whether there are paired devices if (() > 0) {
  for (BluetoothDevice device : pairedDevices) {
   //Travel   (());
   (() + ":" + () + "\n");
  }
 }

Register asynchronously searching for broadcasts for Bluetooth devices

// Find the device's broadcastIntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
// Register for broadcastregisterReceiver(receiver, filter);
// Search completed broadcastfilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
// Register for broadcastregisterReceiver(receiver, filter);

How to search for Bluetooth

 private void scanBluth() {
// Set the progress barsetProgressBarIndeterminateVisibility(true);
setTitle("Searching...");
// Determine whether you are searching. If you are searching, cancel the searchif (()) {
();
}
// Start searching();
}

Broadcast receiver

 private final BroadcastReceiver receiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
  // Type of broadcast received  String action = ();
  // Discover the device's broadcast  if (BluetoothDevice.ACTION_FOUND.equals(action)) {
   // Get the device from the intent   BluetoothDevice device = intent
     .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
   String aa = ().toString() + "";
   if ((())) {
    return;
   } else {
    // Determine whether it has been paired    if (() != BluetoothDevice.BOND_BONDED) {
     // Add to list     short rssi = ().getShort(
       BluetoothDevice.EXTRA_RSSI);
     int iRssi = abs(rssi);
 // Convert Bluetooth signal strength to distance     double power = (iRssi - 59) / 25.0;
     String mm = new Formatter().format("%.2f", pow(10, power)).toString();
     (() + ":"
       + () + " :" + mm + "m" + "\n");
    }else {
    }
   }
   // Search is completed  } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
    .equals(action)) {
   // Close the progress bar   setProgressBarIndeterminateVisibility(true);
   setTitle("Search completed!");
   (1, 1000);
  }
 }
};

I added a loop scan Handler in the code

// Hangdler for scanning Bluetooth cyclicallyHandler mBLHandler = new Handler() {
 @Override
 public void handleMessage(Message msg) {
  (msg);
  switch () {
   case 1:
    scanBluth();
    break;
   default:
    break;
  }
 }
};

A permission manager written in the project is used in the early stage. Go to the previous one to see the address:

https:///article/

The permissions used are

//All the permissions required by the mobile phone can use the Bluetooth function normally <uses-permission android:name="" />
<uses-permission android:name=".BLUETOOTH_ADMIN" />
//Some mobile phones (such as Xiaomi, etc.) need to add the following two permissions to be used normally<uses-permission android:name=".ACCESS_COARSE_LOCATION" />
<uses-permission android:name=".ACCESS_FINE_LOCATION" />

The above example code for obtaining nearby Bluetooth devices and calculating distances is all the content I share with you. I hope you can give you a reference and I hope you can support me more.