Skip to content

Commit d379d97

Browse files
committed
[fix] Fix Android 11 permission issues
1 parent a5efa98 commit d379d97

File tree

28 files changed

+248
-169
lines changed

28 files changed

+248
-169
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Connect with bluetooth in Unity (Android).
1414

1515
## Notes
1616

17+
<!--
18+
1719
### AndroidManifest Permission
1820
These permissions are required!! You **MUST** add them to your manifest (`Plugins/Android/AndroidManifest.xml`).
1921
@@ -22,10 +24,12 @@ These permissions are required!! You **MUST** add them to your manifest (`Plugin
2224
<uses-permission android:name="android.permission.BLUETOOTH"/>
2325
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
2426
<!-- For scanning nearby devices -->
25-
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
26-
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
27+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- Android > 10 -->
28+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- Android 9 -->
2729
```
2830
31+
-->
32+
2933
After you install the app, you may need to manually enable the permissions under android application settings.
3034
3135
<img src="https://i.imgur.com/33vq1ev.png" height="400" />

UnityBluetooth-AndroidStudio/.idea/compiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityBluetooth-AndroidStudio/.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityBluetooth-AndroidStudio/.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityBluetooth-AndroidStudio/app/build.gradle

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88

99
defaultConfig {
1010
// applicationId "com.jcxyis.unitybluetooth"
11-
minSdk 21
11+
minSdk 19
1212
targetSdk 32
1313
// versionCode 1
1414
// versionName "1.0"
@@ -30,25 +30,26 @@ android {
3030

3131
dependencies {
3232

33-
implementation 'androidx.appcompat:appcompat:1.4.1'
34-
implementation 'com.google.android.material:material:1.5.0'
35-
implementation files('libs\\classes.jar')
33+
compileOnly 'androidx.appcompat:appcompat:1.4.1'
34+
compileOnly 'com.google.android.material:material:1.5.0'
35+
compileOnly files('libs\\classes.jar')
36+
3637
testImplementation 'junit:junit:4.13.2'
3738
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
3839
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
3940
}
4041

41-
// 移除舊的 jar 檔
42-
task deleteOldJar(type: Delete) {
43-
delete 'release/UnityBtAndroidPlugin.jar'
44-
}
45-
46-
// 匯出 jar 檔
47-
task exportJar(type: Copy) {
48-
from('build/intermediates/aar_main_jar/release/')
49-
into('release/')
50-
include('classes.jar')
51-
rename('classes.jar', 'UnityBtAndroidPlugin.jar')
52-
}
53-
54-
exportJar.dependsOn(deleteOldJar, build)
42+
// use assembleRelease instead
43+
//// 移除舊的 jar 檔
44+
//task deleteOldJar(type: Delete) {
45+
// delete 'release/UnityBtAndroidPlugin.jar'
46+
//}
47+
//
48+
//// 匯出 jar 檔
49+
//task exportJar(type: Copy) {
50+
// from('build/intermediates/aar_main_jar/release/')
51+
// into('release/')
52+
// include('classes.jar')
53+
// rename('classes.jar', 'UnityBtAndroidPlugin.jar')
54+
//}
55+
//exportJar.dependsOn(deleteOldJar, build)
-7.16 KB
Binary file not shown.

UnityBluetooth-AndroidStudio/app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools">
44

5-
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
65
<uses-permission android:name="android.permission.BLUETOOTH" />
6+
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
77
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
8-
<application
9-
android:allowBackup="true"
10-
android:dataExtractionRules="@xml/data_extraction_rules"
11-
android:fullBackupContent="@xml/backup_rules"
12-
android:icon="@mipmap/ic_launcher"
13-
android:label="@string/app_name"
14-
android:roundIcon="@mipmap/ic_launcher_round"
15-
android:supportsRtl="true"
16-
android:theme="@style/Theme.UnityBluetooth"
17-
tools:targetApi="31" />
8+
9+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- Android > 10 -->
10+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- Android 9 -->
11+
12+
<application />
1813

1914
</manifest>

UnityBluetooth-AndroidStudio/app/src/main/java/com/jcxyis/unitybluetooth/BluetoothManager.java

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,30 @@
33
import com.unity3d.player.UnityPlayer;
44
import com.unity3d.player.UnityPlayerActivity;
55

6+
import android.Manifest;
67
import android.annotation.SuppressLint;
8+
import android.app.Activity;
79
import android.bluetooth.BluetoothAdapter;
810
import android.bluetooth.BluetoothDevice;
911
import android.bluetooth.BluetoothSocket;
1012
import android.content.BroadcastReceiver;
1113
import android.content.Context;
1214
import android.content.Intent;
1315
import android.content.IntentFilter;
16+
import android.content.pm.PackageManager;
17+
import android.os.Build;
1418
import android.util.Log;
1519
import android.widget.Toast;
1620

1721
import java.io.BufferedReader;
1822
import java.io.IOException;
1923
import java.io.InputStream;
2024
import java.io.InputStreamReader;
25+
import java.lang.reflect.Array;
2126
import java.nio.charset.StandardCharsets;
2227
import java.util.ArrayList;
2328
import java.util.UUID;
2429

25-
@SuppressLint("MissingPermission")
2630
public class BluetoothManager {
2731
// instance (expose for unity to get)
2832
private static final BluetoothManager _instance = new BluetoothManager();
@@ -49,11 +53,35 @@ public BluetoothManager() {
4953
// Input pin code
5054
IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
5155
UnityPlayer.currentActivity.registerReceiver(pairRequestHandler, filter2);
56+
57+
IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
58+
UnityPlayer.currentActivity.registerReceiver(disconnectHandler, filter3);
59+
5260
}
5361

5462

5563
// --- Bt ---
5664

65+
public void CheckPermission() {
66+
// Request permissions
67+
ArrayList<String> perms = new ArrayList<>();
68+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
69+
perms.add(Manifest.permission.ACCESS_BACKGROUND_LOCATION);
70+
perms.add(Manifest.permission.ACCESS_FINE_LOCATION);
71+
}
72+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
73+
perms.add(Manifest.permission.BLUETOOTH_SCAN);
74+
perms.add(Manifest.permission.BLUETOOTH_CONNECT);
75+
}
76+
77+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
78+
// if (UnityPlayer.currentActivity.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
79+
// }
80+
Log.i("BTManager", "Requesting "+perms.size()+" permissions");
81+
UnityPlayer.currentActivity.requestPermissions(perms.toArray(new String[]{}), 0);
82+
}
83+
}
84+
5785
// Start Discovering devices
5886
public void StartDiscovery() {
5987
// init the list
@@ -97,28 +125,42 @@ public boolean Connect(final String mac, final String pin) {
97125
}
98126
catch (Exception e) {
99127
bt.startDiscovery();
100-
Log.e("BtManager", "Failed to connect!!! ");
128+
Log.e("BtManager", "Failed to connect!! ");
101129
e.printStackTrace();
102130
return false;
103131
}
104132
}
105133

134+
public boolean IsConnected() {
135+
if(socket == null)
136+
return false;
137+
return true;
138+
// return socket.isConnected(); // this doesn't check connection state!
139+
}
140+
141+
public String GetConnectedDevice() {
142+
if(IsConnected())
143+
return connectedDevice.getName()+ '|' + connectedDevice.getAddress();
144+
return "|";
145+
}
146+
106147
public boolean Send(final String message) {
107148
try {
108149
socket.getOutputStream().write(message.getBytes());
109-
Log.v("tManager", "Sent "+message);
150+
Log.v("BtManager", "Sent "+message);
110151
return true;
111152
}
112153
catch (Exception e) {
113154
Log.e("BtManager", "Failed to send message");
114155
e.printStackTrace();
115156
return false;
116157
}
117-
// return false;
118158
}
119159

120160
// Available
121161
public int Available() {
162+
if(socket == null)
163+
return -2;
122164
try {
123165
return socket.getInputStream().available();
124166
}
@@ -148,9 +190,14 @@ public String ReadLine(){
148190
}
149191

150192
// Stop
151-
public void Stop() throws IOException {
193+
public void Stop() {
152194
if(socket != null) {
153-
socket.close();
195+
try {
196+
socket.close();
197+
} catch (IOException e) {
198+
Log.e("BtManager", "Error on disconnecting");
199+
e.printStackTrace();
200+
}
154201
}
155202
socket = null;
156203
connectedDevice = null;
@@ -212,6 +259,17 @@ public void onReceive(Context context, Intent intent) {
212259
}
213260
};
214261

262+
private final BroadcastReceiver disconnectHandler = new BroadcastReceiver() {
263+
public void onReceive(Context context, Intent intent) {
264+
connectedDevice = null;
265+
try {
266+
socket.close();
267+
} catch (IOException ignored) {}
268+
socket = null;
269+
connectedDevice = null;
270+
}
271+
};
272+
215273

216274
// --- Utility --
217275

UnityBluetooth-AndroidStudio/app/src/main/res/values-night/themes.xml

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
<color name="purple_200">#FFBB86FC</color>
4-
<color name="purple_500">#FF6200EE</color>
5-
<color name="purple_700">#FF3700B3</color>
6-
<color name="teal_200">#FF03DAC5</color>
7-
<color name="teal_700">#FF018786</color>
8-
<color name="black">#FF000000</color>
9-
<color name="white">#FFFFFFFF</color>
3+
<!-- <color name="purple_200">#FFBB86FC</color>-->
4+
<!-- <color name="purple_500">#FF6200EE</color>-->
5+
<!-- <color name="purple_700">#FF3700B3</color>-->
6+
<!-- <color name="teal_200">#FF03DAC5</color>-->
7+
<!-- <color name="teal_700">#FF018786</color>-->
8+
<!-- <color name="black">#FF000000</color>-->
9+
<!-- <color name="white">#FFFFFFFF</color>-->
1010
</resources>

0 commit comments

Comments
 (0)