我試圖創建一個應用程序來掃描信標。前臺應用程序工作正常。但是,當我嘗試在應用程序中包含後臺掃描時,會導致應用程序崩潰。基本上,一旦應用程序啓動,應用程序要求權限,關閉並顯示後臺掃描通知。所以,每次後臺服務檢測到信標時,都會關閉應用程序。Android信標庫後臺掃描導致前臺應用程序崩潰
我想要做的是以某種方式停止後臺掃描,當應用程序進入前臺。我試過使用
regionbootstrap.disable();
但它不能解決我的問題。
我使用的是Android的燈塔庫2.11-β1
public class whereismybag extends Application implements BootstrapNotifier,
RangeNotifier, BeaconConsumer {
private static final String TAG = "wherebg";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Background scanning started up");
Region region = new Region("com.example.utsav.whereismybeacon", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
// 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(BeaconParser.EDDYSTONE_UID_LAYOUT));
// wake up the app when any beacon is seen (you can specify specific id filers in the parameters below)
backgroundPowerSaver = new BackgroundPowerSaver(this);
beaconManager.setBackgroundScanPeriod(5000L);
beaconManager.setBackgroundBetweenScanPeriod(6000L);}
private void sendNotification(String text) {
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setContentTitle("Where is My bag?")
.setContentText(text)
.setSmallIcon(R.drawable.wimbicon);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
public void didEnterRegion(Region region) {
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();
try {
beaconManager.startRangingBeaconsInRegion(region);
}
catch (RemoteException e) {
if (BuildConfig.DEBUG) Log.d(TAG, "Can't start ranging");
}
sendNotification("Detecting a bag! Tap to launch app.");
regionBootstrap.disable();
try {
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
/*Intent intent = new Intent(whereismybag.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);*/
@Override
public void didExitRegion(Region region) {
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);
}
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
Log.d(TAG,"Now finding beacon range");
}
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(this);
}}
上面的類使用啓動:
android:name=".whereismybag"
整個清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.utsav.whereismybag">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".whereismybag"
android:allowBackup="true"
android:icon="@drawable/wimbicon"
android:label="@string/app_name"
android:roundIcon="@drawable/wimbicon"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- android:name=".whereismybag" -->
<activity
android:name=".MainActivity"
android:launchMode="singleInstance"
android:screenOrientation="portrait" />
<activity
android:name=".splashscreen"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".scanning"
android:launchMode="singleInstance"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait" />
<activity
android:name=".beaconfound"
android:launchMode="singleInstance"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait" />
<activity
android:name=".save"
android:launchMode="singleInstance"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait" />
<activity
android:name=".viewsaved"
android:launchMode="singleInstance"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait" />
<activity
android:name=".findingbags"
android:launchMode="singleInstance"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait" />
<activity
android:name=".bagfound"
android:launchMode="singleTask"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait" />
<activity
android:name=".setrange"
android:launchMode="singleInstance"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait" />
<activity android:name=".beaconfoundlist"></activity>
</application>
這是在MainActivity:
public class MainActivity extends AppCompatActivity {
Button button;
/*SharedPreferences preferences = getApplicationContext().getSharedPreferences("com.utsav.whereismybag", 0);*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_ADMIN}, 1);
}
if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
if (checkSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.INTERNET}, 1);
}
}
boolean gps_enabled = false;
boolean bluetoothadmin_enabled = false;
//checking for active location
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
bluetoothadmin_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if (!gps_enabled && !bluetoothadmin_enabled) {
android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(this);
dialog.setMessage(getResources().getString(R.string.gps_network_not_enabled));
dialog.setPositiveButton(getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
}
});
dialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
}
});
dialog.show();
}
//checking for active bluetooth
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(this);
dialog.setMessage("This device does not support Bluetooth");
// Device does not support Bluetooth
} else {
if (!mBluetoothAdapter.isEnabled()) {
// Bluetooth is not enabled
android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(this);
dialog.setMessage(getResources().getString(R.string.bluetooth_not_enabled));
dialog.setPositiveButton(getResources().getString(R.string.open_bluetooth_settings), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
startActivity(myIntent);
}
});
dialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
}
});
dialog.show();
}
}
button = (Button) findViewById(R.id.setupbags);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent1 = new Intent(MainActivity.this,
scanning.class);
startActivity(myIntent1);
}
});//Start scanning
button = (Button) findViewById(R.id.savedbeac);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent1 = new Intent(MainActivity.this,
viewsaved.class);
startActivity(myIntent1);
}
});//Start scanning
button = (Button) findViewById(R.id.findbags);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent1 = new Intent(MainActivity.this,
findingbags.class);
startActivity(myIntent1);
}
});//Start scanning
button = (Button) findViewById(R.id.setrange);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent1 = new Intent(MainActivity.this,
setrange.class);
startActivity(myIntent1);
}
});//Start scanning
}}
這裏是日誌:
06-12 11:20:42.616 26716-26716/com.example.utsav.whereismybag D/.wherebg:
Background scanning started up
06-12 11:20:42.624 26716-26716/com.example.utsav.whereismybag
I/BeaconManager: BeaconManager started up on pid 26716 named
'com.example.utsav.whereismybag' for application package
'com.example.utsav.whereismybag'. isMainProcess=true
06-12 11:20:42.629 26716-26716/com.example.utsav.whereismybag
D/BeaconParser: Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-
23,p:24-24,d:25-25
06-12 11:20:42.635 26716-26716/com.example.utsav.whereismybag
D/BeaconParser: Parsing beacon layout: s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-
13,i:14-19
06-12 11:20:42.790 26716-26753/com.example.utsav.whereismybag D/AppTracker:
App Event: start
06-12 11:20:42.811 26716-26716/com.example.utsav.whereismybag
W/BluetoothCrashResolver: Can't read macs from
BluetoothCrashResolverState.txt
06-12 11:20:42.813 26716-26716/com.example.utsav.whereismybag
I/CycledLeScanner: This Android 5.0. We are using new scanning APIs
06-12 11:20:42.815 26716-26716/com.example.utsav.whereismybag
I/BeaconService: beaconService version 2.11-beta1 is starting up on the main
process
06-12 11:20:42.822 26716-26756/com.example.utsav.whereismybag
I/DpmTcmClient: RegisterTcmMonitor from:
com.android.okhttp.TcmIdleTimerMonitor
06-12 11:20:42.825 26716-26756/com.example.utsav.whereismybag
D/NetworkSecurityConfig: No Network Security Config specified, using
platform default
06-12 11:20:42.843 26716-26716/com.example.utsav.whereismybag
W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
06-12 11:20:42.843 26716-26716/com.example.utsav.whereismybag
W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
06-12 11:20:42.846 26716-26716/com.example.utsav.whereismybag I/BeaconService: starting with intent Intent { cmp=com.example.utsav.whereismybag/org.altbeacon.beacon.service.BeaconService }
06-12 11:20:42.846 26716-26716/com.example.utsav.whereismybag I/BeaconService: binding
06-12 11:20:42.857 26716-26754/com.example.utsav.whereismybag I/Adreno: QUALCOMM build : 853a1ff, I9c435c2712
Build Date : 01/10/17
OpenGL ES Shader Compiler Version: XE031.09.00.04
Local Branch :
Remote Branch :
Remote Branch :
Reconstruct Branch :
06-12 11:20:42.861 26716-26754/com.example.utsav.whereismybag I/OpenGLRenderer: Initialized EGL, version 1.4
06-12 11:20:42.861 26716-26754/com.example.utsav.whereismybag D/OpenGLRenderer: Swap behavior 1
06-12 11:20:42.867 26716-26716/com.example.utsav.whereismybag D/.wherebg: I have just switched from seeing/not seeing beacons: 0
06-12 11:20:42.946 26716-26716/com.example.utsav.whereismybag I/BeaconService: start monitoring received
06-12 11:20:44.166 26716-26756/com.example.utsav.whereismybag I/ModelSpecificDistanceCalculator: Successfully saved new distance model file
06-12 11:20:44.167 26716-26756/com.example.utsav.whereismybag W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
06-12 11:20:44.167 26716-26756/com.example.utsav.whereismybag I/ModelSpecificDistanceCalculator: Successfully updated distance model with latest from online database
06-12 11:20:45.799 26716-26819/com.example.utsav.whereismybag D/AppTracker: App Event: stop
06-12 11:20:45.860 26716-26716/com.example.utsav.whereismybag I/BeaconService: set scan intervals received
06-12 11:20:45.991 26716-26716/com.example.utsav.whereismybag W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-12 11:20:46.021 26716-26716/com.example.utsav.whereismybag V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[])
06-12 11:20:46.021 26716-26716/com.example.utsav.whereismybag V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease()
06-12 11:20:46.021 26716-26716/com.example.utsav.whereismybag V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[])
06-12 11:20:46.021 26716-26716/com.example.utsav.whereismybag V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String)
06-12 11:20:46.021 26716-26716/com.example.utsav.whereismybag V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop()
06-12 11:20:46.023 26716-26716/com.example.utsav.whereismybag V/BoostFramework: BoostFramework() : mPerf = [email protected]
06-12 11:20:46.023 26716-26716/com.example.utsav.whereismybag V/BoostFramework: BoostFramework() : mPerf = [email protected]
06-12 11:20:46.120 26716-26753/com.example.utsav.whereismybag D/AppTracker: App Event: start
06-12 11:20:46.124 26716-26756/com.example.utsav.whereismybag D/AppTracker: App Event: stop
06-12 11:20:46.137 26716-26716/com.example.utsav.whereismybag I/BeaconService: set scan intervals received
06-12 11:20:46.149 26716-26716/com.example.utsav.whereismybag I/CycledLeScanner: Adjusted scanStopTime to be 885106541
06-12 11:20:46.215 26716-26716/com.example.utsav.whereismybag I/BeaconService: set scan intervals received
06-12 11:20:46.238 26716-26716/com.example.utsav.whereismybag W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
06-12 11:20:46.314 26716-26755/com.example.utsav.whereismybag D/BluetoothAdapter: STATE_ON
06-12 11:20:46.314 26716-26755/com.example.utsav.whereismybag D/BluetoothLeScanner: could not find callback wrapper
06-12 11:20:46.318 26716-26755/com.example.utsav.whereismybag D/BluetoothAdapter: STATE_ON
06-12 11:20:46.327 26716-26729/com.example.utsav.whereismybag D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5 mClientIf=0
06-12 11:20:46.335 26716-26729/com.example.utsav.whereismybag W/Binder: Binder call failed.
java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results
at android.os.Parcel.readException(Parcel.java:1684)
at android.os.Parcel.readException(Parcel.java:1637)
at android.bluetooth.IBluetoothGatt$Stub$Proxy.startScan(IBluetoothGatt.java:678)
at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper.onClientRegistered(BluetoothLeScanner.java:367)
at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:56)
at android.os.Binder.execTransact(Binder.java:580)
06-12 11:20:48.328 26716-26755/com.example.utsav.whereismybag D/BluetoothAdapter: STATE_ON
06-12 11:20:50.089 26716-26819/com.example.utsav.whereismybag D/AppTracker: App Event: start
06-12 11:20:50.096 26716-26716/com.example.utsav.whereismybag I/BeaconService: set scan intervals received
06-12 11:20:50.102 26716-26716/com.example.utsav.whereismybag I/CycledLeScanner: Adjusted scanStopTime to be 885111115
06-12 11:20:50.555 26716-26755/com.example.utsav.whereismybag D/BluetoothAdapter: STATE_ON
06-12 11:20:50.560 26716-26728/com.example.utsav.whereismybag D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5 mClientIf=0
06-12 11:20:50.938 26716-26973/com.example.utsav.whereismybag D/.wherebg: I have just switched from seeing/not seeing beacons: 1
06-12 11:20:50.938 26716-26973/com.example.utsav.whereismybag D/.wherebg: Got a didEnterRegion call
06-12 11:20:50.959 26716-26716/com.example.utsav.whereismybag I/BeaconService: stop monitoring received
--------- beginning of crash
06-12 11:20:50.963 26716-26973/com.example.utsav.whereismybag E/AndroidRuntime: FATAL EXCEPTION: IntentService[BeaconIntentProcessor]
Process: com.example.utsav.whereismybag, PID: 26716
java.lang.NullPointerException: Attempt to invoke virtual method 'void org.altbeacon.beacon.BeaconManager.startRangingBeaconsInRegion(org.altbeacon.beacon.Region)' on a null object reference
at com.example.utsav.whereismybag.whereismybag.didEnterRegion(whereismybag.java:91)
at org.altbeacon.beacon.BeaconIntentProcessor.onHandleIntent(BeaconIntentProcessor.java:99)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:68)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)
06-12 11:20:50.963 26716-26973/com.example.utsav.whereismybag D/AppTracker: App Event: crash
06-12 11:20:50.967 26716-26716/com.example.utsav.whereismybag I/BeaconService: unbinding
06-12 11:20:50.967 26716-26716/com.example.utsav.whereismybag E/BeaconService: onDestroy()
06-12 11:20:50.967 26716-26755/com.example.utsav.whereismybag D/BluetoothAdapter: STATE_ON
06-12 11:20:50.969 26716-26716/com.example.utsav.whereismybag I/BeaconService: onDestroy called. stopping scanning
06-12 11:20:50.980 26716-26753/com.example.utsav.whereismybag D/AppTracker: App Event: stop
06-12 11:20:50.981 26716-26716/com.example.utsav.whereismybag E/BeaconManager: Cannot contact service to set scan periods
06-12 11:20:50.984 26716-26753/com.example.utsav.whereismybag E/DeviceManagerUtil: RunningAppProcessInfo is null
06-12 11:20:50.986 26716-26973/com.example.utsav.whereismybag I/Process: Sending signal. PID: 26716 SIG: 9
您可以發佈日誌貓 –
我現在已經添加了日誌。 @MuhammadYounas – uns
您正在測試哪個版本 –