onBeaconServiceConnect not called

后端 未结 3 1880
滥情空心
滥情空心 2021-01-16 06:04

As before, I work with Android Beacon Library,

It already worked and I can found out beacon via BLE - Bluetooth low energy,

But now, after updated to latest

相关标签:
3条回答
  • 2021-01-16 06:23

    I am not sure.before few days ,this beacon code is working for me.please check.if any issue ,i will send whole my code.

    try this one:

     beaconManager.startRangingBeaconsInRegion(new Region("myBeaons", Identifier.parse(UUID), null, null));
    

    instead of this line in your code.

    beaconManager.startRangingBeaconsInRegion(
                    new Region(Constant.UUID, null, null, null));
    
    0 讨论(0)
  • 2021-01-16 06:27

    If you are implementing the BeaconConsumer interface in a Fragment (and not an Activity, Service or Application instance), you need to chain all of the methods. Like this:

    @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);
    }
    
    0 讨论(0)
  • 2021-01-16 06:33

    this Code is working for me.please try this code:

    Create Application Class:

    public class BeaconReferenceApplication extends Application implements BootstrapNotifier {
        private BackgroundPowerSaver backgroundPowerSaver;
        private boolean haveDetectedBeaconsSinceBoot = false;
        private MonitoringActivity monitoringActivity = null;
        private String UUID = "23542266-18D1-4FE4-B4A1-23F8195B9D39";
    
    
        private static final String TAG = ".MyApplicationName";
        private RegionBootstrap regionBootstrap;
    
        @Override
        public void onCreate() {
            super.onCreate();
            Log.d(TAG, "App started up");
            BeaconManager 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"));
    
            // wake up the app when any beacon is seen (you can specify specific id filers in the parameters below)
            Region region = new Region("com.example.myapp.boostrapRegion", null, null, null);
            regionBootstrap = new RegionBootstrap(this, region);
        }
    
        @Override
        public void didDetermineStateForRegion(int arg0, Region arg1) {
            // Don't care
        }
    
        @Override
        public void didEnterRegion(Region arg0) {
            Log.d(TAG, "Got a didEnterRegion call");
            // This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched)
            // if you want the Activity to launch every single time beacons come into view, remove this call.
            regionBootstrap.disable();
            Intent intent = new Intent(this, MainActivity.class);
            // IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
            // created when a user launches the activity manually and it gets launched from here.
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            this.startActivity(intent);
        }
    
        @Override
        public void didExitRegion(Region arg0) {
            // Don't care
        }
    }
    

    Activity Class

    public class MainActivity extends Activity implements BeaconConsumer {
    
        public static final String TAG = "BeaconsEverywhere";
        private BeaconManager beaconManager;
        private String UUID = "23542266-18D1-4FE4-B4A1-23F8195B9D39";
    
        @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,d:25-25"));
    
            beaconManager.bind(this);
    
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            beaconManager.unbind(this);
        }
    
        @Override
        public void onBeaconServiceConnect() {
            final Region region = new Region("myBeaons", Identifier.parse(UUID), null, null);
    
            beaconManager.setMonitorNotifier(new MonitorNotifier() {
                @Override
                public void didEnterRegion(Region region) {
                    try {
                        Log.d(TAG, "didEnterRegion");
                        beaconManager.startRangingBeaconsInRegion(region);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
    
                @Override
                public void didExitRegion(Region region) {
                    try {
                        Log.d(TAG, "didExitRegion");
                        beaconManager.stopRangingBeaconsInRegion(region);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
    
                @Override
                public void didDetermineStateForRegion(int i, Region region) {
    
                }
            });
    
            beaconManager.setRangeNotifier(new RangeNotifier() {
                @Override
                public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                    for (Beacon oneBeacon : beacons) {
                        Log.d(TAG, "distance: " + oneBeacon.getDistance() + " id:" + oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" + oneBeacon.getId3());
                    }
                }
            });
    
            try {
                beaconManager.startMonitoringBeaconsInRegion(region);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题