Bluetoothデバイスを保存する機能を追加

This commit is contained in:
Asura146 2024-01-12 14:12:21 +09:00
parent a69de31fa2
commit 6fe06fbf48
6 changed files with 49 additions and 24 deletions

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -16,6 +16,8 @@ import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.print.PrintHelper; import androidx.print.PrintHelper;
import android.preference.PreferenceManager;
import android.text.PrecomputedText;
import android.util.AndroidRuntimeException; import android.util.AndroidRuntimeException;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -25,6 +27,7 @@ import android.widget.Button;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.BarcodeFormat; import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException; import com.google.zxing.WriterException;
@ -100,6 +103,10 @@ public class HomeFragment extends Fragment {
replaceFragment(new bluetooth_setupFragment()); replaceFragment(new bluetooth_setupFragment());
} }
}); });
view.findViewById(R.id.bt_debug).setOnClickListener( v -> {
Toast.makeText(getContext(), PreferenceManager.getDefaultSharedPreferences(getContext().getApplicationContext()).getString("bluetooth_device1", "none"), Toast.LENGTH_SHORT).show();
});
return view; return view;
} }

View File

@ -1,5 +1,7 @@
package com.example.childguard; package com.example.childguard;
import android.content.Context;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -16,11 +18,14 @@ public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ItemVi
ArrayList<String[]> deviceList; ArrayList<String[]> deviceList;
Context applicationContext;
// Constructor // Constructor
public RecyclerAdapter(ArrayList<String[]> deviceList) { public RecyclerAdapter(ArrayList<String[]> deviceList, Context applicationContext) {
// Init // Init
Log.d("RecyclerAdapter", "Constructor called"); Log.d("RecyclerAdapter", "Constructor called");
this.deviceList = deviceList; this.deviceList = deviceList;
this.applicationContext = applicationContext;
} }
@NonNull @NonNull
@ -35,7 +40,6 @@ public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ItemVi
public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) { public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
holder.textView.setText(deviceList.get(position)[0]); holder.textView.setText(deviceList.get(position)[0]);
holder.textView.setOnClickListener( v -> { holder.textView.setOnClickListener( v -> {
Toast.makeText(v.getContext(), deviceList.get(position)[1], Toast.LENGTH_SHORT).show();
// アラートダイアログを表示 // アラートダイアログを表示
new androidx.appcompat.app.AlertDialog.Builder(v.getContext()) new androidx.appcompat.app.AlertDialog.Builder(v.getContext())
@ -44,6 +48,8 @@ public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ItemVi
.setPositiveButton(android.R.string.ok, (dialog, which) -> { .setPositiveButton(android.R.string.ok, (dialog, which) -> {
// OK button pressed // OK button pressed
Toast.makeText(v.getContext(), "OK button clicked", Toast.LENGTH_SHORT).show(); 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();
}) })
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.show(); .show();

View File

@ -134,7 +134,7 @@ public class bluetooth_setupFragment extends Fragment {
Log.d("b", s[0]); Log.d("b", s[0]);
} }
Log.d(" ", String.valueOf(arrayList.size())); Log.d(" ", String.valueOf(arrayList.size()));
RecyclerAdapter adapter = new RecyclerAdapter(arrayList); RecyclerAdapter adapter = new RecyclerAdapter(arrayList, requireActivity().getApplicationContext());
//RecyclerViewとAdapterの結び付け //RecyclerViewとAdapterの結び付け
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
@ -144,4 +144,5 @@ public class bluetooth_setupFragment extends Fragment {
} }
} }

View File

@ -103,6 +103,16 @@
android:textColor="@color/white" android:textColor="@color/white"
android:textSize="40dp" /> android:textSize="40dp" />
<Button
android:id="@+id/bt_debug"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#778899"
android:text="Bluetooth設定"
android:textColor="@color/white"
android:textSize="40dp" />
<FrameLayout <FrameLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"