gpt4 book ai didi

ibeacon - Altbeacon 在 Android10 上停止工作并且 didEnterRegion 根本没有被调用

转载 作者:行者123 更新时间:2023-12-02 05:33:27 25 4
gpt4 key购买 nike

altbeacon在 Android 10 上已经停止工作。为了升级到 Android 10,是否需要进行任何更改?

我已将以下权限添加到我的 AndroidManifest.xml

<uses-permission android:name=\"android.permission.BLUETOOTH_ADMIN\"/>
<uses-permission android:name=\"android.permission.BLUETOOTH\"/>
<uses-permission android:name=\"android.permission.ACCESS_FINE_LOCATION\"/>
<uses-permission android:name=\"android.permission.ACCESS_BACKGROUND_LOCATION\"/>
<uses-permission android:name=\"android.permission.RECEIVE_BOOT_COMPLETED\" />
<uses-permission android:name=\"android.permission.ACCESS_COARSE_LOCATION\"/>

查看应用程序权限,我的应用程序在运行时授予了位置和 BT 权限。我开始按照 https://altbeacon.github.io/android-beacon-library/samples.html 中的规定在 onBeaconServiceConnect 上寻找信标。 。我错过了什么?

最佳答案

Android 10 添加了执行 BLE 扫描和检测 BLE 信标的新权限要求 - 任何设置 targetSdkVersion 29 或更高版本的应用都需要获取如下所述的权限,否则不需要信标将被检测到。 (如果您设置 targetSdkVersion 28 或更早版本,则在 Android 10 上运行时这些新要求不适用,因为 Android 会自动授予权限。这允许旧应用在升级到 Android 10 后继续运行而不受影响。)

如果您将项目设置为 targetSdkVersion 29 或更高版本,则需要进行以下更改:

除了将这些权限添加到 list 中之外:

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

您还必须按照此处所述动态获取用户的位置权限:

private static final int PERMISSION_REQUEST_FINE_LOCATION = 1;
private static final int PERMISSION_REQUEST_BACKGROUND_LOCATION = 2;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

...


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
if (this.checkSelfPermission(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (this.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs background location access");
builder.setMessage("Please grant location access so this app can detect beacons in the background.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

@TargetApi(23)
@Override
public void onDismiss(DialogInterface dialog) {
requestPermissions(new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION},
PERMISSION_REQUEST_BACKGROUND_LOCATION);
}

});
builder.show();
}
else {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Functionality limited");
builder.setMessage("Since background location access has not been granted, this app will not be able to discover beacons in the background. Please go to Settings -> Applications -> Permissions and grant background location access to this app.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

@Override
public void onDismiss(DialogInterface dialog) {
}

});
builder.show();
}

}
} else {
if (this.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION},
PERMISSION_REQUEST_FINE_LOCATION);
}
else {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Functionality limited");
builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons. Please go to Settings -> Applications -> Permissions and grant location access to this app.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

@Override
public void onDismiss(DialogInterface dialog) {
}

});
builder.show();
}

}
}
}

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_FINE_LOCATION: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "fine location permission granted");
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Functionality limited");
builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

@Override
public void onDismiss(DialogInterface dialog) {
}

});
builder.show();
}
return;
}
case PERMISSION_REQUEST_BACKGROUND_LOCATION: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "background location permission granted");
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Functionality limited");
builder.setMessage("Since background location access has not been granted, this app will not be able to discover beacons when in the background.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

@Override
public void onDismiss(DialogInterface dialog) {
}

});
builder.show();
}
return;
}
}
}

如果您没有看到此功能,请转到“设置”->“应用程序”->“您的应用程序”->“权限”并验证是否已授予位置。

关于ibeacon - Altbeacon 在 Android10 上停止工作并且 didEnterRegion 根本没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58059097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com