diff --git a/app/src/androidTest/java/com/example/childguard/Bluetooth_device_save.java b/app/src/androidTest/java/com/example/childguard/Bluetooth_device_save.java deleted file mode 100644 index de0df1c..0000000 --- a/app/src/androidTest/java/com/example/childguard/Bluetooth_device_save.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.example.childguard; - -import android.content.Context; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; - -import androidx.appcompat.app.AppCompatActivity; - -public class Bluetooth_device_save extends AppCompatActivity { - final SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(this); - public Bluetooth_device_save() { - } - public void device_save(String deviceAddress){ - SharedPreferences.Editor e=pref.edit(); - e.putString("bluetooth_device1",deviceAddress); - e.apply(); - } - public String device_info(){ - return pref.getString("bluetooth_device1","not_device"); - } -} diff --git a/app/src/main/java/com/example/childguard/Bluetooth_device_save.java b/app/src/main/java/com/example/childguard/Bluetooth_device_save.java new file mode 100644 index 0000000..5673005 --- /dev/null +++ b/app/src/main/java/com/example/childguard/Bluetooth_device_save.java @@ -0,0 +1,22 @@ +package com.example.childguard; + +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; + +import androidx.appcompat.app.AppCompatActivity; + +public class Bluetooth_device_save { + SharedPreferences pref; + +// public void device_save(String deviceAddress){ +// pref = PreferenceManager.getDefaultSharedPreferencesName() +// SharedPreferences.Editor e=pref.edit(); +// e.putString("bluetooth_device1",deviceAddress); +// e.apply(); +// } + public String device_info(){ + return pref.getString("bluetooth_device1","not_device"); + } +} diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index 1e46b64..7d04874 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -3,9 +3,25 @@ package com.example.childguard; import static android.content.ContentValues.TAG; import static android.content.Context.MODE_PRIVATE; +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; +import androidx.fragment.app.FragmentTransaction; +import androidx.print.PrintHelper; + import android.preference.PreferenceManager; +import android.text.PrecomputedText; +import android.util.AndroidRuntimeException; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -28,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. @@ -151,10 +168,20 @@ public class HomeFragment extends Fragment { }); //bluetooth設定ボタンの処理 Button bt2 = view.findViewById(R.id.Bluetooth_setup); - bt2.setOnClickListener(v -> replaceFragment(new bluetooth_setupFragment())); + bt2.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + replaceFragment(new bluetooth_setupFragment()); + } + }); + + //デバック用ボタン + view.findViewById(R.id.bt_debug).setOnClickListener( v -> { + Toast.makeText(getContext(), PreferenceManager.getDefaultSharedPreferences(getContext().getApplicationContext()).getString("bluetooth_device_id", "none"), Toast.LENGTH_SHORT).show(); + }); + return view; } - @Override public void onResume() { super.onResume(); @@ -200,4 +227,5 @@ public class HomeFragment extends Fragment { } -} \ No newline at end of file +} + diff --git a/app/src/main/java/com/example/childguard/MainActivity.java b/app/src/main/java/com/example/childguard/MainActivity.java index 23928bd..165ab5c 100644 --- a/app/src/main/java/com/example/childguard/MainActivity.java +++ b/app/src/main/java/com/example/childguard/MainActivity.java @@ -10,9 +10,13 @@ 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.SharedPreferences; +import android.content.Intent; +import android.content.IntentFilter; import android.content.pm.PackageManager; import android.graphics.Color; import android.os.Bundle; @@ -34,6 +38,9 @@ import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.res.ResourcesCompat; +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; @@ -162,8 +169,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/QRFragment.java b/app/src/main/java/com/example/childguard/QRFragment.java index 59e21e7..0d694ae 100644 --- a/app/src/main/java/com/example/childguard/QRFragment.java +++ b/app/src/main/java/com/example/childguard/QRFragment.java @@ -82,7 +82,8 @@ public class QRFragment extends Fragment { //読み取ったQRコードがChiled Guard用サイトのドメインを含むかの判定 if(!((result.getContents()).contains("https://practicefirestore1-8808c.web.app/"))) { Toast.makeText(getContext(), "Chiled Guardに対応するQRコードではありません", Toast.LENGTH_LONG).show(); - } else { + } + else { //URLの表示 Toast.makeText(getContext(), result.getContents(), Toast.LENGTH_SHORT).show(); //ブラウザを起動し、URL先のサイトを開く diff --git a/app/src/main/java/com/example/childguard/RecyclerAdapter.java b/app/src/main/java/com/example/childguard/RecyclerAdapter.java index ea0eea1..3aeb1fc 100644 --- a/app/src/main/java/com/example/childguard/RecyclerAdapter.java +++ b/app/src/main/java/com/example/childguard/RecyclerAdapter.java @@ -1,5 +1,9 @@ package com.example.childguard; +import android.bluetooth.BluetoothAdapter; +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -8,19 +12,26 @@ import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; +import java.util.function.Predicate; public class RecyclerAdapter extends RecyclerView.Adapter { ArrayList deviceList; + Context applicationContext; + View parentView; + // Constructor - public RecyclerAdapter(ArrayList deviceList) { + public RecyclerAdapter(ArrayList deviceList, Context applicationContext, View parentView) { // Init Log.d("RecyclerAdapter", "Constructor called"); this.deviceList = deviceList; + this.applicationContext = applicationContext; + this.parentView = parentView; } @NonNull @@ -35,7 +46,28 @@ public class RecyclerAdapter extends RecyclerView.Adapter { - Toast.makeText(v.getContext(), deviceList.get(position)[1], Toast.LENGTH_SHORT).show(); + + // アラートダイアログを表示 + new AlertDialog.Builder(v.getContext()) + .setTitle("登録") + .setMessage("このデバイスを登録しますか?") + .setPositiveButton(android.R.string.ok, (dialog, which) -> { + // 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(); + sharedPreferences.edit().putString("bluetooth_device_name",deviceList.get(position)[0]).apply(); + Toast.makeText(v.getContext(),PreferenceManager.getDefaultSharedPreferences(this.applicationContext).getString("bluetooth_device_id","none"), Toast.LENGTH_SHORT).show(); + + TextView textView = this.parentView.findViewById(R.id.registered_device); + textView.setText(PreferenceManager.getDefaultSharedPreferences(this.applicationContext).getString("bluetooth_device_name","none")); + + + + }) + .setNegativeButton(android.R.string.cancel, null) + .show(); }); } diff --git a/app/src/main/java/com/example/childguard/RecyclerAdapter2.java b/app/src/main/java/com/example/childguard/RecyclerAdapter2.java new file mode 100644 index 0000000..c2e2606 --- /dev/null +++ b/app/src/main/java/com/example/childguard/RecyclerAdapter2.java @@ -0,0 +1,86 @@ +package com.example.childguard; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; + +//RecyclerView.Adapterクラスを継承 +public class RecyclerAdapter2 extends RecyclerView.Adapter { + + ArrayList arrayList; + + //RecyclerAdapterのコンストラクタ + public RecyclerAdapter2(ArrayList arrayList, Context applicationContext) { + this.arrayList = arrayList; + } + + //新しいViewHolderを生成すると呼び出される + @NonNull + @Override + public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + + //recycler_row.xmlをactivity_main.xmlの部品にする + View view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.recycler_row, parent, false); + + //新しいViewHolderを作成 + //ItemViewHolderクラスを呼び出す + ItemViewHolder holder = new ItemViewHolder(view); + + //クリックイベントを登録 + holder.itemView.setOnClickListener(v -> { + //クリックされた行を取得 + int position = holder.getAdapterPosition(); + + //クリックされた文字をトースト表示 + Toast.makeText(v.getContext(), arrayList.get(position), Toast.LENGTH_SHORT).show(); + + //クリックされた行を削除 + arrayList.remove(position); + + //行が削除されたことを画面に通知 + notifyItemRemoved(position); + }); + + //生成したViewHolderを戻す + return holder; + } + + //1行分のレイアウトの詳細設定 + @Override + public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) { + //指定された位置の値を取得 + holder.getTextView().setText(arrayList.get(position)); + } + + //ArrayListのデータ件数を取得 + @Override + public int getItemCount() { + return arrayList.size(); + } +} + +//RecyclerView.ViewHolderクラスを継承 +class ItemViewHolder extends RecyclerView.ViewHolder { + private final TextView textView; + + //ItemViewHolderのコンストラクタ + public ItemViewHolder(View view) { + super(view); + //ViewHolderのビューにテキストを定義する + textView = view.findViewById(R.id.textView1); + } + + //テキストの値を取得 + public TextView getTextView() { + return textView; + } +} diff --git a/app/src/main/java/com/example/childguard/bluetooth_setupFragment.java b/app/src/main/java/com/example/childguard/bluetooth_setupFragment.java index 2507185..14d4044 100644 --- a/app/src/main/java/com/example/childguard/bluetooth_setupFragment.java +++ b/app/src/main/java/com/example/childguard/bluetooth_setupFragment.java @@ -1,5 +1,6 @@ package com.example.childguard; +import android.annotation.SuppressLint; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; @@ -15,11 +16,13 @@ import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import android.preference.PreferenceManager; import android.util.Log; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.widget.TextView; import java.util.ArrayList; import java.util.Objects; @@ -100,9 +103,9 @@ public class bluetooth_setupFragment extends Fragment { } Set pairedDevices = bluetoothAdapter.getBondedDevices(); - RecyclerView recyclerView = view.findViewById(R.id.recyclerView1); + RecyclerView recyclerView1 = view.findViewById(R.id.recyclerView1); //RecyclerViewのサイズを固定 - recyclerView.setHasFixedSize(true); + recyclerView1.setHasFixedSize(true); //RecyclerViewに区切り線を入れる // RecyclerView.ItemDecoration itemDecoration = @@ -111,7 +114,8 @@ public class bluetooth_setupFragment extends Fragment { //レイアウトマネージャを設 RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext()); - recyclerView.setLayoutManager(layoutManager); + recyclerView1.setLayoutManager(layoutManager); + //recyclerView2.setLayoutManager(layoutManager); //①リスト構造(String型の可変長の配列)を宣言 ArrayList arrayList = new ArrayList<>(); @@ -134,14 +138,21 @@ public class bluetooth_setupFragment extends Fragment { Log.d("b", s[0]); } Log.d(" ", String.valueOf(arrayList.size())); - RecyclerAdapter adapter = new RecyclerAdapter(arrayList); + RecyclerAdapter adapter = new RecyclerAdapter(arrayList, requireActivity().getApplicationContext(), view); + //④RecyclerViewとAdapterの結び付け - recyclerView.setAdapter(adapter); + recyclerView1.setAdapter(adapter); + TextView textView=view.findViewById(R.id.registered_device); + textView.setText(PreferenceManager.getDefaultSharedPreferences(requireActivity().getApplicationContext()).getString("bluetooth_device_name","none")); + + + } return view; } + } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_bluetooth_setup.xml b/app/src/main/res/layout/fragment_bluetooth_setup.xml index 84e47b3..df088f3 100644 --- a/app/src/main/res/layout/fragment_bluetooth_setup.xml +++ b/app/src/main/res/layout/fragment_bluetooth_setup.xml @@ -6,21 +6,53 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" + android:background="@color/white" tools:context=".bluetooth_setupFragment"> + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 1bf7ef2..af0b1f2 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -107,6 +107,16 @@ android:textColor="@color/white" android:textSize="40dp" /> +