Bluetoothの切断接続に応じて、HomeFrgmentの表示を変化させる機能を実装

This commit is contained in:
it232115 2024-01-23 03:44:35 +09:00
parent 1077db14b0
commit 75b4be9ff1
4 changed files with 68 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.core.content.res.ResourcesCompat; import androidx.core.content.res.ResourcesCompat;
@ -132,11 +133,58 @@ public class HomeFragment extends Fragment implements OnEventListener{
return true; 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 @Override
public boolean onEvent(boolean isInCar) { public boolean onEvent(boolean isInCar) {//乗車状態と降車状態の変更を受け取ってupdateUiState()に渡す
Log.d("HomeFragment", "onEvent: called"); Log.d("HomeFragment", "onEvent: called");
return updateUiState(isInCar); return updateUiState(isInCar);
} }
@Override
public boolean onEvent2(boolean BluetoothConnect) {
return false;
}
@Override
public boolean onEvent2(Boolean Bluetoothconnect) {//Bluetoothの接続切断を受け取ってupdateBluetoothSituation()に渡す
updateBluetoothSituation(Bluetoothconnect);
return false;
}
} }

View File

@ -371,17 +371,21 @@ public class MainActivity extends AppCompatActivity {
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
//Do something if connected //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", "Device connected");
Log.d("BT_Judge", "Registered: " + registeredId); Log.d("BT_Judge", "Registered: " + registeredId);
if (deviceHardwareAddress.equals(registeredId)) { if (deviceHardwareAddress.equals(registeredId)) {
//登録済みのデバイスだったときの処理
Log.d("BT_Judge", "登録済み"); Log.d("BT_Judge", "登録済み");
e.putBoolean("connection_status",true); e.putBoolean("connection_status",true);
} else{ } else{
//登録していないデバイスだったときの処理
Log.d("BT_Judge", "未登録"); Log.d("BT_Judge", "未登録");
e.putBoolean("connection_status",false); e.putBoolean("connection_status",false);
} }
@ -390,6 +394,10 @@ public class MainActivity extends AppCompatActivity {
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)&&!isInCar) {//bluetoothが切断されたときに乗車状態のとき } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)&&!isInCar) {//bluetoothが切断されたときに乗車状態のとき
//Do something if disconnected //Do something if disconnected
//デバイスが切断されたときの処理
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
Boolean Bluetoothconnect=false;
((HomeFragment) fragment).onEvent2(Bluetoothconnect);//HomeFragmentの表示を切断にする
if (deviceHardwareAddress.equals(registeredId)) { if (deviceHardwareAddress.equals(registeredId)) {
// 5分待機する // 5分待機する
Handler handler = new Handler(); Handler handler = new Handler();
@ -400,12 +408,12 @@ public class MainActivity extends AppCompatActivity {
NotificationBluetooth(getApplicationContext());//通知を行うメソッド NotificationBluetooth(getApplicationContext());//通知を行うメソッド
}} }}
}, 5*60*1000); // 5分をミリ秒に変換 }, 5 *60 *1000); // 5分をミリ秒に変換
} }
}else { }else {
Log.d("BT", "Device disconnected"); Log.d("BT", "Device disconnected");
} }
} }
}; };
} }

View File

@ -3,4 +3,8 @@ package com.example.childguard;
public interface OnEventListener { public interface OnEventListener {
boolean onEvent(boolean state); boolean onEvent(boolean state);
boolean onEvent2(boolean BluetoothConnect);
boolean onEvent2(Boolean Bluetoothconnect);
} }

View File

@ -49,6 +49,7 @@
</FrameLayout> </FrameLayout>
<FrameLayout <FrameLayout
android:id="@+id/situation_bg2"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="20dp" android:layout_marginLeft="20dp"
@ -57,6 +58,7 @@
android:background="@drawable/frame_style"> android:background="@drawable/frame_style">
<TextView <TextView
android:id="@+id/Bluetoothsituation"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="5dp" android:layout_marginTop="5dp"
@ -73,6 +75,7 @@
android:textAlignment="center" /> android:textAlignment="center" />
<ImageView <ImageView
android:id="@+id/Bluetoothsituationimage"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="100dp" android:layout_height="100dp"
android:layout_marginTop="50dp" android:layout_marginTop="50dp"