可能的解决方法是使用新的BLE扫描API。以下是使用新API的代码示例:
implementation 'com.google.android.gms:play-services-location:17.0.0'
private BluetoothAdapter bluetoothAdapter;
private LocationCallback locationCallback;
private LocationRequest locationRequest;
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(5000)
.setFastestInterval(1000);
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
// TODO: Process location data
}
}
};
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(locationRequest, locationCallback,
null /* Looper */);
} else {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_PERMISSION_CODE);
}
locationManager.removeUpdates(locationCallback);
if (requestCode == REQUEST_PERMISSION_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted
} else {
// Permission denied
}
}
请注意,这只是基本示例代码,您需要根据自己的需求进行调整。还需要检查权限并在AndroidManifest.xml中声明必要的权限。
这个解决方法可能对于其他手机上的Altbeacon BLE库问题也有帮助。