2626import java .nio .charset .StandardCharsets ;
2727import java .util .ArrayList ;
2828import java .util .UUID ;
29+ import java .util .concurrent .ConcurrentLinkedQueue ;
2930
3031public class BluetoothManager {
3132 // instance (expose for unity to get)
@@ -36,7 +37,7 @@ public static BluetoothManager getInstance() {
3637
3738 // var
3839 public BluetoothAdapter bt ;
39- public ArrayList <BluetoothDevice > availableDevices = new ArrayList <BluetoothDevice >();
40+ public ConcurrentLinkedQueue <BluetoothDevice > availableDevices = new ConcurrentLinkedQueue <BluetoothDevice >();
4041 public BluetoothDevice connectedDevice ;
4142 public BluetoothSocket socket ;
4243 public String usePin = "" ;
@@ -83,7 +84,9 @@ public void CheckPermission() {
8384 }
8485
8586 // Start Discovering devices
87+ @ SuppressLint ("MissingPermission" )
8688 public void StartDiscovery () {
89+
8790 // init the list
8891 availableDevices .clear ();
8992
@@ -97,7 +100,10 @@ public void StartDiscovery() {
97100 Log .i ("BtManager" , "Start Discovery..." );
98101 }
99102
100- // Get Discovery list, in format "Name|Mac"
103+ /**
104+ * Get Discovery list, in format "Name|Mac"
105+ */
106+ @ SuppressLint ("MissingPermission" )
101107 public String [] GetAvailableDevices () {
102108 ArrayList <String > result = new ArrayList <>();
103109 for (BluetoothDevice device : availableDevices ) {
@@ -108,7 +114,13 @@ public String[] GetAvailableDevices() {
108114 return arr ;
109115 }
110116
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" )
112124 public boolean Connect (final String mac , final String pin ) {
113125 connectedDevice = bt .getRemoteDevice (mac );
114126 usePin = pin ;
@@ -131,13 +143,16 @@ public boolean Connect(final String mac, final String pin) {
131143 }
132144 }
133145
146+ /**
147+ *
148+ * @return
149+ */
134150 public boolean IsConnected () {
135- if (socket == null )
136- return false ;
137- return true ;
151+ return socket != null ;
138152// return socket.isConnected(); // this doesn't check connection state!
139153 }
140154
155+ @ SuppressLint ("MissingPermission" )
141156 public String GetConnectedDevice () {
142157 if (IsConnected ())
143158 return connectedDevice .getName ()+ '|' + connectedDevice .getAddress ();
@@ -211,6 +226,7 @@ public void Stop() {
211226 // Append available device list
212227 // https://stackoverflow.com/questions/19683034/getting-the-address-and-names-of-all-available-bluetooth-devices-in-android
213228 private final BroadcastReceiver discoverFinishHandler = new BroadcastReceiver () {
229+ @ SuppressLint ("MissingPermission" )
214230 @ Override
215231 public void onReceive (Context context , Intent intent ) {
216232 String action = intent .getAction ();
@@ -234,6 +250,7 @@ else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
234250 // Programmatically input pin code on connect
235251 // https://stackoverflow.com/questions/35519321/android-bluetooth-pairing-without-user-enter-pin-and-confirmation-using-android
236252 private final BroadcastReceiver pairRequestHandler = new BroadcastReceiver () {
253+ @ SuppressLint ("MissingPermission" )
237254 public void onReceive (Context context , Intent intent ) {
238255 // no need to use pin
239256 if (usePin .equals ("" )) {
0 commit comments