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; } }