-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Doesn't work with H10 with Android 11 #246
Comments
I see. Are you sure your Galaxy S22 is on Android 11, the error indicates it is running Android 12? Android 12 introduced the new permissions for bluetooth, like BLUETOOTH_SCAN. If your phone is running Android 12, is your application requesting the permission BLUETOOTH_SCAN from the user? This is the code snippet from the example app how the permission queries are initialised for example application purposes: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
requestPermissions(arrayOf(Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT), PERMISSION_REQUEST_CODE)
} else {
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_CODE)
}
} else {
requestPermissions(arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), PERMISSION_REQUEST_CODE)
}
} |
Thanks for the quick reply. Yes, it's Android 12. Sorry. No I'm not requesting the permission. I am not scanning. IMHO it should be inherited from the SDK. It was late last night when I discovered this. I am in the process of fixing all my apps to run on 12. The main problems are with the even more limited acess to the file system. I will get to my own Bluetooth ones today. They should already be set for Android 12, including SCAN permission. If they work, I will add the permissions I used. I still think it should be done in the SDK, but I can do it. |
Ok, I got it to work, but not exactly as it should. I used the following permissions in AndroidManifest:
It still crashed, saying it needed BLUETOOTH_SCAN. I then manually set the permissions. There were two choices: Location and Nearby devices. I had previously set Location, because it asks for it. I didn't allow Nearby devices as I wasn't asked. I didn't do it manually because I didn't know what it was. It never asked for BLUETOOTH_CONNECT or BLUETOOTH_SCAN. Apparently these are presented to the user as the group Nearby devices. I do ask for permissions that are not granted at the appropriate time in other of my apps. I don't think I am asking for the above permissions from my code in this app (because there would be no appropriate place to do so). What did happen must come from your code. It isn't asking for Nearby devices (or whatever that includes) though. There is another issue with Android 12 . I notice that my apps on Android 12 are not calling onDestroy() when the back button is pressed, just onPause(). Consequently the service keeps running (what filled up the logs). I had to implement on BackPressed() and call finish() in there to fix it. I haven't yet checked that it still works on Android 10, but it probably will. |
And I would like it to not crash if Nearby devices is not granted. That's not a good user experience. Note that it is not granted the first time after installation. |
The SDK needs the permissions, discussed on README, to access bluetooth features of the Android phone. However, SDK cannot launch or actively start the permission request on behalf of the application. That why it is up to the application developer to implement the actually visible requests for the user. Very basic samples given in example app and demo app for permission request, which just follow the Android permission documentation .
This is actually a bug -> need to be fixed on SDK. Good finding. 👍 |
It's asking for location ok. The guidelines are to ask for the others when scanning is about to start as that is when they are needed. I have discovered my Bluetooth apps have the same problem (crash if not set manually). I set it up while still working with Android 10, so it wasn't really tested. So it's not just with the SDK. The OS should not be crashing apps if permissions are not given. I have some other things to work on before checking into it. It is not so critical as some of my other problems related to restricted access to the file system, and there is a work around (manually set the permissions). I'll get back. |
Back to you original post. The attached logs shows how the Polar BLE SDK has end up to forever loop because of missing BLUETOOTH_SCAN permission. However, I have hard time to reproduce the issue and fix it. I have purposely left the app without BLUETOOTH_SCAN permission and tried different ways to trigger the similar forever loop you reported. I am pretty sure that this is still bug in the SDK, I have seen this the similar logs before. Bottom line is that SDK shall not end up to forever loop even some of the required permissions are missing. For now pause the investigation, but I will come back to this later if I get the idea of actual root cause. |
I have looked into this some, but don't have it completely figured out yet. In my app the problem is in the service. I had fixed the UI to properly handle permissions, but forgot the service. The guidelines are to ask the user for ungranted permissions at the time they are needed and only proceed if not granted. The service is not a good place to present a UI to the user, so this doesn't work so well. The goal seems to be sure the service doesn't crash or do other bad things when the permissions are not granted. And this must be done if the user revokes the permissions while it is running! So it seems the job of the UI is to handle asking for permissions when needed. In the case of the SDK, since I don't know when it needs them, it seems I should not start the SDK if they are not granted. I am not sure how to handle it when they are revoked in the middle, however. It would seem to involve some notification process from the service to the UI. I am not aware of this in the SDK. I believe whatever is done must insure the app does not crash but rather gracefully handles these user decisions. Crashing is just not acceptable. Those are my thoughts as of the moment. It's not a trivial problem. |
Thinking about the forever loop, I'm not sure it is a bug. The new policy in Android 12 is not calling onDestroy when the user touches the back button, but rather to call onPause and leave it not destroyed. This means the service continues to run "forever" as it is not stopped until onDestroy. This doesn't seem to be highly publicized (nor well thought out), but it seems to be true for all apps. I fixed it by implementing onBackPressed and calling finish. It doesn't continue to run forever now, as it it did when I started this issue. |
I have spent more time than I would have liked understanding permissions. There are a lot of changes with Android 12. I believe I have my app working however. I only tested on Android 10 and 12. I will soon lose the Android 10 device though, since they give me money for trading it in. This is what is in AndroidManifest
It is slightly different from what I posted before. This is what I used in onCreate():
This is quite a bit different from the one you posted for the example app.
I am not seeing an infinite loop. I think that is owing to the behavior in Android 12 of only pausing the app, not destroying it when the back button is pressed. I don't think it is a bug in the SDK. For completeness, this is what I did:
I tried turning off permissions while the app was running. Granted, it is somewhat contentious to do this, but according to the guidelines, the app should handle it (Note that on Android 12, the only permission group is Nearby devices (no Location).) I tried turning it off while the app was running. It seems to close the app, calling onPause(), but not onDestroy(). The app does not restart. The next time it asks for the permission. I am ok with what it does. |
I spoke too soon. I forgot to actually try it with Nearby devices not granted. When I do that, it crashes. It does appear to be in an infinite loop and eventually crashes from an OutOfMemoryError. I think this is what happend the first time, when I made the original post. It was probably also what happened when I revoked the permission while it was running, but I have not explored that further.
There are two files attached. The first is from running ADB on the command line. It includes two runs: 03-11 20:53: This was the first time I tried this. The H10 was not available. These don't show the OutOfMemoryError. (Errors are not Exceptions, both are Throwables.) KE.Net-ECG-Crash-2022-03-11.2.txt The second is from the Run window in Android Studio for the second run. It has overflowed the Run window buffer, which is why I attached the the first one also. You probably only have to look at this one, though. KE.Net-ECG-Crash-2022-03-11.3.txt This is happening in the SDK. In simple terms, it seems to be getting an error and continuing to run without notifying the client or doing anything else about it. I think I have done the right things in my code about handling permissions. It seems to be ok if the permissions are granted. It is not acceptable for it to crash like this if the permissions are not granted, though. |
👍 run time permissions were introduced in Android 6.0
The location permission has been Android platform requirement for the Bluetooth until API 32. It has been really painful to explain why location permission is needed even if user just wants to use bluetooth. This actually my full understanding of the needed Permissions. Below Android 6.0:
Android 6.0 - Android 9.0
Android 10.0 - Android 11.0
Android 12.0 and above
You runtime permission request should work just fine. In theory you could add even one more
Correct. That is the big and good change in Android 31. Attribute |
I stand corrected. What you say is consistent with this article, which is probably the current definitive one. I have also verified with my own app that it needs FINE on Android 10 to scan properly. As an aside, that app now works with no permissions granted. It does not crash. It continues to do things that don't require Bluetooth and just does nothing for those that do. This is at the expense of a lot of
all over the code. Some of these were found by Lint, but not all of them. There is one other issue I don't understand. Currently with the android:minSdkVersion="31" in AndroidManifest, you would think the new permissions would not show on Android 10. However, they do: This code snippet will get all the requested permissions:
On Android 10 (for an app that is granted all the available permissions), it returns:
I don't understand this. (It returns what you would expect on Android 12, so the android:maxSdkVersion="30" works as expected.) |
Exactly. You are totally correct.
I cannot confirm that. New permissions BLUETOOTH_CONNECT and BLUETOOTH_SCAN shall only work when your app target is set to 31 and your app is running on Android12 device or above. That's my understanding and also what I see when testing on different phones. |
Did you run the code snippet? On my Android 10 device, they are there. However there is no UI to access them as far as I know, and they are not granted. So effectively it is as you say. On my Android 12 device there are only SCAN and CONNECT, and they can be granted or denied. That is what I would expect. |
It is important. The reason is that you have to do something like:
Without the build check it does "something" not "something else" on Android versions less than 12, because the permissions are there but not granted on those devices (or at least on my own Android 10 device, the only one I've checked). You should do the build check anyway, but the Lint suggested code does not include that, and it works without it, just incorrectly. |
Platform on which you observed the bug:
Device on which you observed the bug:
I got a new Galaxy S22, which runs Android 11. My app, which ran ok on Android 10, is crashing. There are so many messages some are lost in Logcat. At least one problem seems to be
Need android.permission.BLUETOOTH_SCAN permission
The Logcat contents (that weren't truncated) are attached. Note that the Logcat continued filling long after the UI was gone.
The H10 was actually not available during this run. I don't think that matters, though. It ran ok before in that situation, just never connects.
Logcat-2022-03-03.txt
The text was updated successfully, but these errors were encountered: