Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate distance between two android device using Bluetooth signal strength?

I'm working on android application. In my project I want to show Bluetooth scanning device, MAC Address, Bluetooth signal strength and Distance between two Android device.

I have done 3 requirements but I don't know how to get distance using Signal strength.

package com.example.bluetoothdemo;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.content.BroadcastReceiver;

public class MainActivity extends Activity {
 private BluetoothAdapter BTAdapter = BluetoothAdapter.getDefaultAdapter();
private static final int REQUEST_ENABLE_BT = 1;

ListView listDevicesFound;
Button btnScanDevice;
TextView stateBluetooth;
BluetoothAdapter bluetoothAdapter;

ArrayAdapter<String> btArrayAdapter;

/**  Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  btnScanDevice = (Button)findViewById(R.id.scan_device);

  stateBluetooth = (TextView)findViewById(R.id.textView1);
  bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

  listDevicesFound = (ListView)findViewById(R.id.listView1);

  btArrayAdapter = new ArrayAdapter<String>(MainActivity.this, 

   android.R.layout.simple_list_item_1);
  listDevicesFound.setAdapter(btArrayAdapter);

  CheckBlueToothState();

  btnScanDevice.setOnClickListener(btnScanDeviceOnClickListener);

  registerReceiver(ActionFoundReceiver,   new IntentFilter(BluetoothDevice.ACTION_FOUND));
  registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
   }


        @Override

     protected void onDestroy() {
// TODO Auto-generated method stub
  super.onDestroy();
unregisterReceiver(ActionFoundReceiver);
 }

      private void CheckBlueToothState(){
   if (bluetoothAdapter == null){
   stateBluetooth.setText("Bluetooth NOT support");
    }else{
   if (bluetoothAdapter.isEnabled()){
    if(bluetoothAdapter.isDiscovering()){
     stateBluetooth.setText("Bluetooth is currently in device discovery process.");
    }else{
     stateBluetooth.setText("Bluetooth is Enabled.");
     btnScanDevice.setEnabled(true);
    }
   }else{
    stateBluetooth.setText("Bluetooth is NOT Enabled!");
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
       startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
   }
  }
 }

   private Button.OnClickListener btnScanDeviceOnClickListener  = new Button.OnClickListener(){


      @Override
    public void onClick(View arg0) {
     // TODO Auto-generated method stub
  btArrayAdapter.clear();
  bluetoothAdapter.startDiscovery();
        }};

       @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if(requestCode == REQUEST_ENABLE_BT){
      CheckBlueToothState();
     }
}

    private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver(){

     @Override
        public void onReceive(Context context, Intent intent) {
     // TODO Auto-generated method stub
      String action = intent.getAction();
    if(BluetoothDevice.ACTION_FOUND.equals(action)) {
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
       btArrayAdapter.add(device.getName() + "\n" + device.getAddress()+ "        
 \n "+intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE));
       btArrayAdapter.notifyDataSetChanged();
   }
  }};
     @Override
   public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

   private final BroadcastReceiver receiver = new BroadcastReceiver(){
   @Override
   public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    if(BluetoothDevice.ACTION_FOUND.equals(action)) {
        int  rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
        String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
        TextView rssi_msg = (TextView) findViewById(R.id.listView1);

        Toast.makeText(getApplicationContext(),"  RSSI: " + rssi + "dBm",     
   Toast.LENGTH_SHORT).show();
    }
 }
  };
 }
like image 624
user3560169 Avatar asked Apr 22 '14 11:04

user3560169


2 Answers

Try using the "b and l bluetooth le scanner" app from the Google Play Store, to visualize the signals from several Bluetooth LE devices. You will immediately discover that the rssi signal strengths are "noisy" when multiple devices are present. Walls, furniture with metal components, and Wi-Fi sources will also cause signal variations. The best solution is to create "zones" for your distance readings... Something like: far, close, next-to, etc.

like image 96
BlueSpectrumz Avatar answered Sep 28 '22 23:09

BlueSpectrumz


That is practically not possible to calculate the distance based on only the bluetooth signal strength. You can however use the triangulation technique to get the distance between the two devices or altogether can use the GPS concept to find out the distance. Using only bluetooth, finding distance is not possible. Even though you can do something like this to denote the signal strength in an easy understandable way.

https://play.google.com/store/apps/details?id=com.bluetoothFinder

see this link.

like image 27
akash89 Avatar answered Sep 29 '22 00:09

akash89