Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions android/hello_sdl_android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 31
compileSdkVersion 33
defaultConfig {
applicationId "com.sdl.hellosdlandroid"
minSdkVersion 16
targetSdkVersion 31
targetSdkVersion 33
versionCode 1
versionName "1.0"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
Expand Down
2 changes: 2 additions & 0 deletions android/hello_sdl_android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
tools:targetApi="31"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"
tools:targetApi="33"/>
<uses-permission android:name="android.permission.INTERNET" />
<!-- Required to check if WiFi is enabled -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.sdl.hellosdlandroid;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import static android.Manifest.permission.BLUETOOTH_CONNECT;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

Expand All @@ -23,12 +22,18 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


if (BuildConfig.TRANSPORT.equals("MULTI") || BuildConfig.TRANSPORT.equals("MULTI_HB")) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !checkPermission()) {
requestPermission();
return;
String[] permissionsNeeded = permissionsNeeded();
if (permissionsNeeded.length > 0) {
requestPermission(permissionsNeeded, REQUEST_CODE);
for (String permission : permissionsNeeded) {
if (Manifest.permission.BLUETOOTH_CONNECT.equals(permission)) {
// We need to request BLUETOOTH_CONNECT permission to connect to SDL via Bluetooth
return;
}
}
}

//If we are connected to a module we want to start our SdlService
SdlReceiver.queryForConnectedService(this);
} else if (BuildConfig.TRANSPORT.equals("TCP")){
Expand All @@ -37,24 +42,61 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

private boolean checkPermission() {
return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(getApplicationContext(), BLUETOOTH_CONNECT);
/**
* Boolean method that checks API level and check to see if we need to request BLUETOOTH_CONNECT permission
* @return false if we need to request BLUETOOTH_CONNECT permission
*/
private boolean hasBTPermission() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? checkPermission(Manifest.permission.BLUETOOTH_CONNECT) : true;
}

/**
* Boolean method that checks API level and check to see if we need to request POST_NOTIFICATIONS permission
* @return false if we need to request POST_NOTIFICATIONS permission
*/
private boolean hasPNPermission() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU ? checkPermission(Manifest.permission.POST_NOTIFICATIONS) : true;
}

private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{BLUETOOTH_CONNECT}, REQUEST_CODE);
private boolean checkPermission(String permission) {
return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(getApplicationContext(), permission);
}

private void requestPermission(String[] permissions, int REQUEST_CODE) {
ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE);
}

private @NonNull String[] permissionsNeeded() {
ArrayList<String> result = new ArrayList<>();
if (!hasBTPermission()) {
result.add(Manifest.permission.BLUETOOTH_CONNECT);
}
if (!hasPNPermission()) {
result.add(Manifest.permission.POST_NOTIFICATIONS);
}
return (result.toArray(new String[result.size()]));
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE:
if (grantResults.length > 0) {

boolean btConnectGranted = grantResults[0] == PackageManager.PERMISSION_GRANTED;

if (btConnectGranted) {
SdlReceiver.queryForConnectedService(this);
for (int i = 0; i < grantResults.length; i++) {
if (permissions[i].equals(Manifest.permission.BLUETOOTH_CONNECT)) {
boolean btConnectGranted =
grantResults[i] == PackageManager.PERMISSION_GRANTED;
if (btConnectGranted) {
SdlReceiver.queryForConnectedService(this);
}
} else if (permissions[i].equals(Manifest.permission.POST_NOTIFICATIONS)) {
boolean postNotificationGranted =
grantResults[i] == PackageManager.PERMISSION_GRANTED;
if (!postNotificationGranted) {
// User denied permission, Notifications for SDL will not appear
// on Android 13 devices.
}
}
}
}
break;
Expand Down
4 changes: 2 additions & 2 deletions android/sdl_android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 31
compileSdkVersion 33
defaultConfig {
minSdkVersion 16
targetSdkVersion 31
targetSdkVersion 33
versionCode 23
versionName new File(projectDir.path, ('/../../VERSION')).text.trim()
buildConfigField "String", "VERSION_NAME", '\"' + versionName + '\"'
Expand Down