Merge pull request 'HomeFragment以外でBluetoothの状態が変化するとクラッシュする不具合を修正' (#25) from improve/fix_bt into main

Reviewed-on: #25
This commit is contained in:
asura146 2024-07-11 06:29:51 +00:00
commit b88684ef9a
2 changed files with 10 additions and 12 deletions

View File

@ -80,6 +80,12 @@ public class HomeFragment extends Fragment implements OnEventListener {
super.onResume(); super.onResume();
Log.d("HomeFragment", "onResume: called"); Log.d("HomeFragment", "onResume: called");
this.updateUiState(getIsInCarLocal()); 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() { private boolean getIsInCarLocal() {

View File

@ -427,9 +427,7 @@ 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デバイスが接続されたときの処理 //Bluetoothデバイスが接続されたときの処理
e.putBoolean("change", true); changeBluetooth(true);
e.apply();
changeBluetooth();//HomeFragmentの表示を接続にする
Log.d("BT", "Device connected"); Log.d("BT", "Device connected");
Log.d("BT_Judge", "Registered: " + registeredId); Log.d("BT_Judge", "Registered: " + registeredId);
@ -450,9 +448,7 @@ public class MainActivity extends AppCompatActivity {
//Do something if disconnected //Do something if disconnected
//デバイスが切断されたときの処理 //デバイスが切断されたときの処理
e.putBoolean("change", false); changeBluetooth(false);
e.apply();
changeBluetooth();//HomeFragmentの表示を切断にする
if (deviceHardwareAddress.equals(registeredId)) { if (deviceHardwareAddress.equals(registeredId)) {
// 5分待機する // 5分待機する
Handler handler = new Handler(); Handler handler = new Handler();
@ -471,12 +467,8 @@ public class MainActivity extends AppCompatActivity {
} }
}; };
public void changeBluetooth(){ public void changeBluetooth(boolean actual){
SharedPreferences pref=getSharedPreferences("Bluetooth_situation",MODE_PRIVATE); getSharedPreferences("Bluetooth_situation",MODE_PRIVATE).edit().putBoolean("status",actual).apply();
SharedPreferences.Editor e=pref.edit();
Boolean BluetoothConnect = pref.getBoolean("change", false);
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
((HomeFragment) fragment).onEvent2(BluetoothConnect);
} }
} }