app can now turn on Bluetooth

This commit is contained in:
CDaut 2022-03-27 20:53:39 +02:00 committed by CDaut
parent 6bae48d22d
commit a4cb7da53f
5 changed files with 46 additions and 11 deletions

View file

@ -2,6 +2,21 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.cdaut.climategoapp">
<!--Ensure Bluetooth is present on the device-->
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<!-- Request legacy Bluetooth permissions on older devices. -->
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<!-- Scan for the sensor -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<!-- Communicate with paired sensor -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<application
android:allowBackup="true"
android:icon="@mipmap/logo"
@ -20,5 +35,4 @@
</intent-filter>
</activity>
</application>
</manifest>

View file

@ -1,10 +1,18 @@
package de.cdaut.climategoapp;
import static android.app.Activity.RESULT_OK;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
@ -14,6 +22,22 @@ public class DebugFragment extends Fragment {
private FragmentDebugBinding binding;
//this is the Launcher that will make the request to
// enable Bluetooth and handle the result callback
private final ActivityResultLauncher<Intent> mBtEnable = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() != RESULT_OK) {
//make an error toast if the user denys enabling bluetooth
Toast.makeText(
getActivity().getApplicationContext(),
R.string.bt_required,
Toast.LENGTH_LONG
).show();
}
}
);
@Override
public View onCreateView(
@NonNull LayoutInflater inflater, ViewGroup container,
@ -22,20 +46,15 @@ public class DebugFragment extends Fragment {
binding = FragmentDebugBinding.inflate(inflater, container, false);
//TODO: Add proper Function when button is pressed here
this.binding.buttonTest.setOnClickListener(view -> {
this.binding.debugView.setText("NewText");
//make request to enable bluetooth
mBtEnable.launch(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));
});
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onDestroyView() {
super.onDestroyView();

View file

@ -8,9 +8,11 @@ public final class SensorDebugUtils {
throw new IllegalAccessError("Utility Class");
}
public static List<String> readSensorData(){
public static List<String> readSensorData() {
ArrayList<String> data = new ArrayList<>();
data.add("Test");
data.add("a");
data.add("b");
return data;
}
}

View file

@ -21,7 +21,6 @@
android:id="@+id/debugView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"

View file

@ -4,9 +4,10 @@
<!-- Strings used for fragments for navigation -->
<string name="first_fragment_label">Main Fragment</string>
<string name="debug_fragment_label">Debug Fragment</string>
<string name="btn_test">Test</string>
<string name="btn_test">Enable Bluetooth</string>
<string name="nav_main_label">Main</string>
<string name="nav_debug_frag_label">Debug view</string>
<string name="nav_open">Open</string>
<string name="nav_close">Close</string>
<string name="bt_required">Bluetooth is required to connect to the sensor</string>
</resources>