I am trying to scan beacons with bluetooth low energy and the altbeacon-library on an Samsung Galaxy S5, and maybe I did not understand It correctly, or I just can't get it running.
A part of my manifest looks like this:
<uses-sdk android:minSdkVersion="18" android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
....
<service
android:name=".BeaconScanner"
android:icon="@drawable/ic_launcher"
android:label="BeaconScanner" >
</service>
....
I am scanning for beacons inside a service:
public class BeaconScanner extends Service implements BeaconConsumer {
private BeaconManager beaconManager;
private DBHelper myDB;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
myDB = new DBHelper(this);
if (Build.VERSION.SDK_INT >= 19 && getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
// BLE
BeaconManager.setAndroidLScanningDisabled(true);
} else {
// kein BLE
}
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
beaconManager.setForegroundBetweenScanPeriod(15000l);
beaconManager.setBackgroundBetweenScanPeriod(15000l);
beaconManager.bind(this);
return Service.START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
for(Beacon beacon : beacons)
{
if (beacon != null && beacon.getId1() != null) {
System.out.println("id1: " + beacon.getId1().toString());
System.out.println("id2: " + beacon.getId2().toString());
System.out.println("id3: " + beacon.getId3().toString());
String majorMinor = beacon.getId2().toString() + "-" + beacon.getId3().toString();
System.out.println(majorMinor);
long count = myDB.beaconExists(majorMinor);
System.out.println(count);
if (count == 0) {
getAndInsertBeaconDataSet(myDB, majorMinor);
System.out.println("inserted beacon: " + majorMinor);
}
}
}
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("trewysUniqueBeaconRegion", null, null, null));
} catch (RemoteException e) {
}
}
private Boolean getAndInsertBeaconDataSet(DBHelper dbHelper, String beaconId) {
try {
BeaconGetter beaconGetter = new BeaconGetter();
return beaconGetter.execute(dbHelper, beaconId).get();
} catch (Exception e) {
return false;
}
}
}
I checked, if my phone is able to use Bluetooth low energy, and it is. If my bluetooth is not activated I get this message from the library:
06-15 09:16:46.399: W/CycledLeScanner(22748): Bluetooth is disabled. Cannot scan for beacons.
If I activate Bluetooth, the library is working fine and I get all beacon in range.
So maybe I understood it wrong, do I need to activate Bluetooth on android to use Bluetooth low energy? I thought it is possible to get bluetooth-signals without activating bluetooth?
If it is possible to scan Bluetooth low energy without activating Bluetooth: what am I doing wrong?
Any help is appreciated, thanks in advance.
rholtermann
You MUST activate Bluetooth to use BLE (Low Energy). Otherwise you can't scan for Beacons. Also your device MUST support BLE, most modern devices support it.