From fab0f75d1a69495c1173352e6c58e43cd13480bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=8D=E3=82=80=E3=81=AD=E3=81=93?= Date: Tue, 23 Jan 2024 09:45:08 +0900 Subject: [PATCH] =?UTF-8?q?=E6=A8=A9=E9=99=90=E3=81=AE=E8=A6=81=E6=B1=82?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/childguard/MainActivity.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/app/src/main/java/com/example/childguard/MainActivity.java b/app/src/main/java/com/example/childguard/MainActivity.java index 2c6ce51..8b62e07 100644 --- a/app/src/main/java/com/example/childguard/MainActivity.java +++ b/app/src/main/java/com/example/childguard/MainActivity.java @@ -23,6 +23,7 @@ import android.widget.FrameLayout; import android.widget.Toast; import androidx.activity.result.ActivityResultLauncher; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.browser.customtabs.CustomTabsIntent; import androidx.core.app.ActivityCompat; @@ -35,6 +36,9 @@ import com.google.firebase.firestore.FirebaseFirestore; import com.journeyapps.barcodescanner.ScanContract; import com.journeyapps.barcodescanner.ScanOptions; +// Manifest +import android.Manifest; + import java.util.HashMap; import java.util.Map; @@ -85,6 +89,10 @@ public class MainActivity extends AppCompatActivity { db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ + if (!hasPermissions()) { + requestPermissions(); + } + BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view); this.homeFragment = HomeFragment.newInstance("test", "test"); @@ -188,6 +196,44 @@ public class MainActivity extends AppCompatActivity { } + private boolean hasPermissions() { + return ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED && + ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADMIN) == PackageManager.PERMISSION_GRANTED && + ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED && + ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED && + ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && + ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED && + ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED && + ActivityCompat.checkSelfPermission(this, Manifest.permission.VIBRATE) == PackageManager.PERMISSION_GRANTED && + ActivityCompat.checkSelfPermission(this, Manifest.permission.FOREGROUND_SERVICE) == PackageManager.PERMISSION_GRANTED; + } + + private void requestPermissions() { + ActivityCompat.requestPermissions(this, + new String[]{ + Manifest.permission.BLUETOOTH, + Manifest.permission.BLUETOOTH_ADMIN, + Manifest.permission.BLUETOOTH_SCAN, + Manifest.permission.BLUETOOTH_CONNECT, + Manifest.permission.ACCESS_FINE_LOCATION, + Manifest.permission.ACCESS_COARSE_LOCATION, + Manifest.permission.POST_NOTIFICATIONS, + Manifest.permission.VIBRATE, + Manifest.permission.FOREGROUND_SERVICE + }, + 2); + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + if (requestCode == 2) { + if (!hasPermissions()) { + Toast.makeText(this, "Permissions not granted.", Toast.LENGTH_SHORT).show(); + } + } + } +