unity bluetooth android,unity3d - How to discover Bluetooth devices in a Unity Android Plugin? - Sta...

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">

android:smallScreens="true"

android:normalScreens="true"

android:largeScreens="true"

android:xlargeScreens="true"

android:anyDensity="true"/>

android:theme="@style/UnityThemeSelector"

android:icon="@mipmap/app_icon"

android:label="@string/app_name">

android:label="@string/app_name">

So why is my turning on bluetoothAdapter.startDiscovery() and finding devices not received in the receiver?

Thanks for ANY help or ideas :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值