capacitor-community / bluetooth-le

Capacitor plugin for Bluetooth Low Energy
MIT License
285 stars 85 forks source link

[Android] Android 14 / api 34 Crashing issue on android 14 devices #663

Closed Marcello17 closed 4 months ago

Marcello17 commented 4 months ago

Describe the bug I've upgraded my app using : compileSdkVersion = 34 targetSdkVersion = 34

and it gave this error:
FATAL EXCEPTION: main

java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackagename/MainActivity}: java.lang.SecurityException: mypackagenamei: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts

I see the files show this: context.registerReceiver(stateReceiver, intentFilter) does this need to be adjusted to accommodate android 14?

To Reproduce Steps to reproduce the behavior: Set the API to 34:

compileSdkVersion = 34 targetSdkVersion = 34

Screenshots image

Plugin version:

Smartphone (please complete the following information):

peitschie commented 4 months ago

Just copying over my notes from https://github.com/don/cordova-plugin-ble-central/issues/1020#issuecomment-2226819996

Relevant documentation from Android on this:

Although these are all system broadcasts, because they're being registered via an Activity, they're effectively context-registered receivers, and thus need a RECEIVER_EXPORTED flag, as the app needs intents coming from outside the application itself (i.e., from the system).

lajith111 commented 4 months ago

Same issue happen on cordova capacitor. Please check https://github.com/cordova-sms/cordova-sms-plugin/pull/233/files Now i dont have android 14 device to check but in kotlin may be this will solve

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) //or andoid 14
{
   context.registerReceiver(bondStateReceiver, intentFilter,Context.RECEIVER_EXPORTED)
}
else
{
  context.registerReceiver(bondStateReceiver, intentFilter)
}
peitschie commented 4 months ago

@Marcello17 or @lajith111 do either you have the capacity to see if https://github.com/capacitor-community/bluetooth-le/pull/667 fixes the problem for you?

Marcello17 commented 4 months ago

@Marcello17 or @lajith111 do either you have the capacity to see if #667 fixes the problem for you?

I've tested out your solution and it didn't work BUT it looks like it's not a ble capacitor issue.

I've used your method on the @capacitor/share which also had a broadcast receiver and it worked!

But then I realized that I also had to update the @cacpacitor/share

from version '5.0.7' to the latest '6.0.1'

so in the end for me it was the @capacitor/share plugin in the SharePlugin.java file that was causing the issue

pwespi commented 4 months ago

Thank you @Marcello17 for reporting back.

I can confirm that with

compileSdkVersion = 34
targetSdkVersion = 34

my test app works fine on an Android 14 device.