Merge pull request #21 from child-guardian/kyou/Bluetooth

Bluetoothの接続状態を返却する機能を追加
This commit is contained in:
Asura146 2024-01-23 01:14:53 +09:00 committed by GitHub
commit 944f08f988
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 98 additions and 3 deletions

View File

@ -6,7 +6,7 @@
<GradleProjectSettings>
<option name="testRunner" value="GRADLE" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="Embedded JDK" />
<option name="gradleJvm" value="jbr-17" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />

View File

@ -18,6 +18,7 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Vibrator;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
@ -46,6 +47,7 @@ public class MainActivity extends AppCompatActivity {
BluetoothManager bluetoothManager;
BluetoothAdapter bluetoothAdapter;
DocumentReference mDocRef;
private HomeFragment homeFragment;
@ -81,6 +83,11 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Bluetooth接続判定用
SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor e=pref.edit();
e.putBoolean("connection_status",false);
db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ
BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
@ -187,12 +194,19 @@ public class MainActivity extends AppCompatActivity {
//Bluetoothの検知機能
private final BroadcastReceiver receiver = new BroadcastReceiver() {
//PreferenceManager.getDefaultSharedPreferences("myPreferences",Context.MODE_PRIVATE);
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor e=pref.edit();
String action = intent.getAction(); // may need to chain this to a recognizing function
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// HomeFragment homeFragment=new HomeFragment();
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
@ -211,12 +225,19 @@ public class MainActivity extends AppCompatActivity {
if (deviceHardwareAddress.equals(registeredId)) {
Log.d("BT_Judge", "登録済み");
} else Log.d("BT_Judge", "未登録");
e.putBoolean("connection_status",true);
} else{
Log.d("BT_Judge", "未登録");
e.putBoolean("connection_status",false);
}
e.apply();
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//Do something if disconnected
Log.d("BT", "Device disconnected");
e.putBoolean("connection_status",false);
e.apply();
}
}
};

View File

@ -64,6 +64,8 @@ public class TestService extends Service {
mDocRef = FirebaseFirestore.getInstance().document("status/" + IdPref);//現在の位置を取得
initNotification(mDocRef);//現在の位置を引数に initNotification()を処理
}
Bluetooth_status();
return flags;
@ -272,6 +274,78 @@ public class TestService extends Service {
handler.removeCallbacks(periodicTask);
}
}
public void Bluetooth_status() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
Log.d("BT", "No permission to connect bluetooth devices");
return;
} else {
Log.d("BT", "Permission to connect bluetooth devices granted");
}
registerReceiver(receiver, intentFilter);
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
//PreferenceManager.getDefaultSharedPreferences("myPreferences",Context.MODE_PRIVATE);
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor e=pref.edit();
String action = intent.getAction(); // may need to chain this to a recognizing function
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// HomeFragment homeFragment=new HomeFragment();
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
Log.d("BT", "No permission to connect bluetooth devices");
return;
}
String deviceHardwareAddress = device.getAddress(); // MAC address
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
//Do something if connected
Log.d("BT", "Device connected");
String registeredId = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("bluetooth_device_id", "none");
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);
}
e.apply();
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//Do something if disconnected
Log.d("BT", "Device disconnected");
e.putBoolean("connection_status",false);
e.apply();
}
}
};
// public void bluetoothTest() {
// SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
// Boolean b = pref.getBoolean("connection_status", false);
// Log.d("bluetooth", b.toString());
// }
@Nullable
@Override
public IBinder onBind(Intent intent) {