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

This commit is contained in:
Asura146 2024-01-19 09:45:26 +09:00
parent c90d0b8f6e
commit 99201fda27
3 changed files with 63 additions and 8 deletions

View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">

View File

@ -174,6 +174,8 @@ public class MainActivity extends AppCompatActivity {
this.flg = false; this.flg = false;
initNotification(mDocRef); initNotification(mDocRef);
} }
} }
private void initNotification(DocumentReference mDocRef) { private void initNotification(DocumentReference mDocRef) {
@ -289,12 +291,12 @@ public class MainActivity extends AppCompatActivity {
} }
// @Override @Override
// public void onStop() { public void onStop() {
// super.onStop(); super.onStop();
// Intent intent = new Intent(getApplication(), TestService.class); Intent intent = new Intent(getApplication(), TestService.class);
// startService(intent); startService(intent);
// } }
//Bluetooth_setupの戻るボタン //Bluetooth_setupの戻るボタン
public void setupBackButton(boolean enableBackButton) { public void setupBackButton(boolean enableBackButton) {

View File

@ -3,11 +3,15 @@ package com.example.childguard;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.IBinder; import android.os.IBinder;
import android.os.Vibrator; import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -19,13 +23,63 @@ public class TestService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
//Bluetooth検知機能
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
audioStart(); if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
Log.d("BT", "No permission to connect bluetooth devices");
}
else {
Log.d("BT", "Permission to connect bluetooth devices granted");
}
registerReceiver(receiver, intentFilter);
//audioStart();
return START_NOT_STICKY; return START_NOT_STICKY;
} }
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");
}
}
};
private void audioStart(){ private void audioStart(){
//通知をする際に起動するバイブレーション //通知をする際に起動するバイブレーション
((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(1000); ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(1000);