onBeaconServiceConnect不被调用 [英] onBeaconServiceConnect not called

查看:84
本文介绍了onBeaconServiceConnect不被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像以前一样,我使用Android信标库,

As before, I work with Android Beacon Library,

它已经工作了,我可以通过BLE-低功耗蓝牙找到信标,

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

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

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,

谢谢,

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

ANSWER

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

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


推荐答案

如果要实现< 片段中的code> BeaconConsumer 接口(而不是 Activity Service Application 实例),则需要链接所有方法。像这样:

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

这篇关于onBeaconServiceConnect不被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆