Bluetoothの権限確認を隔離, メソッド改名など

This commit is contained in:
ろむねこ 2024-07-11 15:12:06 +09:00
parent 712d33d607
commit 2b7b8b3e9d
No known key found for this signature in database
GPG Key ID: FA1F39A1BA37D168

View File

@ -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() {