I am developing a Unity game for Android, which uses a Android Plugin to be able to send data via Bluetooth during the game. Sending Data and establishing a connection to a paired device works fine. However, I can't get the discovery of new Bluetooth devices to work.
In my Plugin class (which also handles the sending data and so on, which works fine):
public void startBluetoothDeviceDiscovery(){
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
currentActivity.registerReceiver(receiver, filter);
if (bluetoothAdapter.isDiscovering()){
bluetoothAdapter.cancelDiscovery();
}
bluetoothAdapter.startDiscovery();
}
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.
Also inside the Plugin class:
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
}
}
}
Basically, what the Android documentation suggests, when discovering new devices. https://developer.android.com/guide/topics/connectivity/bluetooth#DiscoverDevices
I thought, maybe there are just no devices arround, so I wanted the receiver already to be triggered when the discovery starts, so I tried to look for BluetoothAdapter.ACTION_DISCOVERY_STARTED
:
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
currentActivity.registerReceiver(receiver, filter);
And in my Receiver at onReceive:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
// discovery started, do stuff
}
}
This should be called every time, when the button is clicked and the method startBluetoothDeviceDiscovery()
is called, since it always starts the Bluetooth Discovery, but it still didn't work.
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 BluetoothAdapter.ACTION_STATE_CHANGED
and then when I turned ON and OFF the Bluetooth on my device, it suddenly was received in the onReceive()
.
My Manifest (copied to the correct Unity folder location):
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
xmlns:tools="http://schemas.android.com/tools"
android:installLocation="preferExternal">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
<application
android:theme="@style/UnityThemeSelector"
android:icon="@mipmap/app_icon"
android:label="@string/app_name">
<activity android:name="com.unity3d.player.UnityPlayerActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
</manifest>
So why is my turning on bluetoothAdapter.startDiscovery()
and finding devices not received in the receiver?
Thanks for ANY help or ideas :)
Have you tried Bluetooth plugin in Unity Asset Store? I'm doing same thing but using Bluetooth Classic (HC-06) and I'm using plugin from Asset store. My app will only use that BT module so in C# script in Unity I wrote: