Bluetoothの接続状態を返却する機能を追加 #21

Merged
Asura146 merged 1 commits from kyou/Bluetooth into main 2024-01-22 16:14:53 +00:00
3 changed files with 98 additions and 3 deletions

View File

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

View File

@ -18,6 +18,7 @@ import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Vibrator; import android.os.Vibrator;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
@ -46,6 +47,7 @@ public class MainActivity extends AppCompatActivity {
BluetoothManager bluetoothManager; BluetoothManager bluetoothManager;
BluetoothAdapter bluetoothAdapter; BluetoothAdapter bluetoothAdapter;
DocumentReference mDocRef; DocumentReference mDocRef;
private HomeFragment homeFragment; private HomeFragment homeFragment;
@ -81,6 +83,11 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); 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との紐づけ db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ
BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view); BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
@ -187,12 +194,19 @@ public class MainActivity extends AppCompatActivity {
//Bluetoothの検知機能 //Bluetoothの検知機能
private final BroadcastReceiver receiver = new BroadcastReceiver() { private final BroadcastReceiver receiver = new BroadcastReceiver() {
//PreferenceManager.getDefaultSharedPreferences("myPreferences",Context.MODE_PRIVATE);
@Override @Override
public void onReceive(Context context, Intent intent) { 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 String action = intent.getAction(); // may need to chain this to a recognizing function
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// HomeFragment homeFragment=new HomeFragment(); // HomeFragment homeFragment=new HomeFragment();
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { 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)) { if (deviceHardwareAddress.equals(registeredId)) {
Log.d("BT_Judge", "登録済み"); 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)) { } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//Do something if disconnected //Do something if disconnected
Log.d("BT", "Device 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);//現在の位置を取得 mDocRef = FirebaseFirestore.getInstance().document("status/" + IdPref);//現在の位置を取得
initNotification(mDocRef);//現在の位置を引数に initNotification()を処理 initNotification(mDocRef);//現在の位置を引数に initNotification()を処理
} }
Bluetooth_status();
return flags; return flags;
@ -272,6 +274,78 @@ public class TestService extends Service {
handler.removeCallbacks(periodicTask); 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 @Nullable
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {