Bluetoothの切断接続に応じて、HomeFrgmentの表示を変化させる機能を実装
This commit is contained in:
parent
1077db14b0
commit
75b4be9ff1
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -3,4 +3,8 @@ package com.example.childguard;
|
|||
public interface OnEventListener {
|
||||
|
||||
boolean onEvent(boolean state);
|
||||
|
||||
boolean onEvent2(boolean BluetoothConnect);
|
||||
|
||||
boolean onEvent2(Boolean Bluetoothconnect);
|
||||
}
|
||||
|
|
|
@ -49,6 +49,7 @@
|
|||
</FrameLayout>
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/situation_bg2"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="20dp"
|
||||
|
@ -57,6 +58,7 @@
|
|||
android:background="@drawable/frame_style">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/Bluetoothsituation"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="5dp"
|
||||
|
@ -73,6 +75,7 @@
|
|||
android:textAlignment="center" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/Bluetoothsituationimage"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="100dp"
|
||||
android:layout_marginTop="50dp"
|
||||
|
|
Loading…
Reference in New Issue
Block a user