Skip to content

Commit

Permalink
Added requestPermissions for Bluetooth on API 31+
Browse files Browse the repository at this point in the history
  • Loading branch information
graham22 committed Sep 15, 2023
1 parent 45fa1e8 commit f0e1cc6
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 31 deletions.
4 changes: 2 additions & 2 deletions code/Android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 33
compileSdk 33

defaultConfig {
applicationId "com.skye.skyetracker"
minSdkVersion 15
minSdkVersion 16
targetSdkVersion 33
}

Expand Down
18 changes: 11 additions & 7 deletions code/Android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="254"
android:versionName="2.5.4" >
android:versionCode="255"
android:versionName="2.5.5" >

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
android:minSdkVersion="31"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
android:minSdkVersion="31"/>
<uses-feature android:name="android.hardware.bluetooth" android:required="true"/>

<application
android:name=".MainApplication"
Expand All @@ -16,7 +21,6 @@
android:theme="@android:style/Theme.Holo">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
2 changes: 1 addition & 1 deletion code/Android/app/src/main/assets/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>About SkyeTracker</title>
</head>
<body>
<h2>SkyeTracker (version 2.5.4)</h2>
<h2>SkyeTracker (version 2.5.5)</h2>
<p>
<h5>
<a href="https://www.buymeacoffee.com/r4K2HIB" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
package com.skye.skyetracker;

import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;

import androidx.core.app.ActivityCompat;
import androidx.viewpager.widget.ViewPager;

import android.util.Log;
import android.widget.Toast;


public class MainActivity extends Activity {
private TabStripAdapter tabStripAdapter;
private SlidingTabLayout stl;

private static final String[] PERMISSIONS_BLUETOOTH = {
Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_CONNECT
};

@Override
protected void onPause() {
super.onPause();
Expand Down Expand Up @@ -47,8 +58,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
public void onPageSelected(int position) {
if (position > 0) {
MainApplication.SendCommand("StopBroadcast");
}
else {
} else {
MainApplication.SendCommand("BroadcastPosition");
}
}
Expand All @@ -71,19 +81,39 @@ public void onPageScrollStateChanged(int state) {
private void checkBTState() {
// Check for Bluetooth support and then check to make sure it is turned on
// Emulator doesn't support Bluetooth and will return null
boolean bluetoothAvailable = getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
if (!bluetoothAvailable) {
errorExit("Fatal Error", "Bluetooth not support");
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(this, PERMISSIONS_BLUETOOTH,1);
}
return;
}
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if(btAdapter==null) {
if (btAdapter == null) {
errorExit("Fatal Error", "Bluetooth not support");
} else {
if (btAdapter.isEnabled()) {
Log.d(Constants.TAG, "...Bluetooth ON...");
} else {
//Prompt user to turn on Bluetooth

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
}
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
startActivityForResult(enableBtIntent, 1);
}
}
}

private void errorExit(String title, String message){
Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
Expand Down
39 changes: 22 additions & 17 deletions code/Android/app/src/main/java/com/skye/skyetracker/Tracker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.skye.skyetracker;

import android.Manifest;
import android.app.Activity;
import android.app.IntentService;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
Expand All @@ -8,9 +10,12 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.content.pm.PackageManager;
import android.os.Handler;

import androidx.core.app.ActivityCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.util.Log;
import android.widget.Toast;

Expand All @@ -19,6 +24,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.UUID;
Expand Down Expand Up @@ -78,6 +84,7 @@ public void handleMessage(android.os.Message msg) {
startBluetoothStateBroadcast();
LocalBroadcastManager.getInstance(getBaseContext()).registerReceiver(mCommandReceiver, new IntentFilter("com.skye.skyetracker.Write"));
}

@Override
protected void onHandleIntent(Intent intent) {
Log.d(Constants.TAG, String.format("Tracker onHandleIntent action: %s", intent.getAction()));
Expand Down Expand Up @@ -140,7 +147,7 @@ private void BroadcastMessage(String json, String action) {
Intent commandIntent = new Intent(action, null, getBaseContext(), Tracker.class);
commandIntent.putExtra("json", json);
LocalBroadcastManager.getInstance(getBaseContext()).sendBroadcast(commandIntent);
}
}

// Our handler for received Intents.
private BroadcastReceiver mCommandReceiver = new BroadcastReceiver() {
Expand All @@ -155,6 +162,11 @@ public void onReceive(Context context, Intent intent) {

private boolean SetupBluetooth() throws InterruptedException {
boolean rVal = false;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
return rVal;
}
}
while (!bluetoothConnected) {
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
ArrayList<String> addresses = new ArrayList<String>();
Expand All @@ -178,9 +190,14 @@ private boolean SetupBluetooth() throws InterruptedException {
// A MAC address, which we got above.
// A Service ID or UUID. In this case we are using the
// UUID for SPP.
btSocket = createBluetoothSocket(device);
} catch (IOException e1) {
e1.printStackTrace();
final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[]{UUID.class});
btSocket = (BluetoothSocket) m.invoke(device, Constants.MY_UUID);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}

// Discovery is resource intensive. Make sure it isn't going on
Expand Down Expand Up @@ -232,18 +249,6 @@ private boolean SetupBluetooth() throws InterruptedException {
return rVal;
}

private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
if (Build.VERSION.SDK_INT >= 10) {
try {
final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[]{UUID.class});
return (BluetoothSocket) m.invoke(device, Constants.MY_UUID);
} catch (Exception e) {
Log.e(Constants.TAG, "Could not create Insecure RFComm Connection", e);
}
}
return device.createRfcommSocketToServiceRecord(Constants.MY_UUID);
}

/* Call this from the main activity to send data to the remote device */
private void Write(String message) {
if (mmOutStream != null) {
Expand Down
2 changes: 1 addition & 1 deletion code/Android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.4.1'
classpath 'com.android.tools.build:gradle:8.1.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
3 changes: 3 additions & 0 deletions code/Android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.defaults.buildfeatures.buildconfig=true
android.enableJetifier=true
android.nonFinalResIds=false
android.nonTransitiveRClass=false
android.useAndroidX=true

0 comments on commit f0e1cc6

Please sign in to comment.