Für alle, die SensorTags oder BLE-Geräte verwenden, die nicht von der Android Beacon Library unterstützt werden.
Ich habe es einfach so:
Hier habe ich für BLE Geräte mit dem Namen „SensorTag“ suchen.
public class MainActivity extends AppCompatActivity {
private LeDeviceListAdapter mLeDeviceListAdapter;
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;
private static final int REQUEST_ENABLE_BT = 1;
// Stops scanning after 0.2 seconds.
private static final long SCAN_PERIOD = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHandler = new Handler();
// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
}
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
mLeDeviceListAdapter.printBeacons();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
// Adapter for holding devices found through scanning.
private class LeDeviceListAdapter {
private ArrayList<Beacon> mLeDevices;
public LeDeviceListAdapter() {
super();
mLeDevices = new ArrayList<Beacon>();
}
public void printBeacons() {
for (int i = 0; i< mLeDevices.size(); i++) {
System.out.println("Beacon " + mLeDevices.get(i).address + ", Signal: " + mLeDevices.get(i).rssi +", Timestamp: " + mLeDevices.get(i).timestamp);
}
}
public boolean test(Beacon beacon) {
for(int i = 0; i< mLeDevices.size(); i++){
if (mLeDevices.get(i).address.equalsIgnoreCase(beacon.address)){
return true;
}
}
return false;
}
public void addDevice(Beacon beacon) {
if(!test(beacon)) {
mLeDevices.add(beacon);
}
else{
for(int i =0; i<mLeDevices.size(); i++){
if(mLeDevices.get(i).address.equalsIgnoreCase(beacon.address)){
mLeDevices.set(i, beacon);
}
}
}
}
public Beacon getDevice(int position) {
return mLeDevices.get(position);
}
public void clear() {
mLeDevices.clear();
}
public int getCount() {
return mLeDevices.size();
}
public Object getItem(int i) {
return mLeDevices.get(i);
}
public long getItemId(int i) {
return i;
}
}
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(device.getName()!= null || device.getName() == "SensorTag"){
Beacon beacon = new Beacon(device.getAddress(), rssi, System.currentTimeMillis());
mLeDeviceListAdapter.addDevice(beacon);
}
}
});
}
};
@Override
protected void onPause() {
super.onPause();
scanLeDevice(false);
mLeDeviceListAdapter.clear();
}
protected void onResume() {
super.onResume();
// Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled,
// fire an intent to display a dialog asking the user to grant permission to enable it.
if (!mBluetoothAdapter.isEnabled()) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
// Initializes list view adapter.
mLeDeviceListAdapter = new LeDeviceListAdapter();
//setListAdapter(mLeDeviceListAdapter);
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
scanLeDevice(true);
mLeDeviceListAdapter.clear();
}
};
//start new Scan Period every second
timer.scheduleAtFixedRate(task, 0 , 1000);
}
}
}
Und das ist die Klasse Beacon, die mit einer Adresse ein Beacon darstellt, RSSI und dem Zeitstempel der Beacon zuletzt gesehen wurde.
public class Beacon {
public String address;
public int rssi;
public long timestamp;
public Beacon(String address, int rssi, long timestamp){
this.address = address;
this.rssi = rssi;
this.timestamp=timestamp;
}
}
Der Code ist vereinfacht.
Grüße Max
Hallo David, vielen Dank, die perfekt mit simulierten Baken funktioniert. Aber ich bekomme meine SensorTags von Texas Instruments nicht. –
SensorTags müssen geladen werden, damit die Beacon-Firmware erkannt wird. Wenn Sie iBeacon verwenden, müssen Sie außerdem 'beaconManager.getBeaconParsers() aufrufen. Add (new BeaconParser(). SetBeaconLayout (...))' Übergeben Sie das Layout für iBeacon. Sie können dieses Layout mit einer schnellen Google-Suche finden. – davidgyoung
Hallo, ich habe es gerade manuell gemacht, danke für deine Hilfe. –