11package com .sdl .hellosdlandroid ;
22
3+ import android .Manifest ;
34import android .content .Intent ;
45import android .content .pm .PackageManager ;
56import android .os .Build ;
67import android .os .Bundle ;
78import android .view .Menu ;
89import android .view .MenuItem ;
9-
1010import androidx .annotation .NonNull ;
1111import androidx .appcompat .app .AppCompatActivity ;
1212import androidx .core .app .ActivityCompat ;
1313import androidx .core .content .ContextCompat ;
14-
15- import static android .Manifest .permission .BLUETOOTH_CONNECT ;
14+ import java .util .ArrayList ;
1615
1716public class MainActivity extends AppCompatActivity {
1817
@@ -23,12 +22,18 @@ protected void onCreate(Bundle savedInstanceState) {
2322 super .onCreate (savedInstanceState );
2423 setContentView (R .layout .activity_main );
2524
26-
2725 if (BuildConfig .TRANSPORT .equals ("MULTI" ) || BuildConfig .TRANSPORT .equals ("MULTI_HB" )) {
28- if (android .os .Build .VERSION .SDK_INT >= Build .VERSION_CODES .S && !checkPermission ()) {
29- requestPermission ();
30- return ;
26+ String [] permissionsNeeded = permissionsNeeded ();
27+ if (permissionsNeeded .length > 0 ) {
28+ requestPermission (permissionsNeeded , REQUEST_CODE );
29+ for (String permission : permissionsNeeded ) {
30+ if (Manifest .permission .BLUETOOTH_CONNECT .equals (permission )) {
31+ // We need to request BLUETOOTH_CONNECT permission to connect to SDL via Bluetooth
32+ return ;
33+ }
34+ }
3135 }
36+
3237 //If we are connected to a module we want to start our SdlService
3338 SdlReceiver .queryForConnectedService (this );
3439 } else if (BuildConfig .TRANSPORT .equals ("TCP" )){
@@ -37,24 +42,61 @@ protected void onCreate(Bundle savedInstanceState) {
3742 }
3843 }
3944
40- private boolean checkPermission () {
41- return PackageManager .PERMISSION_GRANTED == ContextCompat .checkSelfPermission (getApplicationContext (), BLUETOOTH_CONNECT );
45+ /**
46+ * Boolean method that checks API level and check to see if we need to request BLUETOOTH_CONNECT permission
47+ * @return false if we need to request BLUETOOTH_CONNECT permission
48+ */
49+ private boolean hasBTPermission () {
50+ return Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ? checkPermission (Manifest .permission .BLUETOOTH_CONNECT ) : true ;
51+ }
52+
53+ /**
54+ * Boolean method that checks API level and check to see if we need to request POST_NOTIFICATIONS permission
55+ * @return false if we need to request POST_NOTIFICATIONS permission
56+ */
57+ private boolean hasPNPermission () {
58+ return Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ? checkPermission (Manifest .permission .POST_NOTIFICATIONS ) : true ;
4259 }
4360
44- private void requestPermission () {
45- ActivityCompat .requestPermissions (this , new String []{BLUETOOTH_CONNECT }, REQUEST_CODE );
61+ private boolean checkPermission (String permission ) {
62+ return PackageManager .PERMISSION_GRANTED == ContextCompat .checkSelfPermission (getApplicationContext (), permission );
63+ }
64+
65+ private void requestPermission (String [] permissions , int REQUEST_CODE ) {
66+ ActivityCompat .requestPermissions (this , permissions , REQUEST_CODE );
67+ }
68+
69+ private @ NonNull String [] permissionsNeeded () {
70+ ArrayList <String > result = new ArrayList <>();
71+ if (!hasBTPermission ()) {
72+ result .add (Manifest .permission .BLUETOOTH_CONNECT );
73+ }
74+ if (!hasPNPermission ()) {
75+ result .add (Manifest .permission .POST_NOTIFICATIONS );
76+ }
77+ return (result .toArray (new String [result .size ()]));
4678 }
4779
4880 @ Override
4981 public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
5082 switch (requestCode ) {
5183 case REQUEST_CODE :
5284 if (grantResults .length > 0 ) {
53-
54- boolean btConnectGranted = grantResults [0 ] == PackageManager .PERMISSION_GRANTED ;
55-
56- if (btConnectGranted ) {
57- SdlReceiver .queryForConnectedService (this );
85+ for (int i = 0 ; i < grantResults .length ; i ++) {
86+ if (permissions [i ].equals (Manifest .permission .BLUETOOTH_CONNECT )) {
87+ boolean btConnectGranted =
88+ grantResults [i ] == PackageManager .PERMISSION_GRANTED ;
89+ if (btConnectGranted ) {
90+ SdlReceiver .queryForConnectedService (this );
91+ }
92+ } else if (permissions [i ].equals (Manifest .permission .POST_NOTIFICATIONS )) {
93+ boolean postNotificationGranted =
94+ grantResults [i ] == PackageManager .PERMISSION_GRANTED ;
95+ if (!postNotificationGranted ) {
96+ // User denied permission, Notifications for SDL will not appear
97+ // on Android 13 devices.
98+ }
99+ }
58100 }
59101 }
60102 break ;
0 commit comments