From 3a9decb0f8d263b657bbacc3ece43e19acde26a5 Mon Sep 17 00:00:00 2001 From: it232115 Date: Tue, 23 Jan 2024 02:57:28 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=A2=E3=83=97=E3=83=AA=E3=81=AB=E7=99=BB?= =?UTF-8?q?=E9=8C=B2=E3=81=95=E3=82=8C=E3=81=A6=E3=81=84=E3=82=8BBluetooth?= =?UTF-8?q?=E3=83=87=E3=83=90=E3=82=A4=E3=82=B9=E3=81=AE=E5=88=87=E6=96=AD?= =?UTF-8?q?=E6=99=82=E3=81=AB=E4=B9=97=E8=BB=8A=E7=8A=B6=E6=85=8B=E3=81=AE?= =?UTF-8?q?=E3=81=A8=E3=81=8D=EF=BC=95=E5=88=86=E9=96=93=E3=81=AE=E3=82=BF?= =?UTF-8?q?=E3=82=A4=E3=83=9E=E3=82=92=E8=B5=B7=E5=8B=95=E3=81=97=E3=80=81?= =?UTF-8?q?=EF=BC=95=E5=88=86=E5=BE=8C=E3=81=AB=E3=82=82Bluetooth=E3=81=8C?= =?UTF-8?q?=E5=88=87=E6=96=AD=E3=81=95=E3=82=8C=E3=81=9F=E3=81=BE=E3=81=BE?= =?UTF-8?q?=E3=81=A7=E4=B9=97=E8=BB=8A=E7=8A=B6=E6=85=8B=E3=81=A7=E3=81=82?= =?UTF-8?q?=E3=82=8B=E3=81=A8=E3=81=8D=E3=81=AB=E9=80=9A=E7=9F=A5=E3=81=8C?= =?UTF-8?q?=E9=80=81=E3=82=89=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82=E3=81=BE=E3=81=9F?= =?UTF-8?q?=E3=80=81=E5=90=8C=E6=A7=98=E3=81=AE=E8=A8=98=E8=BF=B0=E3=82=92?= =?UTF-8?q?MainActivity=E3=81=AB=E3=82=82=E8=A1=8C=E3=81=84=E3=80=81?= =?UTF-8?q?=E3=83=90=E3=83=83=E3=82=AF=E3=82=B0=E3=83=A9=E3=82=A6=E3=83=B3?= =?UTF-8?q?=E3=83=89=E6=99=82=E3=81=A0=E3=81=91=E3=81=A7=E3=81=AA=E3=81=8F?= =?UTF-8?q?=E3=82=A2=E3=83=97=E3=83=AA=E8=B5=B7=E5=8B=95=E6=99=82=E3=82=82?= =?UTF-8?q?=E4=BD=BF=E3=81=88=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97?= =?UTF-8?q?=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/childguard/HomeFragment.java | 1 - .../com/example/childguard/MainActivity.java | 185 ++++++++++++------ .../com/example/childguard/TestService.java | 41 ++-- 3 files changed, 146 insertions(+), 81 deletions(-) diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index e95ce6d..2798c1d 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -1,6 +1,5 @@ package com.example.childguard; -import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; diff --git a/app/src/main/java/com/example/childguard/MainActivity.java b/app/src/main/java/com/example/childguard/MainActivity.java index aefe10b..48cd6ac 100644 --- a/app/src/main/java/com/example/childguard/MainActivity.java +++ b/app/src/main/java/com/example/childguard/MainActivity.java @@ -4,9 +4,7 @@ import android.annotation.SuppressLint; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; -import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -17,11 +15,10 @@ import android.graphics.Color; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.Handler; import android.os.Vibrator; -import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.util.Log; -import android.view.View; import android.widget.FrameLayout; import android.widget.Toast; @@ -44,8 +41,6 @@ import java.util.Map; public class MainActivity extends AppCompatActivity { FirebaseFirestore db; - BluetoothManager bluetoothManager; - BluetoothAdapter bluetoothAdapter; DocumentReference mDocRef; @@ -114,6 +109,7 @@ public class MainActivity extends AppCompatActivity { .commit(); } firebaselink(); + Bluetooth_status(); return true; }); @@ -168,7 +164,7 @@ public class MainActivity extends AppCompatActivity { E.apply();//確定処理 Log.d("nt", "レスポンスを検知しました1"); //FireBaseで更新された情報の判定 - if (documentSnapshot.getBoolean("isReported") == false) {//isReportedがfalseのとき=サイト上で保護者ボタンが押されたとき + if (!documentSnapshot.getBoolean("isReported")) {//isReportedがfalseのとき=サイト上で保護者ボタンが押されたとき if (fragment instanceof HomeFragment) {//fragmentがHomeFragmentのインスタンスかの判定 // changessituation();// changessituation()メソッドを処理→アプリ側の乗降状態を変化 ((HomeFragment) fragment).onEvent(!isInCar); @@ -193,54 +189,6 @@ public class MainActivity extends AppCompatActivity { } - //Bluetoothの検知機能 - - private final BroadcastReceiver receiver = new BroadcastReceiver() { - - - //PreferenceManager.getDefaultSharedPreferences("myPreferences",Context.MODE_PRIVATE); - - @Override - public void onReceive(Context context, Intent intent) { - SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); - SharedPreferences.Editor e=pref.edit(); - String action = intent.getAction(); // may need to chain this to a recognizing function - BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); - -// HomeFragment homeFragment=new HomeFragment(); - - if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { - Log.d("BT", "No permission to connect bluetooth devices"); - return; - } - String deviceHardwareAddress = device.getAddress(); // MAC address - - if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { - //Do something if connected - Log.d("BT", "Device connected"); - - String registeredId = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("bluetooth_device_id", "none"); - - Log.d("BT_Judge", "Registered: " + registeredId); - - if (deviceHardwareAddress.equals(registeredId)) { - Log.d("BT_Judge", "登録済み"); - e.putBoolean("connection_status",true); - - } else{ - Log.d("BT_Judge", "未登録"); - e.putBoolean("connection_status",false); - } - e.apply(); - - } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { - //Do something if disconnected - Log.d("BT", "Device disconnected"); - e.putBoolean("connection_status",false); - e.apply(); - } - } - }; public void firebaselink() {//Firebaseのドキュメントの取得 @@ -325,6 +273,7 @@ public class MainActivity extends AppCompatActivity { notificationManager.notify(R.string.app_name, builder.build());//通知の表示 } + @Override public void onStop() {//アプリをバックグラウンドにした時のメソッド super.onStop(); @@ -332,5 +281,131 @@ public class MainActivity extends AppCompatActivity { startService(intent);//TestServiceを起動 } + public void NotificationBluetooth(Context context) {//実際に通知を行うメソッド + final String CHANNEL_ID = "my_channel_id"; + // 通知がクリックされたときに送信されるIntent + Intent intent = new Intent(context, MainActivity.class); + intent.setAction("OPEN_ACTIVITY"); + // PendingIntentの作成 + int requestCode = 100; + int flags = 0; + PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, intent, flags | PendingIntent.FLAG_IMMUTABLE); + + ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(2000);//バイブレーション + + @SuppressLint("NotificationTrampoline") NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "CHANNEL_ID") + .setSmallIcon(android.R.drawable.ic_menu_info_details) + .setContentTitle("子供の置き去りをしていませんか?")//通知のタイトル + .setContentText("Bluetoothと車の切断から5分が経過しました")//通知の本文 + .setContentIntent(pendingIntent)//通知をタップするとActivityへ移動する + .setAutoCancel(true)//通知をタップすると削除する + .setPriority(NotificationCompat.PRIORITY_HIGH) // プライオリティを高く設定 + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC); // ロック画面に表示する + + // NotificationChannelの作成(Android 8.0以降) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationManager notificationManager = context.getSystemService(NotificationManager.class); + if (notificationManager != null) { + NotificationChannel channel = new NotificationChannel( + CHANNEL_ID, + "Channel Name", + NotificationManager.IMPORTANCE_HIGH + ); + + channel.setDescription("Channel Description"); + channel.enableLights(true); + channel.setLightColor(Color.RED); + channel.enableVibration(true); + notificationManager.createNotificationChannel(channel); + } + } + + + NotificationManager notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE); + if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { + return; + } + notificationManager.notify(R.string.app_name, builder.build());//通知の表示 + } + + + + + + 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() { + + + //PreferenceManager.getDefaultSharedPreferences("myPreferences",Context.MODE_PRIVATE); + + @Override + public void onReceive(Context context, Intent intent) { + SharedPreferences pref=getSharedPreferences("Bluetooth_situation",MODE_PRIVATE); + SharedPreferences.Editor e=pref.edit(); + String action = intent.getAction(); // may need to chain this to a recognizing function + BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + Boolean isInCar = pref.getBoolean("isInCarPref", false); + + + if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { + Log.d("BT", "No permission to connect bluetooth devices"); + return; + } + String deviceHardwareAddress = device.getAddress(); // MAC address + + String registeredId = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("bluetooth_device_id", "none"); + + if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { + //Do something if connected + Log.d("BT", "Device connected"); + + + + Log.d("BT_Judge", "Registered: " + registeredId); + + if (deviceHardwareAddress.equals(registeredId)) { + Log.d("BT_Judge", "登録済み"); + e.putBoolean("connection_status",true); + + } else{ + Log.d("BT_Judge", "未登録"); + e.putBoolean("connection_status",false); + } + e.apply(); + + } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)&&!isInCar) {//bluetoothが切断されたときに乗車状態のとき + + //Do something if disconnected + if (deviceHardwareAddress.equals(registeredId)) { + // 5分待機する + Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)&&!isInCar) {//その後bluetoothを再接続したり降車状態になったりしていない=置き去りが発生した可能性大 + NotificationBluetooth(getApplicationContext());//通知を行うメソッド + }} + + }, 5*60*1000); // 5分をミリ秒に変換 + } + }else { + Log.d("BT", "Device disconnected"); + } + } +}; } diff --git a/app/src/main/java/com/example/childguard/TestService.java b/app/src/main/java/com/example/childguard/TestService.java index a16628e..7c5b02e 100644 --- a/app/src/main/java/com/example/childguard/TestService.java +++ b/app/src/main/java/com/example/childguard/TestService.java @@ -1,7 +1,6 @@ package com.example.childguard; import android.annotation.SuppressLint; -import android.app.Activity; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; @@ -10,35 +9,24 @@ import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.graphics.Color; import android.os.Build; -import android.os.Bundle; import android.os.Handler; import android.os.IBinder; -import android.os.Looper; import android.os.Vibrator; import android.preference.PreferenceManager; import android.util.Log; -import android.widget.Toast; import androidx.annotation.Nullable; import androidx.core.app.ActivityCompat; import androidx.core.app.NotificationCompat; -import androidx.recyclerview.widget.DividerItemDecoration; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.FirebaseFirestore; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -48,8 +36,6 @@ public class TestService extends Service { public static final String TAG = "InspirationQuote"; -; - @Override public int onStartCommand(Intent intent, int flags, int startId) { @@ -95,9 +81,7 @@ public class TestService extends Service { NotificationSetting();//通知に関する設定のメソッド Notification(getApplicationContext());//通知を行うメソッド } - } else if(!isInCar){//isReportedがfalse=サイト上で降車状態のとき - ResetReported();//ResetReported();を処理→FireBaseのisReportedをfalseにする - }else { + } else {//isReportedがfalse=サイト上で降車状態のとき ResetReported();//ResetReported();を処理→FireBaseのisReportedをfalseにする } } @@ -248,10 +232,11 @@ public class TestService extends Service { @Override public void onReceive(Context context, Intent intent) { - SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + SharedPreferences pref=getSharedPreferences("Bluetooth_situation",MODE_PRIVATE); SharedPreferences.Editor e=pref.edit(); String action = intent.getAction(); // may need to chain this to a recognizing function BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + Boolean isInCar = pref.getBoolean("isInCarPref", false); if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { @@ -280,17 +265,23 @@ public class TestService extends Service { } e.apply(); - } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { + } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)&&!isInCar) {//bluetoothが切断されたときに乗車状態のとき + //Do something if disconnected if (deviceHardwareAddress.equals(registeredId)) { - Log.d("BT_Judge", "登録済み切断"); - e.putBoolean("connection_status",false); + // 5分待機する + Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)&&!isInCar) {//その後bluetoothを再接続したり降車状態になったりしていない=置き去りが発生した可能性大 + NotificationBluetooth(getApplicationContext());//通知を行うメソッド + }} + }, 5 *60 *1000); // 5分をミリ秒に変換 } + }else { Log.d("BT", "Device disconnected"); - e.apply(); - NotificationBluetooth(getApplicationContext()); - } } };