Haris
Haris

Reputation: 14053

iBeacon not detecting nearby beacon device

I am not able to detect nearby beacons, I am following the code from here, where using beacon scan app I am able to detect the beacon, what could be the issue.

package com.example.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.MonitorNotifier;
import org.altbeacon.beacon.Region;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class MainActivity extends Activity implements BeaconConsumer{

    private BeaconManager beaconManager;
    protected static final String TAG = "MonitoringActivity";
    EditText editText;// = (EditText)findViewById(R.id.reg_firstname);
    private String line;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText =  (EditText)findViewById(R.id.log);
        beaconManager = BeaconManager.getInstanceForApplication(this);

         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!");        
                editText.append("I just saw an beacon for the first time!");
            }

            @Override
            public void didExitRegion(Region region) {
                Log.i(TAG, "I no longer see an beacon");
                editText.append("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);   
                editText.append("I have just switched from seeing/not seeing beacons: "+state);
                }
            });

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



}

Manifest

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="23" />


<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.BLUETOOTH" />
 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
 <uses-permission android:name="android.permission.READ_LOGS" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

     <activity
        android:name="com.example.test.RegisterActivity"
        android:label="title_register" >

     </activity>

     <service android:enabled="true"
         android:exported="true" 
         android:isolatedProcess="false" 
         android:label="beacon" 
         android:name="org.altbeacon.beacon.service.BeaconService">
    </service> 

    <service android:enabled="true" 
         android:name="org.altbeacon.beacon.BeaconIntentProcessor"> 
    </service>

</application>

Edit:

As per the answer below I have added below piece of code inside onCreate()

        beaconManager.getBeaconParsers().clear();
        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"));//ALTBEACON_LAYOUT
        beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));//EDDYSTONE_UID_LAYOUT
        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"));//Kontakt beacon
        beaconManager.setForegroundScanPeriod(1000l);
        beaconManager.setForegroundBetweenScanPeriod(10000l);

But it showing only one beacon device. But using the application here https://play.google.com/store/apps/details?id=com.radiusnetworks.locate&hl=en I am able to detect two beacons.

Upvotes: 0

Views: 421

Answers (1)

davidgyoung
davidgyoung

Reputation: 64916

By default, the Android Beacon Library will only detect AltBeacon transmissions. If you want it to detect other beacon formats like Eddystone or iBeacon you must supply a BeaconParser format for that beacon type. You can easily find different BeaconParser expressions for beacon types by doing a Google search.

Upvotes: 1

Related Questions