diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index 2798c1d..290eb55 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -7,6 +7,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; +import android.widget.ImageView; import android.widget.TextView; import androidx.core.content.res.ResourcesCompat; @@ -132,11 +133,58 @@ public class HomeFragment extends Fragment implements OnEventListener{ return true; } + private boolean updateBluetoothSituation(Boolean Bluetoothconnect){ + FrameLayout frameLayout; + TextView textView; + ImageView imageView; + try { + frameLayout=requireView().findViewById(R.id.situation_bg2); + textView=requireView().findViewById(R.id.Bluetoothsituation); + imageView=requireView().findViewById(R.id.Bluetoothsituationimage); + }catch (NullPointerException e) { + Log.d("HomeFragment", "updateUiState: view is null"); + return false; + } catch (IllegalStateException e) { + Log.d("HomeFragment", "updateUiState: view is not attached"); + getParentFragmentManager().beginTransaction().replace(R.id.fragmentContainerView, HomeFragment.newInstance("test", "test")).commit(); + updateBluetoothSituation(Bluetoothconnect); + return false; + } + String connect="接続中"; + String disconnect="切断中"; + if (Bluetoothconnect) { + //接続状態にする + frameLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null)); + textView.setText(connect); + imageView.setVisibility(View.GONE); + } else { + //降車状態にする + frameLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null)); + textView.setText(disconnect); + imageView.setVisibility(View.VISIBLE); + } + + return true; + } + + @Override - public boolean onEvent(boolean isInCar) { + public boolean onEvent(boolean isInCar) {//乗車状態と降車状態の変更を受け取ってupdateUiState()に渡す Log.d("HomeFragment", "onEvent: called"); return updateUiState(isInCar); } + + @Override + public boolean onEvent2(boolean BluetoothConnect) { + return false; + } + + + @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 48cd6ac..a3b31d5 100644 --- a/app/src/main/java/com/example/childguard/MainActivity.java +++ b/app/src/main/java/com/example/childguard/MainActivity.java @@ -371,17 +371,21 @@ public class MainActivity extends AppCompatActivity { if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { //Do something if connected + //Bluetoothデバイスが接続されたときの処理 + Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView); + Boolean Bluetoothconnect=true; + ((HomeFragment) fragment).onEvent2(Bluetoothconnect);//HomeFragmentの表示を接続にする Log.d("BT", "Device connected"); - - Log.d("BT_Judge", "Registered: " + registeredId); if (deviceHardwareAddress.equals(registeredId)) { + //登録済みのデバイスだったときの処理 Log.d("BT_Judge", "登録済み"); e.putBoolean("connection_status",true); } else{ + //登録していないデバイスだったときの処理 Log.d("BT_Judge", "未登録"); e.putBoolean("connection_status",false); } @@ -390,6 +394,10 @@ public class MainActivity extends AppCompatActivity { } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)&&!isInCar) {//bluetoothが切断されたときに乗車状態のとき //Do something if disconnected + //デバイスが切断されたときの処理 + Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView); + Boolean Bluetoothconnect=false; + ((HomeFragment) fragment).onEvent2(Bluetoothconnect);//HomeFragmentの表示を切断にする if (deviceHardwareAddress.equals(registeredId)) { // 5分待機する Handler handler = new Handler(); @@ -400,12 +408,12 @@ public class MainActivity extends AppCompatActivity { NotificationBluetooth(getApplicationContext());//通知を行うメソッド }} - }, 5*60*1000); // 5分をミリ秒に変換 + }, 5 *60 *1000); // 5分をミリ秒に変換 } }else { Log.d("BT", "Device disconnected"); } } -}; + }; } diff --git a/app/src/main/java/com/example/childguard/OnEventListener.java b/app/src/main/java/com/example/childguard/OnEventListener.java index 8bf8757..9e967b5 100644 --- a/app/src/main/java/com/example/childguard/OnEventListener.java +++ b/app/src/main/java/com/example/childguard/OnEventListener.java @@ -3,4 +3,8 @@ package com.example.childguard; public interface OnEventListener { boolean onEvent(boolean state); + + boolean onEvent2(boolean BluetoothConnect); + + boolean onEvent2(Boolean Bluetoothconnect); } diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index e246d8e..589bae9 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -49,6 +49,7 @@