RSS-Feed

David Hadizadeh

E-Mail

Xing-Profil

LinkedIn

Impressum

 




Bluetooth LE Technology (by MAC-Addresses)

06.05.2015

Here is an implementation for Bluetooth LE on Android. You can add this technology to the positioning manager with a single line of code:

1
positionManager.addTechnology(new BluetoothLeTechnology("BLUETOOTH_LE"));

The technology will take the strongest signal of an iBeacon or another Bluetooth LE device.

Add the following classes to your technology package:

Model for the technology:
BluetoothLeDevice .java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class BluetoothLeDevice implements Comparable<BluetoothLeDevice> {
 
    private String name;
    private String address;
    private int rssi;
 
    public BluetoothLeDevice(String name, String address, int rssi) {
        this.name = name;
        this.address = address;
        this.rssi = rssi;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
 
    public int getRssi() {
        return rssi;
    }
 
    public void setRssi(int rssi) {
        this.rssi = rssi;
    }
 
    @Override
    public int compareTo(BluetoothLeDevice another) {
        if(rssi > another.rssi) {
            return 1;
        } else if(rssi < another.rssi) {
            return -1;
        }
        return 0;
    }
}

The technology:
BluetoothLeTechnology.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import de.hadizadeh.positioning.controller.Technology;
import de.hadizadeh.positioning.model.SignalInformation;
 
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
 
public class BluetoothLeTechnology extends Technology {
    private Map<String, SignalInformation> signalData;
    private BluetoothAdapter bluetoothAdapter;
    private final int cacheSize = 10;
    private LinkedList<BluetoothLeDevice> btLeDevices;
 
    private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
            String address = device.getAddress();
            btLeDevices.add(new BluetoothLeDevice(device.getName(), address, rssi));
 
            while (btLeDevices.size() > cacheSize) {
                btLeDevices.removeFirst();
            }
        }
    };
 
    public BluetoothLeTechnology(String name) {
        super(name, null);
        btLeDevices = new LinkedList<BluetoothLeDevice>();
        providesExactlyPosition();
        this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        this.signalData = new HashMap<String, SignalInformation>();
        signalData = new HashMap<String, SignalInformation>();
 
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        bluetoothAdapter.startLeScan(leScanCallback);
    }
 
    @Override
    public Map<String, SignalInformation> getSignalData() {
        if (btLeDevices.size() > 0) {
            signalData.clear();
            BluetoothLeDevice bestDevice = Collections.max(btLeDevices);
            signalData.put(bestDevice.getAddress(), new SignalInformation(1.0));
            return signalData;
        } else {
            return null;
        }
    }
 
    @Override
    public void stopScanning() {
        super.stopScanning();
        bluetoothAdapter.stopLeScan(leScanCallback);
    }
}

Kategorie: Indoor-Positioning Addons |


Impressum