onBeaconServiceConnect not called

℡╲_俬逩灬. 提交于 2019-12-01 11:13:46

问题


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 version of library, now method onBeaconServiceConnect() not run anymore.

Please tell me what I need to do to make it works,

Thank you,

p/s : Code :

Manifest.xml

<uses-feature
    android:name="android.hardware.bluetooth_le"
    android:required="true" />

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />

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

Java

public class FoundBeaconFragment extends Fragment
    implements BeaconConsumer {

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

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

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_found_beacon, null);

    // Set adapter
    foundBeaconAdapter = new FoundBeaconAdapter(
            getActivity(),
            R.layout.simple_list_item_found_beacon,
            mAlFoundBeacon);
    mLvFoundBeacon.setAdapter(foundBeaconAdapter);

    // Register Scan beacons feature
    register();

    return v;
}

@Override
public void onDestroy() {
    super.onDestroy();

    try {
        // Unbind scan beacon progress
        if (beaconManager != null)
            beaconManager.unbind(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void unbindService(ServiceConnection serviceConnection) {

}

// CUSTOM METHODS
private void register() {
    beaconManager = BeaconManager.getInstanceForApplication(getActivity());
    // 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.
    try {
        // todo
        beaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    } catch (Exception e) {
        e.printStackTrace();
    }

    // CAN SEE THIS LOG CAT, NO EXCEPTION
    Log.i("", "Register Service");

    beaconManager.bind(this);
}

@Override
public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(
                Collection<org.altbeacon.beacon.Beacon> beacons, Region region) {
            Log.i("", "IS_SCAN_BEACON " + FoundBeaconFragment.IS_SCAN_BEACON);

            if (FoundBeaconFragment.IS_SCAN_BEACON) {
                Log.i("", "Found " + beacons.size() + " beacon!");

                if (beacons.size() > 0) {
                    /**
                     * Begin transfer data
                     */
                    for (final org.altbeacon.beacon.Beacon beacon : beacons) {
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                getDataViaBLE(getActivity(), beacon.getId1() + "",
                                        beacon.getId2() + "", beacon.getId3() + "");
                            }
                        });
                    }
                }
            }
        }
    });

    try {
        beaconManager.startRangingBeaconsInRegion(
                new Region(Constant.UUID, null, null, null));
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

public static String UUID = "01122334-4556-6778-899a-abbccddeeff0";

ANSWER

Since I used Fragment not Activity as sample codes from the library.

So I need do these changes :

@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);
}

回答1:


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);
}



回答2:


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));



回答3:


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

    }

}


来源:https://stackoverflow.com/questions/32815275/onbeaconserviceconnect-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!