From 12239a269f4d0913fafc8019bd011c9d7ea51523 Mon Sep 17 00:00:00 2001 From: Asura146 Date: Wed, 17 Jan 2024 12:07:01 +0900 Subject: [PATCH] =?UTF-8?q?Bluetooth=E3=83=87=E3=83=90=E3=82=A4=E3=82=B9?= =?UTF-8?q?=E3=82=92=E7=99=BB=E9=8C=B2=E6=B8=88=E3=81=BF=E3=81=8B=E5=88=A4?= =?UTF-8?q?=E5=AE=9A=E3=81=99=E3=82=8B=E6=A9=9F=E8=83=BD=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/childguard/HomeFragment.java | 28 ++++++++- .../com/example/childguard/MainActivity.java | 62 +++++++++++++++++++ .../example/childguard/RecyclerAdapter.java | 1 - .../res/layout/fragment_bluetooth_setup.xml | 29 +++++++-- app/src/main/res/layout/recycler_row.xml | 3 +- 5 files changed, 114 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index 5b0e050..473d4cc 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -2,9 +2,16 @@ package com.example.childguard; import static android.content.ContentValues.TAG; +import android.bluetooth.BluetoothDevice; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.os.Bundle; + +import androidx.core.app.ActivityCompat; import androidx.core.content.res.ResourcesCompat; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; @@ -37,6 +44,7 @@ import com.google.firebase.firestore.FirebaseFirestore; import java.util.HashMap; import java.util.Map; +import java.util.Objects; /** * A simple {@link Fragment} subclass. @@ -172,11 +180,13 @@ public class HomeFragment extends Fragment { //デバック用ボタン view.findViewById(R.id.bt_debug).setOnClickListener( v -> { - Toast.makeText(getContext(), PreferenceManager.getDefaultSharedPreferences(getContext().getApplicationContext()).getString("bluetooth_device1", "none"), Toast.LENGTH_SHORT).show(); + Toast.makeText(getContext(), PreferenceManager.getDefaultSharedPreferences(getContext().getApplicationContext()).getString("bluetooth_device_id", "none"), Toast.LENGTH_SHORT).show(); }); + + + return view; } - @Override public void onResume() { super.onResume(); @@ -212,4 +222,16 @@ public class HomeFragment extends Fragment { // フラグメントトランザクションをコミット transaction.commit(); } -} \ No newline at end of file + + public void bluetooth_judge(String device_id){ + if(device_id.equals(PreferenceManager.getDefaultSharedPreferences(requireContext().getApplicationContext()).getString("Bluetooth_device_id","none"))){ + Log.d(" ","登録デバイスです"); + } + else { + Log.d(" ","登録デバイスではないです"); + } + } + + +} + diff --git a/app/src/main/java/com/example/childguard/MainActivity.java b/app/src/main/java/com/example/childguard/MainActivity.java index 9d01fb7..1d6a38c 100644 --- a/app/src/main/java/com/example/childguard/MainActivity.java +++ b/app/src/main/java/com/example/childguard/MainActivity.java @@ -10,8 +10,12 @@ import android.annotation.SuppressLint; import android.app.NotificationChannel; import android.app.NotificationManager; import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager; +import android.content.BroadcastReceiver; import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Vibrator; @@ -24,10 +28,16 @@ import java.util.Date; import java.util.Random; import java.util.UUID; +import android.preference.PreferenceManager; import android.util.Log; +import android.widget.TextView; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; + import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.EventListener; @@ -111,8 +121,60 @@ public class MainActivity extends AppCompatActivity { flg = true; } }); + + //Bluetooth検知機能 + 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); + } + + //Bluetoothの検知機能 + private final BroadcastReceiver receiver = new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + 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 deviceName = device.getName(); + 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", "登録済み"); + } else Log.d("BT_Judge", "未登録"); + + } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { + //Do something if disconnected + Log.d("BT", "Device disconnected"); + + } + } + }; + //↓通知のやつ public void notifyMain() { //↓通知をする際に起動するバイブレーション diff --git a/app/src/main/java/com/example/childguard/RecyclerAdapter.java b/app/src/main/java/com/example/childguard/RecyclerAdapter.java index f6a038f..3aeb1fc 100644 --- a/app/src/main/java/com/example/childguard/RecyclerAdapter.java +++ b/app/src/main/java/com/example/childguard/RecyclerAdapter.java @@ -54,7 +54,6 @@ public class RecyclerAdapter extends RecyclerView.Adapter { // OK button pressed Toast.makeText(v.getContext(), "OK button clicked", Toast.LENGTH_SHORT).show(); - //共有プリファレンスに保存 SharedPreferences sharedPreferences=PreferenceManager.getDefaultSharedPreferences(this.applicationContext); sharedPreferences.edit().putString("bluetooth_device_id", deviceList.get(position)[1]).apply(); diff --git a/app/src/main/res/layout/fragment_bluetooth_setup.xml b/app/src/main/res/layout/fragment_bluetooth_setup.xml index c040b59..df088f3 100644 --- a/app/src/main/res/layout/fragment_bluetooth_setup.xml +++ b/app/src/main/res/layout/fragment_bluetooth_setup.xml @@ -6,6 +6,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" + android:background="@color/white" tools:context=".bluetooth_setupFragment"> @@ -13,8 +14,17 @@ + + + + android:layout_height="60dp" + android:textSize="30sp" + android:textColor="@color/black" + /> diff --git a/app/src/main/res/layout/recycler_row.xml b/app/src/main/res/layout/recycler_row.xml index b1e2ab3..d066b8d 100644 --- a/app/src/main/res/layout/recycler_row.xml +++ b/app/src/main/res/layout/recycler_row.xml @@ -6,6 +6,7 @@ android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" - android:textSize="30sp"/> + android:textSize="30sp" + android:textColor="@color/black"/> \ No newline at end of file