title: How to discover Bluetooth devices in a Unity Android Plugin?
body: <p>I am developing a <strong>Unity</strong> game for <strong>Android</strong>, which uses a <strong>Android Plugin</strong> to be able to send data via <strong>Bluetooth</strong> during the game. Sending Data and establishing a connection to a paired device works fine. 
  However, I can't get the <strong>discovery of new Bluetooth devices</strong> to work.</p>
  
  <p>In my <strong>Plugin class</strong> (which also handles the sending data and so on, which works fine):</p>
  
  <pre><code> public void startBluetoothDeviceDiscovery(){
  
          IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
          currentActivity.registerReceiver(receiver, filter);
  
  
          if (bluetoothAdapter.isDiscovering()){
              bluetoothAdapter.cancelDiscovery();
          }
          bluetoothAdapter.startDiscovery();
  
      }
  </code></pre>
  
  <p>The method startBluetoothDeviceDiscovery is called for sure (triggered by a button), and the bluetoothAdapter is already set and not null. Bluetooth on the device was always ON.</p>
  
  <p>Also inside the <strong>Plugin class</strong>:</p>
  
  <pre><code> BroadcastReceiver receiver = new MyBroadcastReceiver();
  
      public class MyBroadcastReceiver extends BroadcastReceiver {
  
          @Override
          public void onReceive(Context context, Intent intent) {
  
              String action = intent.getAction();
  
              if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  
                    // a device was found, do stuff
  
              }
          }
      }
  </code></pre>
  
  <p>Basically, what the Android documentation suggests, when discovering new devices.
  <a href="https://developer.android.com/guide/topics/connectivity/bluetooth#DiscoverDevices" rel="nofollow noreferrer">https://developer.android.com/guide/topics/connectivity/bluetooth#DiscoverDevices</a></p>
  
  <p>I thought, maybe there are just no devices arround, so I wanted the receiver already to be <strong>triggered when the discovery starts</strong>, so I tried to look for <code>BluetoothAdapter.ACTION_DISCOVERY_STARTED</code>:</p>
  
  <pre><code> IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
          currentActivity.registerReceiver(receiver, filter);
  
  </code></pre>
  
  <p>And in my <strong>Receiver at onReceive</strong>:</p>
  
  <pre><code>@Override
          public void onReceive(Context context, Intent intent) {
  
              String action = intent.getAction();
  
              if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
  
                   // discovery started, do stuff
  
              }
  
          }
  </code></pre>
  
  <p>This should be called every time, when the button is clicked and the method <code>startBluetoothDeviceDiscovery()</code> is called, since it always starts the Bluetooth Discovery, but it still didn't work.</p>
  
  <p>My last guess was, that due to the Plugin not being an activity, the BroadcastReceiver can not be accessed. So I changed the action to <code>BluetoothAdapter.ACTION_STATE_CHANGED</code> and then when I turned ON and OFF the Bluetooth on my device, it suddenly was received in the <code>onReceive()</code>.</p>
  
  <p>My Manifest (copied to the correct Unity folder location):</p>
  
  <pre><code>    xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.unity3d.player"
      xmlns:tools="http://schemas.android.com/tools"
      android:installLocation="preferExternal"&gt;
  
      &lt;uses-permission android:name="android.permission.BLUETOOTH" /&gt;
      &lt;uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  
  
  
      &lt;supports-screens
          android:smallScreens="true"
          android:normalScreens="true"
          android:largeScreens="true"
          android:xlargeScreens="true"
          android:anyDensity="true"/&gt;
  
      &lt;application
          android:theme="@style/UnityThemeSelector"
          android:icon="@mipmap/app_icon"
          android:label="@string/app_name"&gt;
          &lt;activity android:name="com.unity3d.player.UnityPlayerActivity"
                    android:label="@string/app_name"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
              &lt;meta-data android:name="unityplayer.UnityActivity" android:value="true" /&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>So why is my turning on <code>bluetoothAdapter.startDiscovery()</code> and finding devices not received in the receiver?</strong></p>
  
  <p>Thanks for <strong>ANY</strong> help or ideas :)</p>