Bluetoothデバイスを登録済みか判定する機能を追加

This commit is contained in:
Asura146 2024-01-17 12:07:01 +09:00
parent 87aa82529c
commit 12239a269f
5 changed files with 114 additions and 9 deletions

View File

@ -2,9 +2,16 @@ package com.example.childguard;
import static android.content.ContentValues.TAG; 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.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.os.Bundle; import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import androidx.core.content.res.ResourcesCompat; import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
@ -37,6 +44,7 @@ import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
/** /**
* A simple {@link Fragment} subclass. * A simple {@link Fragment} subclass.
@ -172,11 +180,13 @@ public class HomeFragment extends Fragment {
//デバック用ボタン //デバック用ボタン
view.findViewById(R.id.bt_debug).setOnClickListener( v -> { 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; return view;
} }
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
@ -212,4 +222,16 @@ public class HomeFragment extends Fragment {
// フラグメントトランザクションをコミット // フラグメントトランザクションをコミット
transaction.commit(); transaction.commit();
} }
}
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(" ","登録デバイスではないです");
}
}
}

View File

@ -10,8 +10,12 @@ import android.annotation.SuppressLint;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Vibrator; import android.os.Vibrator;
@ -24,10 +28,16 @@ import java.util.Date;
import java.util.Random; import java.util.Random;
import java.util.UUID; import java.util.UUID;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; 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.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener; import com.google.firebase.firestore.EventListener;
@ -111,8 +121,60 @@ public class MainActivity extends AppCompatActivity {
flg = true; 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() { public void notifyMain() {
//通知をする際に起動するバイブレーション //通知をする際に起動するバイブレーション

View File

@ -54,7 +54,6 @@ 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();
//共有プリファレンスに保存 //共有プリファレンスに保存
SharedPreferences sharedPreferences=PreferenceManager.getDefaultSharedPreferences(this.applicationContext); SharedPreferences sharedPreferences=PreferenceManager.getDefaultSharedPreferences(this.applicationContext);
sharedPreferences.edit().putString("bluetooth_device_id", deviceList.get(position)[1]).apply(); sharedPreferences.edit().putString("bluetooth_device_id", deviceList.get(position)[1]).apply();

View File

@ -6,6 +6,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
android:background="@color/white"
tools:context=".bluetooth_setupFragment"> tools:context=".bluetooth_setupFragment">
@ -13,8 +14,17 @@
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="登録可能デバイス" android:text="デバイス"
android:textSize="35dp"
android:textColor="@color/black"
android:layout_marginTop="20dp"
/> />
<View
android:id="@+id/divider1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/black" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView1" android:id="@+id/recyclerView1"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -24,13 +34,24 @@
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="登録済みデバイス" android:text="登録デバイス"
android:textSize="35dp"
android:textColor="@color/black"
android:layout_marginTop="20dp"
/>
<View
android:id="@+id/divider2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/black"
/> />
<TextView <TextView
android:id="@+id/registered_device" android:id="@+id/registered_device"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="60dp"
android:textSize="50dp"/> android:textSize="30sp"
android:textColor="@color/black"
/>

View File

@ -6,6 +6,7 @@
android:id="@+id/textView1" android:id="@+id/textView1"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textSize="30sp"/> android:textSize="30sp"
android:textColor="@color/black"/>
</LinearLayout> </LinearLayout>