当应用程序处于后台时,AltBeacon不会检测到信标

时间:2016-04-04 09:56:01

标签: android eclipse android-studio ibeacon altbeacon

经过Eclipse和Android Studio的大量测试后,我们无法理解为什么我们的应用程序无法从应用程序类开始扫描。

只有当我从MainActivity中的按钮开始扫描时才会检测到信标(在ApplicationClass中正确调用了didEnterRegion,我在logCat中看到了正在进行的扫描)但是如果我不让扫描从MainActivity开始,那么< / p>

beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.bind(MainActivity.this);

它根本没有开始。 在这种情况下,logCat中没有任何内容出现。

我阅读了所有可能的内容,并尝试在Android Studio上启动该项目,但同样的问题也出现了。

有谁看到我错过了什么?我应该采取其他措施让扫描直接从ApplicationClass开始吗?

以下是代码:

项目属性

target=Google Inc.:Google APIs:18
android.library.reference.1=../android-beacon-library
manifestmerger.enabled=true
android.library=false

清单

<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pstm.testbeacon"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" 
android:name="com.pstm.testbeacon.ApplicationClass">
<activity
android:launchMode="singleInstance" 
android:name="com.pstm.testbeacon.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

申请类

public class ApplicationClass extends Application implements BootstrapNotifier, BeaconConsumer {



private static final String TAG = "Demo1";
private RegionBootstrap regionBootstrap;
private BeaconManager beaconManager;
int contRange = 0;
Region regionRange = new Region("apr", null, null, null);



@Override
public void onCreate()
{
    super.onCreate();
    initSingletons();
}

protected void initSingletons()
{

    BeaconManager.setsManifestCheckingDisabled(true);
    Log.d(TAG, "App started up");
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.bind(this);
}

@Override
public void onBeaconServiceConnect() {
    // TODO Auto-generated method stub
    beaconManager.setBackgroundBetweenScanPeriod(1000l);
    beaconManager.setBackgroundScanPeriod(3000l);
    beaconManager.setBackgroundMode(true);
      try {
        beaconManager.updateScanPeriods();
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
//         beaconManager.getBeaconParsers().add(new BeaconParser().
//                setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-    23,p:24-24"));
    Region region = new Region("apr", null, null, null);
    regionBootstrap = new RegionBootstrap(this, region);
}

@Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
    // TODO Auto-generated method stub      
}

@Override
public void didEnterRegion(Region arg0) {
    // TODO Auto-generated method stub
    showNotification(
            "Enter in Beacon area",
            "Thanks");  
}

@Override
public void didExitRegion(Region arg0) {
    // TODO Auto-generated method stub
    showNotification(
            "Exit From Beacon",
            "Thanks");  
}

public void showNotification(String title, String message) {
    Intent notifyIntent = new Intent(this, MainActivity.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     Random random = new Random();
    int n =  random.nextInt(9999 - 1000) + 1000;
    PendingIntent pendingIntent = PendingIntent.getActivities(this, n,
            new Intent[] { notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(this)
    .setSmallIcon(android.R.drawable.ic_dialog_info)
    .setContentTitle(title)
    .setContentText(message)
    .setAutoCancel(true)
    .setContentIntent(pendingIntent)
    .build();
    notification.defaults |= Notification.DEFAULT_SOUND;
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(n, notification);
}
}

MainActivity

public class MainActivity extends Activity implements BeaconConsumer {
private BeaconManager beaconManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.bind(MainActivity.this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBeaconServiceConnect() {

}
}

0 个答案:

没有答案