使用altbeacon android

时间:2016-05-25 22:47:15

标签: android bluetooth altbeacon

我试图通过示例应用程序获取和Estimote信标,但无法使其工作。我查看了这两个示例,因为我复制了大部分代码,所以我看不出自己的行为与他们不同。

我正在使用的参考资料:

https://altbeacon.github.io/android-beacon-library/samples.html

http://www.software7.com/blog/creating-a-beacon-app-for-android-in-less-than-10-minutes-from-scratch/

让我感到困惑的是,我从我的应用程序中看到了这些奇怪的日志,看起来蓝牙无论出于何种原因都无法正常工作。

我知道UUID是正确的,因为我安装了另一个应用程序可以看到这个完全相同的灯塔没有问题,但是当我厌倦了构建我自己的应用程序时,我无法让它工作。蓝牙绝对开启,无线开启或关闭无关紧要。

我想知道是否有人能发现这个问题,因为我已经尝试过HTC,三星Galaxy S6和S5(Lollipo和Marshmallow)上的代码,它们都显示完全相同的日志。

来自我的应用程序的日志(无法看到信标)

example.com.beacontest D/BeaconParser: Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
example.com.beacontest D/BeaconParser: Parsing beacon layout: m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24
example.com.beacontest D/Activity: performCreate Call secproduct feature valuefalse
example.com.beacontest D/Activity: performCreate Call debug elastic valuetrue
example.com.beacontest W/BluetoothCrashResolver: Can't read macs from BluetoothCrashResolverState.txt
example.com.beacontest D/BluetoothAdapter: startLeScan(): null
example.com.beacontest D/BluetoothLeScanner: onClientRegistered() - status=133 clientIf=0
example.com.beacontest I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@149c498c time:7966418897
example.com.beacontest I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@8067c9 time:7966418898
example.com.beacontest D/BluetoothAdapter: stopLeScan()
example.com.beacontest D/BluetoothLeScanner: could not find callback wrapper
example.com.beacontest D/BluetoothAdapter: startLeScan(): null
example.com.beacontest D/BluetoothLeScanner: onClientRegistered() - status=133 clientIf=0
example.com.beacontest D/BluetoothAdapter: stopLeScan()

以下是应用中 * WORKS * 的日志(使用相同的手机,信标和操作系统)。看起来我需要在我的应用程序上为蓝牙设置,但我真的不知道它是什么。

com.prodcode.dev D/BluetoothAdapter: STATE_ON
com.prodcode D/BluetoothAdapter: STATE_ON
com.prodcode D/BluetoothLeScanner: Stop Scan
com.prodcode D/BluetoothLeScanner: Start Scan
com.prodcode D/BluetoothAdapter: STATE_ON
com.prodcode D/BluetoothAdapter: STATE_ON
com.prodcode D/BluetoothAdapter: STATE_ON
com.prodcode D/BluetoothAdapter: STATE_ON
com.prodcode D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=13
谢谢。

我的示例应用的代码:

public class MonitoringActivity extends AppCompatActivity implements BeaconConsumer {
    protected static final String TAG = "ESTIMOTE_BEACON";
    private BeaconManager beaconManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranging);
        org.altbeacon.beacon.BeaconManager altBeaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
        altBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
        altBeaconManager.setBackgroundScanPeriod(5000);
        altBeaconManager.setBackgroundBetweenScanPeriod(25000);
        beaconManager = BeaconManager.getInstanceForApplication(this);
        BeaconManager.setAndroidLScanningDisabled(true);
        altBeaconManager.bind(this);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        beaconManager.unbind(this);
    }
    public static final int BEACON_REGION_MAX = 10;
    @Override
    public void onBeaconServiceConnect() {
        final String beaconID = getApplicationContext().getString(R.string.beaconUUID).toUpperCase();
        final Region region = new Region("BEACON_TEST", Identifier.parse(beaconID), Identifier.fromInt(BEACON_REGION_MAX), null);
        beaconManager.setMonitorNotifier(new MonitorNotifier() {
            @Override
            public void didEnterRegion(Region region) {
                Log.d(TAG, "I just saw an beacon for the first time!");
                try {
                    beaconManager.startRangingBeaconsInRegion(region);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void didExitRegion(Region region) {
                Log.d(TAG, "I no longer see an beacon");
                try {
                    beaconManager.stopRangingBeaconsInRegion(region);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void didDetermineStateForRegion(int state, Region region) {
                Log.d(TAG, "I have just switched from seeing/not seeing beacons: " + state);
            }
        });

        beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                for (Beacon beacon : beacons) {
                    Log.d(TAG, "distance: " + beacon.getDistance() + " , " +
                            "id: " + beacon.getId1() + ", " +
                            "id: " + beacon.getId2() + ", " +
                            "id: " + beacon.getId3() + ", ");
                }
            }
        });
        try {
            beaconManager.startMonitoringBeaconsInRegion(region);
        } catch (RemoteException e) {
        }
    }
}

这是我的清单和gradle:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

gradle:

apply plugin: 'com.android.application'
repositories { jcenter() }
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "example.com.beacontest"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'org.altbeacon:android-beacon-library:2.+'
    compile 'com.android.support:design:23.3.0'
}

1 个答案:

答案 0 :(得分:0)

该问题与应用程序被杀并停止从信标扫描有关。

http://altbeacon.github.io/android-beacon-library/resume-after-terminate.html

如果我卸载应用程序并重新安装它可以正常工作。有没有更好的方法来解决这个问题?