aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/io/trygvis/android/bt/BtActivitySupport.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/io/trygvis/android/bt/BtActivitySupport.java')
-rw-r--r--app/src/main/java/io/trygvis/android/bt/BtActivitySupport.java57
1 files changed, 57 insertions, 0 deletions
diff --git a/app/src/main/java/io/trygvis/android/bt/BtActivitySupport.java b/app/src/main/java/io/trygvis/android/bt/BtActivitySupport.java
new file mode 100644
index 0000000..03a36fb
--- /dev/null
+++ b/app/src/main/java/io/trygvis/android/bt/BtActivitySupport.java
@@ -0,0 +1,57 @@
+package io.trygvis.android.bt;
+
+import android.app.Activity;
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothManager;
+import android.content.Context;
+import android.content.Intent;
+import android.widget.Toast;
+
+import io.trygvis.soilmoisture.R;
+
+public class BtActivitySupport {
+
+ private final Activity activity;
+ private final int requestCode;
+ private BluetoothAdapter mBluetoothAdapter;
+
+ public BtActivitySupport(Activity activity, int requestCode) {
+ this.activity = activity;
+ this.requestCode = requestCode;
+ }
+
+ public boolean onCreate() {
+ final BluetoothManager bluetoothManager = (BluetoothManager) activity.getSystemService(Context.BLUETOOTH_SERVICE);
+ mBluetoothAdapter = bluetoothManager.getAdapter();
+
+ // Checks if Bluetooth is supported on the device.
+ if (mBluetoothAdapter == null) {
+ Toast.makeText(activity, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).
+ show();
+ return false;
+ }
+
+ return true;
+ }
+
+ public boolean enableBt() {
+ // Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled,
+ // fire an intent to display a dialog asking the user to grant permission to enable it.
+ if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
+ Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
+ activity.startActivityForResult(enableBtIntent, requestCode);
+ }
+
+ return true;
+ }
+
+ @SuppressWarnings({"RedundantIfStatement", "UnusedParameters"})
+ public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
+ // User chose not to enable Bluetooth.
+ if (requestCode == this.requestCode && resultCode == Activity.RESULT_CANCELED) {
+ return false;
+ }
+
+ return true;
+ }
+}