onBeaconServiceConnect 未调用

onBeaconServiceConnect not called

和以前一样,我使用 Android Beacon Library,

它已经工作了,我可以通过 BLE - Bluetooth low energy 找到信标,

但是现在,在更新到最新版本的库后,现在方法 onBeaconServiceConnect() 不再是 运行。

请告诉我我需要做什么才能使其正常工作,

谢谢,

p/s : 代码 :

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

回答

因为我使用 Fragment 而不是 Activity 作为库中的示例代码。

所以我需要做这些改变:

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

我已经 sure.before 几天了,这个信标代码可以解决 me.please check.if 任何问题,我会发送我的全部代码。

试试这个:

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

而不是代码中的这一行。

beaconManager.startRangingBeaconsInRegion(
                new Region(Constant.UUID, null, null, null));

此代码适用于 me.please 尝试此代码:

创建应用程序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();
        }

    }

}

如果您在 Fragment(而不是 ActivityServiceApplication 实例中实现 BeaconConsumer 接口,您需要链接所有的方法。像这样:

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