javaandroidionic-frameworkaltbeacon

Is this a correct way to override beacon parse?


Im trying to develop a simple app to detect beacon using capacitor integrated ionic framework and to run it into android platform. I have done all the delegates at homepage.ts and permissions as usual (from official docs and github examples). Now all is left is set new beacon parse to ibeacon (default is altbeacon). When i run the app, it crashed.

This is what i have tried so far (heads up im not good with Java) This is from my MainActivity.java file calling parsing file

import android.os.RemoteException;

public class MainActivity extends BridgeActivity {
    private Parsing myParsing;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Parsing myParsing = new Parsing();
        myParsing.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

and this is my Parsing.java file

package io.ionic.starter;

import com.getcapacitor.BridgeActivity;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Identifier;


import java.util.Collection;

import android.os.Bundle;
import android.util.Log;
import android.os.RemoteException;
//m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24 ibeacon

public class Parsing extends BridgeActivity implements BeaconConsumer{

    protected static final String TAG = "RangingActivity";
    BeaconManager beaconManager;
    Identifier uuidb =Identifier.parse("sumthing");
    int major=sumthing;
    int minor=sumthing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        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.bind(this);    
    }

    @Override
    public void onBeaconServiceConnect() {
        Region region = new Region("beaconTest",uuidb, Identifier.fromInt(major), Identifier.fromInt(minor));
        try {
            beaconManager.startMonitoringBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}


when run the app crash and shows that Your activity is not yet attached to the Application instance. You can't request ViewModel before onCreate call.

if i only do the parsing in main activity, it will throw something about cannot override abstract method.

Any help is highly appreciated

and oh here is the ionic info


Ionic:

   Ionic CLI                     : 6.20.8 (C:\Users\User\AppData\Roaming\npm\node_modules\@ionic\cli)
   Ionic Framework               : @ionic/angular 6.5.6
   @angular-devkit/build-angular : 15.2.0
   @angular-devkit/schematics    : 15.2.0
   @angular/cli                  : 15.2.0
   @ionic/angular-toolkit        : 6.1.0

Capacitor:

   Capacitor CLI      : 4.7.0
   @capacitor/android : 4.7.0
   @capacitor/core    : 4.7.0
   @capacitor/ios     : not installed

Utility:

   cordova-res : 0.15.4
   native-run  : 1.7.1

System:

   NodeJS : v18.14.0 (C:\Program Files\nodejs\node.exe)
   npm    : 6.14.13
   OS     : Windows 10

Solution

  • Adding the Parsing class causes problems -- on Android you cannot just create a new Activity instance whenever you want without registering it with the operating system. Try something like this:

    public class MainActivity extends BridgeActivity implements MonitorNotifer {
        BeaconManager beaconManager;
        Identifier uuidb =Identifier.parse("sumthing");
        int major=sumthing;
        int minor=sumthing;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            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"));
            Region region = new Region("beaconTest",uuidb, Identifier.fromInt(major), Identifier.fromInt(minor));
            beaconManager.startMonitoringBeacons(region);
            beaconManager.addMonitorNotifer(this);
        }
        @Override
        public void didEnterRegion(Region arg0) {
            Log.d(TAG, "did enter region.");
        }
    
        @Override
        public void didExitRegion(Region region) {
            Log.d(TAG, "did exit region.");
        }
    
        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            Log.d(TAG, "did determine state for region: "+state);
        }
    }
    

    Be sure that this MainAcitity is properly registered with your app so it is used by Ionic. If the activity is not activated, none of this code will execute.

    The code above shows simpler Autobind APIs in the Android Beacon Library that do not require calls to bind(this). These APIs are supported on Android Beacon Library 2.19+. Make sure you have that library version or higher -- if you are using an older Ionic or Cordova plugin that uses an older Android Beacon Library this might not work.