diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index 9058f94..fab5664 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -80,6 +80,12 @@ public class HomeFragment extends Fragment implements OnEventListener { super.onResume(); Log.d("HomeFragment", "onResume: called"); this.updateUiState(getIsInCarLocal()); + this.updateBluetoothSituation(isBluetoothConnected()); + } + + private boolean isBluetoothConnected() { + SharedPreferences pref = requireActivity().getSharedPreferences("Bluetooth_situation", requireActivity().MODE_PRIVATE); + return pref.getBoolean("status", false); } private boolean getIsInCarLocal() { @@ -174,17 +180,5 @@ public class HomeFragment extends Fragment implements OnEventListener { return updateUiState(isInCar); } - - @Override - public void onEvent2(boolean BluetoothConnect) { - return; - } - - - @Override - public boolean onEvent2(Boolean BluetoothConnect) {//Bluetoothの接続切断を受け取ってupdateBluetoothSituation()に渡す - updateBluetoothSituation(BluetoothConnect); - return false; - } } diff --git a/app/src/main/java/com/example/childguard/MainActivity.java b/app/src/main/java/com/example/childguard/MainActivity.java index 6947e3b..f96d300 100644 --- a/app/src/main/java/com/example/childguard/MainActivity.java +++ b/app/src/main/java/com/example/childguard/MainActivity.java @@ -429,9 +429,7 @@ public class MainActivity extends AppCompatActivity { if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { //Do something if connected //Bluetoothデバイスが接続されたときの処理 - e.putBoolean("change", true); - e.apply(); - changeBluetooth();//HomeFragmentの表示を接続にする + changeBluetooth(true); Log.d("BT", "Device connected"); Log.d("BT_Judge", "Registered: " + registeredId); @@ -452,9 +450,7 @@ public class MainActivity extends AppCompatActivity { //Do something if disconnected //デバイスが切断されたときの処理 - e.putBoolean("change", false); - e.apply(); - changeBluetooth();//HomeFragmentの表示を切断にする + changeBluetooth(false); if (deviceHardwareAddress.equals(registeredId)) { // 5分待機する Handler handler = new Handler(); @@ -473,12 +469,8 @@ public class MainActivity extends AppCompatActivity { } }; - public void changeBluetooth(){ - SharedPreferences pref=getSharedPreferences("Bluetooth_situation",MODE_PRIVATE); - SharedPreferences.Editor e=pref.edit(); - Boolean BluetoothConnect = pref.getBoolean("change", false); - Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView); - ((HomeFragment) fragment).onEvent2(BluetoothConnect); + public void changeBluetooth(boolean actual){ + getSharedPreferences("Bluetooth_situation",MODE_PRIVATE).edit().putBoolean("status",actual).apply(); } } diff --git a/app/src/main/java/com/example/childguard/OnEventListener.java b/app/src/main/java/com/example/childguard/OnEventListener.java index 1782b5f..8bf8757 100644 --- a/app/src/main/java/com/example/childguard/OnEventListener.java +++ b/app/src/main/java/com/example/childguard/OnEventListener.java @@ -3,8 +3,4 @@ package com.example.childguard; public interface OnEventListener { boolean onEvent(boolean state); - - void onEvent2(boolean BluetoothConnect); - - boolean onEvent2(Boolean BluetoothConnect); }