From 2b7b8b3e9d29d5c1e1135c98283996047be3dd4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=8D=E3=82=80=E3=81=AD=E3=81=93?= Date: Thu, 11 Jul 2024 15:12:06 +0900 Subject: [PATCH] =?UTF-8?q?Bluetooth=E3=81=AE=E6=A8=A9=E9=99=90=E7=A2=BA?= =?UTF-8?q?=E8=AA=8D=E3=82=92=E9=9A=94=E9=9B=A2,=20=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E6=94=B9=E5=90=8D=E3=81=AA=E3=81=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/childguard/TestService.java | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/example/childguard/TestService.java b/app/src/main/java/com/example/childguard/TestService.java index 8db0f4d..1bc50ff 100644 --- a/app/src/main/java/com/example/childguard/TestService.java +++ b/app/src/main/java/com/example/childguard/TestService.java @@ -51,7 +51,9 @@ public class TestService extends Service { initNotification(mDocRef);//現在の位置を引数に initNotification()を処理 } - Bluetooth_status(); + if (isNotBluetoothGranted()) return flags; + + registerReceiver(receiver); return flags; } @@ -98,6 +100,32 @@ public class TestService extends Service { } } + /** + * Bluetoothの権限が許可されているかどうかを確認 + * @return Bluetoothの権限の有無 true: 許可されていない false: 許可されている + */ + private boolean isNotBluetoothGranted() { + if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { + Log.d(TAG, "Bluetoothの権限が許可されていません"); + return true; + } else { + Log.d(TAG, "Bluetoothの権限が許可されています"); + return false; + } + } + + /** + * ブロードキャストレシーバーを登録 + * @param receiver ブロードキャストレシーバー + */ + public void registerReceiver(BroadcastReceiver receiver) { + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); + intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); + + registerReceiver(receiver, intentFilter); + } + private void initNotification(DocumentReference mDocRef) {//サイト上で押されたボタンの管理 // PeriodicTaskManagerのインスタンス化 @@ -205,21 +233,6 @@ public class TestService extends Service { } - public void Bluetooth_status() { - IntentFilter intentFilter = new IntentFilter(); - intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); - intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); - - if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { - Log.d("BT", "No permission to connect bluetooth devices"); - return; - } else { - Log.d("BT", "Permission to connect bluetooth devices granted"); - } - registerReceiver(receiver, intentFilter); - } - - private final BroadcastReceiver receiver = new BroadcastReceiver() {