简体   繁体   中英

How to get connect with closest bluetooth low energy device from android ?

I want to connect with closest ble device. How to get / calculate closest distance from rssi ?

For example, there are 2 ble device in a room. When I walk in that room, my android device will connect with nearest ble device from my position.

I'm using don cordova plugin and javascript . This project run in android device.

You can use Android Beacon library: https://altbeacon.github.io/android-beacon-library/

It gives readings for distance (which are not exact), but you can use them to detect which beacon is closer.

An example:

public class RangingActivity extends Activity implements BeaconConsumer {
    protected static final String TAG = "RangingActivity";
    private BeaconManager beaconManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranging);
        beaconManager = BeaconManager.getInstanceForApplication(this);
        beaconManager.bind(this);
    }
    @Override 
    protected void onDestroy() {
        super.onDestroy();
        beaconManager.unbind(this);
    }
    @Override
    public void onBeaconServiceConnect() {
        beaconManager.addRangeNotifier(new RangeNotifier() {
            @Override 
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                if (beacons.size() > 0) {
                    Log.i(TAG, "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");        
                }
            }
        });

        try {
            beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
        } catch (RemoteException e) {    }
    }
}

Otherwise, you can read the RSSI for the devices, and average them out over a period of time, and take out the outliers. The closer one would be the greater one (in magnitude, more positive).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM