BluetoothDeviceを登録する機能を追加

This commit is contained in:
asura146 committed 2024-01-16 11:49:05 +09:00
1 parent 6fe06fbf48
commit e1ec10e981
6 files changed
+138 -13

No files matched your search

@@ -1,6 +1,8 @@
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;
@@ -10,22 +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<RecyclerAdapter.ItemViewHolder> {
ArrayList<String[]> deviceList;
Context applicationContext;
View parentView;
// Constructor
public RecyclerAdapter(ArrayList<String[]> deviceList, Context applicationContext) {
public RecyclerAdapter(ArrayList<String[]> deviceList, Context applicationContext, View parentView) {
// Init
Log.d("RecyclerAdapter", "Constructor called");
this.deviceList = deviceList;
this.applicationContext = applicationContext;
this.parentView = parentView;
}
@NonNull
@@ -42,14 +48,24 @@ public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ItemVi
holder.textView.setOnClickListener( v -> {
// アラートダイアログを表示
new androidx.appcompat.app.AlertDialog.Builder(v.getContext())
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();
Toast.makeText(v.getContext(), deviceList.get(position)[1], Toast.LENGTH_SHORT).show();
PreferenceManager.getDefaultSharedPreferences(this.applicationContext).edit().putString("bluetooth_device1", deviceList.get(position)[1]).apply();
//共有プリファレンスに保存
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();