I am developing application for monitoring and configuring beacon devices. I need to monitor both iBeacon and Eddystone beacon devices. I have go through the nRF Master control application. Its working perfectly. But I need a source code for that. Is any other option available. Kindly get me the best solution for analysing beacon devices. Thanks in advance.
I would suggest you to use AltBeacon library, which I have used in one of my projects and it's pretty good (I am not associated with it in any way). It provides APIs to interact with beacons.
Here is a sample Activity to get you started:
public class MyActivity extends AppCompatActivity implements
BeaconConsumer,
BootstrapNotifier,
RangeNotifier
{
private RegionBootstrap mRegionBootstrap;
private org.altbeacon.beacon.BeaconManager mAltBeaconManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_application);
mAltBeaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(MyActivity.this);
boolean isBleAvailableAndEnabled;
try {
isBleAvailableAndEnabled = mAltBeaconManager.checkAvailability();
} catch (BleNotAvailableException ex) {
isBleAvailableAndEnabled = false;
}
if (!isBleAvailableAndEnabled) {
// Handle case ...
finish();
}
// Disable Android L scanning on devices with Android 5.0 and above
if (Build.VERSION.SDK_INT >= 21) mAltBeaconManager.setAndroidLScanningDisabled(true);
// Add iBeacon Layout
mAltBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
// Add Eddystone Layout
mAltBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("Eddystone_layout"));
mAltBeaconManager.setBackgroundBetweenScanPeriod(3000); // 3 sec
mAltBeaconManager.setBackgroundScanPeriod(5000); // 5 sec
mAltBeaconManager.bind(MyActivity.this);
// Enable Beacon scanning
mRegionBootstrap = new RegionBootstrap(MyActivity.this, getScanningRegion());
}
#Override
public Context getApplicationContext() {
return (!isFinishing()) ? MyActivity.this : null);
}
#Override
public void onBeaconServiceConnect() {
try {
// Attach beacon range listener
mAltBeaconManager.setRangeNotifier(this);
mAltBeaconManager.startRangingBeaconsInRegion(new Region("Region", null, null, null));
} catch (RemoteException ex) {
// Handle exception
}
}
#Override
public void unbindService(ServiceConnection serviceConnection) {
// Not needed
}
#Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return false;
}
#Override
public void didEnterRegion(Region region) {
// Handle event
}
#Override
public void didExitRegion(Region region) {
// Handle event
}
#Override
public void didDetermineStateForRegion(int i, Region region) {
// Handle event
}
#Override
public void didRangeBeaconsInRegion(Collection<org.altbeacon.beacon.Beacon> rangingBeacons, Region region) {
// Here you will receive the beacons which are currently in range
}
}
Add this to your Manifest:
<uses-sdk android:minSdkVersion="18" android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Note: Also, pay attention to this library's limitations, which essentially are Android's software and hardware limitations regarding beacons support.
Related
I'm using the AltBeacon library for detecting iBeacons in my Android app. The code I have works on the following devices:
Xiaomi MI9 (Android 10.0)
Motorola Moto G4 (Android 6.0.1)
Huawei P Smart (Android 8.0)
Samsung Galaxy S8 (Android 9.0)
However, the same code doesn't work for a OnePlus 6 (Android 10, OxygenOS 10.3.2). It doesn't detect any beacons in my app. I tried to detect the beacons using an other app (Locate), that works. The creator of the AltBeacon library told me that Locate uses the AltBeacon library, so the beacons are detectable. This means my code setup is wrong. Can you help me by finding out what is wrong with my setup?
I checked (e.g.) this answer, although it didn't fix my problem. I turned debugging on for the BeaconManager but nothing interesting came out of that (an example at the bottom of this question).
In the ViewModel I call the MyStateManager. It contains a List regionsInRange, which contains beacons that are in range. I left out some code because I think it is irrelevant. If you feel like I left out too much, I will add it.
public class MyStateManager implements BootstrapNotifier {
private static final MyStateManager instance = new MyStateManager();
private final MyBeaconHelper myBeaconHelper;
// ViewModel accessess this List to retrieve the beacons that are found.
public final List<Region> regionsInRange = new ArrayList<>();
private PresenceRegistrationStateManager() {
presenceRegistrationBeaconHelper = new PresenceRegistrationBeaconHelper(this);
updateScanningRegions();
}
#Override
public Context getApplicationContext() {
return MyApplication.getAppContext();
}
#Override
public void didEnterRegion(Region region) {
//Empty method
}
#Override
public void didExitRegion(Region region) {
//Empty method
}
#Override
public void didDetermineStateForRegion(int status, Region region) {
if (status == OUTSIDE) {
regionsInRange.remove(region);
} else {
if (!regionsInRange.contains(region)) {
regionsInRange.add(region);
}
}
updateState();
}
public static MyStateManager getInstance() {
return instance;
}
public void updateState() {
// Own implementation here
}
private void updateScanningRegions() {
// add all the regions here
}
}
In addition, this is the MyBeaconHelper:
public class MyBeaconHelper implements BeaconConsumer, Serializable {
private transient final RegionBootstrap regionBootstrap;
private List<Region> scanRegions = new ArrayList<>();
public MyBeaconHelper(BootstrapNotifier bootstrapNotifier) {
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(getApplicationContext());
beaconManager.getBeaconParsers().clear();
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
LogManager.setVerboseLoggingEnabled(true);
beaconManager.bind(this);
regionBootstrap = new RegionBootstrap(bootstrapNotifier, new ArrayList<>());
}
#Override
public void onBeaconServiceConnect() {
//Empty method
}
#Override
public Context getApplicationContext() {
return MyApplication.getAppContext();
}
#Override
public void unbindService(ServiceConnection serviceConnection) {
getApplicationContext().unbindService(serviceConnection);
}
#Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return getApplicationContext().bindService(intent, serviceConnection, i);
}
public void updateScanRegions(List<Region> newRegions) {
for (Region oldRegion : this.scanRegions) {
if (!newRegions.contains(oldRegion)) {
regionBootstrap.removeRegion(oldRegion);
}
}
for (Region newRegion : newRegions) {
if (!this.scanRegions.contains(newRegion)) {
regionBootstrap.addRegion(newRegion);
}
}
this.scanRegions = newRegions;
}
}
When I turned debugging on for the BeaconManager, it showed me this a lot of times:
2020-03-31 11:57:30.181 25259-25259/com.my.app D/CycledLeScanner: starting a new scan cycle
2020-03-31 11:57:30.181 25259-25259/com.my.app D/CycledLeScanner: We are already scanning and have been for 1134 millis
2020-03-31 11:57:30.181 25259-25259/com.my.app D/CycledLeScanner: Waiting to stop scan cycle for another 1100 milliseconds
2020-03-31 11:57:30.181 25259-25259/com.my.app D/CycledLeScanner: Scan started
2020-03-31 11:57:31.213 25259-25259/com.my.app D/CycledLeScanner: Waiting to stop scan cycle for another 69 milliseconds
2020-03-31 11:57:31.323 25259-25259/com.my.app D/CycledLeScanner: Done with scan cycle
It keeps printing these lines over and over again...
The log messages shown (these are for OnePlus, yes?) indicate that BLE scanning is started. Do you see any log lines showing hex bytes of the packets detected? If BLE scanning is actually functioning you should. You may want to compare the logs output by the other devices.
Are you certain proper location permission has been granted to your app on the OnePlus? You can check in Settings -> Apps - > You App -> Permissions. Also confirm Bluetooth is on and location is on for the global phone settings (but if Locate works on the same device, this should not be a problem .)
It is not clear if this is related, but the use of beaconManager.bind() at the same time as RegionBootstrap is unnecessary and may cause conflicts. The code appears to not use the BeaconConsumer interface that is called back by the bind method. I suggest you remove the bind call, the use of BeaconConsumer and remove all that interface's callback methods just to be sure.
Ì am trying to connect with beacons in my android app. But I can't seem to find my beacons. I am using Ibeacons. I am using the AltBeacon Library. The onBeaconServiceConnect starts and the didDetermineStateForRegion follows. But didEnterReion never gets called. This is my code:
public class ListScenarios extends AppCompatActivity implements BeaconConsumer, MonitorNotifier {
private static final String TAG = "ListScenarios";
private ListView listView;
public String persoonID;
public String adresID;
public Array beaconArray;
//private ArrayList<IBeacon> arrBeacons = new ArrayList<>();
private BeaconManager mBeaconManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_scenarios);
// Setup beaconmanager
mBeaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext());
// Detect iBeacon
mBeaconManager.getBeaconParsers().add(new BeaconParser()
.setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
mBeaconManager.bind(this);
//listview
listView = (ListView) findViewById(R.id.list_scenario);
//send request to load list
getScenarios();
getBeacons();
}
// MARK: - Bluetooth
#Override
public void onBeaconServiceConnect() {
System.out.println("We are in onBeaconServiceConnect");
// Set the two identifiers below to null to detect any beacon regardless of identifiers
Identifier myBeaconNamespaceId = null;
Identifier myBeaconInstanceId = null;
Region region = new Region("my-beacon-region", myBeaconNamespaceId, myBeaconInstanceId, null);
mBeaconManager.addMonitorNotifier(this);
try {
mBeaconManager.startMonitoringBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void didEnterRegion(Region region) {
System.out.println("We are in didEnterRegion");
Log.d(TAG, "I detected a beacon in the region with namespace id " + region.getId1() +
" and instance id: " + region.getId2());
}
public void didExitRegion(Region region) {
System.out.println("We are in didExitRegion");
}
public void didDetermineStateForRegion(int state, Region region) {
System.out.println("We are in didDetermineStateForRegion");
}
#Override
public void onPause() {
super.onPause();
mBeaconManager.unbind(this);
}
Two things to check:
Make sure you are using the correct BeaconParser expression. The expression shown in the question is for AltBeacon: "m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25. If you are trying to detect iBeacon, you'll need to use a different expression. This site has a handy reference: https://beaconlayout.wordpress.com/
If you are running your app on Android 6+ and are targeting SDK 23 or higher, you need to dynamically request location permission for your app. If you don't do this, you will get no detections, and you will see this in the logs: 04-22 22:35:20.152 5158 5254 E BluetoothUtils: Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results You can read how to do this here: http://altbeacon.github.io/android-beacon-library/requesting_permission.html
I am working on Android beacon app with PiBeacon as a beacon device.
I have setup Respberry Pi As Beacon Device as given on adfruit website.
I can see that PiBeacon is transmitting by using other BLE Scanner application available on Play Store.
I have download the Altbeacon and configure my Android Studio Project according to given guide on Altbeacon Sample but the app is not showing the ble device. Can anyone help me what i am doing wrong ?
Following is the code that i am using for scanning PiBeacon.
public class MonitoringActivity extends Activity implements BeaconConsumer {
protected static final String TAG = "MonitoringActivity";
private BeaconManager beaconManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ranging);
beaconManager = BeaconManager.getInstanceForApplication(this);
// To detect proprietary beacons, you must add a line like below corresponding to your beacon
// type. Do a web search for "setBeaconLayout" to get the proper expression.
// beaconManager.getBeaconParsers().add(new BeaconParser().
// setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
beaconManager.bind(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
#Override
public void onBeaconServiceConnect() {
beaconManager.setMonitorNotifier(new MonitorNotifier() {
#Override
public void didEnterRegion(Region region) {
Log.i(TAG, "I just saw an beacon for the first time!");
}
#Override
public void didExitRegion(Region region) {
Log.i(TAG, "I no longer see an beacon");
}
#Override
public void didDetermineStateForRegion(int state, Region region) {
Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
} catch (RemoteException e) { }
}
}
The PiBeacon you reference sends and iBeacon transmission, but by default, the Android Beacon Library only detects open-source AltBeacon transmissions.
It's easy to detect other beacon types, but you need to add a new BeaconParser for the beacon type you are interested in. The instructions are right there in the comments of the code you posted:
To detect proprietary beacons, you must add a line like below corresponding to your beacon type. Do a web search for "setBeaconLayout" to get the proper expression.
Apologies, for the need to do this -- unfortunately intellectual property restrictions mean that we cannot include the expression in the library or post it on public forums like this. Fortunately, it is easy to find with a Google search.
I'm working with the AltBeacon library (2.5.1) to detect beacons.
I setup ranging with an "universal" Region to be able to detect any beacon in range, then do my stuff with it.
The issue is that when I have several beacons in range, the didRangeBeaconsInRegion callback always provides me a Collection of only 1 beacon at a time and this beacon is a random one among all the present beacons... Why can't I get all the beacons in range in my Collection ?
All of this is made from within a Service, I did clean all the other stuff to keep only the relevant parts of the code below -> Hopefully I am doing something wrong here ?
public class MonitorService extends Service implements BeaconConsumer
{
private BeaconManager beaconManager;
#Override
public void onCreate()
{
super.onCreate();
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"));
beaconManager.setForegroundScanPeriod(5000l);
beaconManager.setBackgroundScanPeriod(5000l);
beaconManager.setForegroundBetweenScanPeriod(1100l);
beaconManager.setBackgroundBetweenScanPeriod(1100l);
setupBeaconManager();
}
private void setupBeaconManager()
{
if (!beaconManager.isBound(this))
beaconManager.bind(this);
}
private void unsetBeaconManager()
{
if (beaconManager.isBound(this))
{
beaconManager.unbind(this);
try
{
beaconManager.stopRangingBeaconsInRegion(new Region("apr", null, null, null));
}
catch (RemoteException e)
{
Log.i(TAG, "RemoteException = "+e.toString());
}
}
}
#Override
public void onBeaconServiceConnect()
{
beaconManager.setRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region)
{
Log.i(TAG,"didRangeBeaconsInRegion, number of beacons detected = "+beacons.size());
// HERE IT IS : the size is Always 1, but the beacon (UUID etc. can be different)
}
});
try
{
beaconManager.startRangingBeaconsInRegion(new Region("apr", null, null, null));
}
catch (RemoteException e)
{
Log.i(TAG, "RemoteException = "+e.toString());
}
}
#Override
public void onDestroy()
{
unsetBeaconManager();
super.onDestroy();
}
}
I'm working on Android 5.1.1 with a Nexus 6 (but a Wiko cheap phone gives the same results). The beacons are setup to advertise every 600ms... But even with 100ms it also gives the exact same results...
The code looks OK. A couple of thoughts:
Try using an off the shelf beacon scanner app based on the same library like Locate. Does it detect all of your beacons simultaneously? If not, something may be wrong with the beacons or their configuration.
Do each of your beacons have unique identifiers? The library by default only detects multiple beacons if they have unique identifiers.
I'm trying to detect a Kontakt Beacon with the following BeaconLayout:
setBeaconLayout("m:8-9=0215,i:10-13,i:14-15,i:16-17,i:18-25"));
but I don't seem to be doing it correctly. The advertising packet structure is like this:
Thanks in advance.
Thanks to #davidgyoung comments, I finally could detect my Kontakt beacon with the following code:
public class MainActivity extends Activity implements BeaconConsumer {
protected static final String TAG = "RangingActivity";
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.bind(this);
}
#Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Log.d(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) { }
}
Please note that I'm using a 2.2 version Kontakt beacon, which is a different version from the layout posted above.
A few issues with your beaconLayout:
The byte offsets in the beaconLayout string start with the manufacturer data (byte 6 in the table you show) so you need to subtract 6 from all of your offsets.
The table shows there are only three identifiers in the beacon, but your beaconLayout string has four. Note the first identifier is 16 bytes long.
If you get it working, please post the correct beaconLayout you used.