26
26
import java .nio .charset .StandardCharsets ;
27
27
import java .util .ArrayList ;
28
28
import java .util .UUID ;
29
+ import java .util .concurrent .ConcurrentLinkedQueue ;
29
30
30
31
public class BluetoothManager {
31
32
// instance (expose for unity to get)
@@ -36,7 +37,7 @@ public static BluetoothManager getInstance() {
36
37
37
38
// var
38
39
public BluetoothAdapter bt ;
39
- public ArrayList <BluetoothDevice > availableDevices = new ArrayList <BluetoothDevice >();
40
+ public ConcurrentLinkedQueue <BluetoothDevice > availableDevices = new ConcurrentLinkedQueue <BluetoothDevice >();
40
41
public BluetoothDevice connectedDevice ;
41
42
public BluetoothSocket socket ;
42
43
public String usePin = "" ;
@@ -83,7 +84,9 @@ public void CheckPermission() {
83
84
}
84
85
85
86
// Start Discovering devices
87
+ @ SuppressLint ("MissingPermission" )
86
88
public void StartDiscovery () {
89
+
87
90
// init the list
88
91
availableDevices .clear ();
89
92
@@ -97,7 +100,10 @@ public void StartDiscovery() {
97
100
Log .i ("BtManager" , "Start Discovery..." );
98
101
}
99
102
100
- // Get Discovery list, in format "Name|Mac"
103
+ /**
104
+ * Get Discovery list, in format "Name|Mac"
105
+ */
106
+ @ SuppressLint ("MissingPermission" )
101
107
public String [] GetAvailableDevices () {
102
108
ArrayList <String > result = new ArrayList <>();
103
109
for (BluetoothDevice device : availableDevices ) {
@@ -108,7 +114,13 @@ public String[] GetAvailableDevices() {
108
114
return arr ;
109
115
}
110
116
111
- // Connect
117
+ /**
118
+ * Connect
119
+ * @param mac Device mac
120
+ * @param pin Device pin code (if any)
121
+ * @return is success?
122
+ */
123
+ @ SuppressLint ("MissingPermission" )
112
124
public boolean Connect (final String mac , final String pin ) {
113
125
connectedDevice = bt .getRemoteDevice (mac );
114
126
usePin = pin ;
@@ -131,13 +143,16 @@ public boolean Connect(final String mac, final String pin) {
131
143
}
132
144
}
133
145
146
+ /**
147
+ *
148
+ * @return
149
+ */
134
150
public boolean IsConnected () {
135
- if (socket == null )
136
- return false ;
137
- return true ;
151
+ return socket != null ;
138
152
// return socket.isConnected(); // this doesn't check connection state!
139
153
}
140
154
155
+ @ SuppressLint ("MissingPermission" )
141
156
public String GetConnectedDevice () {
142
157
if (IsConnected ())
143
158
return connectedDevice .getName ()+ '|' + connectedDevice .getAddress ();
@@ -211,6 +226,7 @@ public void Stop() {
211
226
// Append available device list
212
227
// https://stackoverflow.com/questions/19683034/getting-the-address-and-names-of-all-available-bluetooth-devices-in-android
213
228
private final BroadcastReceiver discoverFinishHandler = new BroadcastReceiver () {
229
+ @ SuppressLint ("MissingPermission" )
214
230
@ Override
215
231
public void onReceive (Context context , Intent intent ) {
216
232
String action = intent .getAction ();
@@ -234,6 +250,7 @@ else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
234
250
// Programmatically input pin code on connect
235
251
// https://stackoverflow.com/questions/35519321/android-bluetooth-pairing-without-user-enter-pin-and-confirmation-using-android
236
252
private final BroadcastReceiver pairRequestHandler = new BroadcastReceiver () {
253
+ @ SuppressLint ("MissingPermission" )
237
254
public void onReceive (Context context , Intent intent ) {
238
255
// no need to use pin
239
256
if (usePin .equals ("" )) {
0 commit comments