-1

I'm trying to create my own "BeaconManager" to develop different actions more easily.

So I've created a new class and I've implement "BeaconConsumer" and its functions :

public class MybeaconManager implements BeaconConsumer{

private BeaconManager beaconManager;
private final String TAG = "MybeaconManager";
private boolean mEnterArea = false;
private boolean mAlreadyArea = false;

 public MybeaconManager(Context ctx){
     beaconManager = BeaconManager.getInstanceForApplication(ctx);
     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);

 }

 public void bindBeacon(BeaconConsumer consumer){

    beaconManager.bind(consumer);

}

 public void unBindBeacon(BeaconConsumer consumer){

     beaconManager.unbind(consumer);

 }

public boolean isEnterInArea() {
    return mEnterArea;
}


public boolean isAlreadyInArea() {
    return mAlreadyArea;
}


public void sendNotification(String Notif) {

}


@Override
public void onBeaconServiceConnect() {


    beaconManager.addMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
            mEnterArea = true;
        }

        @Override
        public void didExitRegion(Region region) {
            mEnterArea = false;
        }

        @Override
        public void didDetermineStateForRegion(int i, Region region) {

        }
    });


}

Next to this, I have my MainActivity :

public class MainActivity extends Activity {

MybeaconManager mybeaconManager;
BeaconManager beaconManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mybeaconManager = new MybeaconManager(this);

    if (mybeaconManager.isEnterInArea()){
        Log.i("BeaconTest", "I'm detecting a Beacon");
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mybeaconManager.unBindBeacon((BeaconConsumer) this);
}

@Override
protected void onStart() {
    super.onStart();
}

@Override
protected void onStop() {
    super.onStop();
}


}

So as you can see, I'm trying to use the functions didEnterRegion/didExitRegion more easily in a way that I only have to use on line in my MainActivity code.

The problem is, the bind/unbind(this) don't compile well and I think it's because I don't implement "BeaconConsumer" on the MainActivity because he can't get the consumer right.

It's telling me : "Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference" and return me on the bind thing.

So do you have any ideas on how I can deal with this in a way that I keep my beaconManager ?

Thank you in advance. PS : Sorry if my English is not perfect

P. Laine
  • 19
  • 1

1 Answers1

2

BeaconConsumer interface is designed to be implemented by an Activity or Service class. If you want to implement this interface in a POJO as shown in the question, you need to chain the method definitions shown below.

@Override
public Context getApplicationContext() {
    return getActivity().getApplicationContext();
}

@Override
public void unbindService(ServiceConnection serviceConnection) {
    getActivity().unbindService(serviceConnection);
}

@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
    return getActivity().bindService(intent, serviceConnection, i);
}

I suspect your code already has empty implementations of these methods, otherwise your code would not compile. Make sure you have provided full implementations as shown above.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204