BluetoothDeviceを登録する機能を追加
This commit is contained in:
		
							parent
							
								
									6fe06fbf48
								
							
						
					
					
						commit
						e1ec10e981
					
				@ -104,6 +104,7 @@ 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();
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
@ -80,7 +80,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先のサイトを開く
 | 
			
		||||
 | 
			
		||||
@ -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();
 | 
			
		||||
 | 
			
		||||
@ -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<ItemViewHolder> {
 | 
			
		||||
 | 
			
		||||
    ArrayList<String> arrayList;
 | 
			
		||||
 | 
			
		||||
    //RecyclerAdapterのコンストラクタ
 | 
			
		||||
    public RecyclerAdapter2(ArrayList<String> 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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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<BluetoothDevice> 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<String[]> arrayList = new ArrayList<>();
 | 
			
		||||
@ -134,10 +138,16 @@ public class bluetooth_setupFragment extends Fragment {
 | 
			
		||||
                Log.d("b", s[0]);
 | 
			
		||||
            }
 | 
			
		||||
            Log.d(" ", String.valueOf(arrayList.size()));
 | 
			
		||||
            RecyclerAdapter adapter = new RecyclerAdapter(arrayList, requireActivity().getApplicationContext());
 | 
			
		||||
            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;
 | 
			
		||||
 | 
			
		||||
@ -10,17 +10,28 @@
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    <!-- TODO: Update blank fragment layout -->
 | 
			
		||||
    <TextView
 | 
			
		||||
        android:layout_width="match_parent"
 | 
			
		||||
        android:layout_height="wrap_content"
 | 
			
		||||
        android:text="登録可能デバイス"
 | 
			
		||||
        />
 | 
			
		||||
    <androidx.recyclerview.widget.RecyclerView
 | 
			
		||||
        android:id="@+id/recyclerView1"
 | 
			
		||||
        android:layout_width="match_parent"
 | 
			
		||||
        android:layout_height="match_parent"
 | 
			
		||||
        android:layout_height="wrap_content"
 | 
			
		||||
        android:scrollbars="vertical"/>
 | 
			
		||||
 | 
			
		||||
    <TextView
 | 
			
		||||
        android:id="@+id/click_device"
 | 
			
		||||
        android:layout_width="match_parent"
 | 
			
		||||
        android:layout_height="wrap_content"
 | 
			
		||||
 | 
			
		||||
        android:text="登録済みデバイス"
 | 
			
		||||
        />
 | 
			
		||||
    <TextView
 | 
			
		||||
        android:id="@+id/registered_device"
 | 
			
		||||
        android:layout_width="match_parent"
 | 
			
		||||
        android:layout_height="wrap_content"
 | 
			
		||||
        android:textSize="50dp"/>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
</LinearLayout>
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user