Bluetoothデバイスを登録済みか判定する機能を追加
This commit is contained in:
parent
c90d0b8f6e
commit
99201fda27
|
@ -1,4 +1,3 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
|
||||
|
|
|
@ -174,6 +174,8 @@ public class MainActivity extends AppCompatActivity {
|
|||
this.flg = false;
|
||||
initNotification(mDocRef);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void initNotification(DocumentReference mDocRef) {
|
||||
|
@ -289,12 +291,12 @@ public class MainActivity extends AppCompatActivity {
|
|||
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public void onStop() {
|
||||
// super.onStop();
|
||||
// Intent intent = new Intent(getApplication(), TestService.class);
|
||||
// startService(intent);
|
||||
// }
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
Intent intent = new Intent(getApplication(), TestService.class);
|
||||
startService(intent);
|
||||
}
|
||||
|
||||
//Bluetooth_setupの戻るボタン
|
||||
public void setupBackButton(boolean enableBackButton) {
|
||||
|
|
|
@ -3,11 +3,15 @@ package com.example.childguard;
|
|||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.Service;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.IBinder;
|
||||
import android.os.Vibrator;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
@ -19,13 +23,63 @@ public class TestService extends Service {
|
|||
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
|
||||
//Bluetooth検知機能
|
||||
IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
|
||||
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
|
||||
|
||||
audioStart();
|
||||
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
||||
Log.d("BT", "No permission to connect bluetooth devices");
|
||||
|
||||
}
|
||||
else {
|
||||
Log.d("BT", "Permission to connect bluetooth devices granted");
|
||||
}
|
||||
|
||||
|
||||
registerReceiver(receiver, intentFilter);
|
||||
//audioStart();
|
||||
|
||||
return START_NOT_STICKY;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private final BroadcastReceiver receiver = new BroadcastReceiver() {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
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 deviceName = device.getName();
|
||||
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", "登録済み");
|
||||
} else Log.d("BT_Judge", "未登録");
|
||||
|
||||
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
|
||||
//Do something if disconnected
|
||||
Log.d("BT", "Device disconnected");
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private void audioStart(){
|
||||
//↓通知をする際に起動するバイブレーション
|
||||
((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(1000);
|
||||
|
|
Loading…
Reference in New Issue
Block a user