From 99201fda279126f4e04ff7cf0e6ade6292dcab4b Mon Sep 17 00:00:00 2001 From: Asura146 Date: Fri, 19 Jan 2024 09:45:26 +0900 Subject: [PATCH] =?UTF-8?q?Bluetooth=E3=83=87=E3=83=90=E3=82=A4=E3=82=B9?= =?UTF-8?q?=E3=82=92=E7=99=BB=E9=8C=B2=E6=B8=88=E3=81=BF=E3=81=8B=E5=88=A4?= =?UTF-8?q?=E5=AE=9A=E3=81=99=E3=82=8B=E6=A9=9F=E8=83=BD=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 1 - .../com/example/childguard/MainActivity.java | 14 +++-- .../com/example/childguard/TestService.java | 56 ++++++++++++++++++- 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 0ad17cb..8978d23 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/app/src/main/java/com/example/childguard/MainActivity.java b/app/src/main/java/com/example/childguard/MainActivity.java index 797253a..e89413c 100644 --- a/app/src/main/java/com/example/childguard/MainActivity.java +++ b/app/src/main/java/com/example/childguard/MainActivity.java @@ -174,6 +174,8 @@ public class MainActivity extends AppCompatActivity { this.flg = false; initNotification(mDocRef); } + + } private void initNotification(DocumentReference mDocRef) { @@ -289,12 +291,12 @@ public class MainActivity extends AppCompatActivity { } -// @Override -// public void onStop() { -// super.onStop(); -// Intent intent = new Intent(getApplication(), TestService.class); -// startService(intent); -// } + @Override + public void onStop() { + super.onStop(); + Intent intent = new Intent(getApplication(), TestService.class); + startService(intent); + } //Bluetooth_setupの戻るボタン public void setupBackButton(boolean enableBackButton) { diff --git a/app/src/main/java/com/example/childguard/TestService.java b/app/src/main/java/com/example/childguard/TestService.java index 00c9c31..4547978 100644 --- a/app/src/main/java/com/example/childguard/TestService.java +++ b/app/src/main/java/com/example/childguard/TestService.java @@ -3,11 +3,15 @@ package com.example.childguard; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.Service; +import android.bluetooth.BluetoothDevice; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.pm.PackageManager; import android.os.IBinder; import android.os.Vibrator; +import android.preference.PreferenceManager; import android.util.Log; import androidx.annotation.Nullable; @@ -19,13 +23,63 @@ public class TestService extends Service { 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; + } + 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(){ //↓通知をする際に起動するバイブレーション ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(1000);