如何在Unity Android插件中发现蓝牙设备?

alen0pnh  于 2023-05-27  发布在  Android
关注(0)|答案(1)|浏览(409)

我正在为Android开发一款Unity游戏,它使用了Android插件,可以在游戏过程中通过蓝牙发送数据。发送数据并建立与配对设备的连接工作正常。但是,我无法让“发现新蓝牙设备”功能发挥作用。
在我的Plugin类中(也处理发送数据等,运行正常):

public void startBluetoothDeviceDiscovery(){

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        currentActivity.registerReceiver(receiver, filter);

        if (bluetoothAdapter.isDiscovering()){
            bluetoothAdapter.cancelDiscovery();
        }
        bluetoothAdapter.startDiscovery();

    }

方法startBluetoothDeviceDiscovery肯定会被调用(由按钮触发),并且bluetoothAdapter已设置且不为null。设备上的蓝牙始终打开。
同样在Plugin类中:

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

            }
        }
    }

基本上,什么Android文档建议,当发现新设备.https://developer.android.com/guide/topics/connectivity/bluetooth#DiscoverDevices
我想,也许周围没有设备,所以我希望接收器在发现开始时已经被触发,所以我试图寻找BluetoothAdapter.ACTION_DISCOVERY_STARTED

IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        currentActivity.registerReceiver(receiver, filter);

我的Receiver在onReceive中:

@Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();

            if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {

                 // discovery started, do stuff

            }

        }

每次单击按钮并调用startBluetoothDeviceDiscovery()方法时都应调用此函数,因为它总是启动蓝牙发现,但它仍然不工作。
我的最后一个猜测是,由于插件不是一个活动,BroadcastReceiver无法访问。所以我把动作改成了BluetoothAdapter.ACTION_STATE_CHANGED,然后当我打开和关闭设备上的蓝牙时,它突然被onReceive()接收到了。
我的清单(复制到正确的Unity文件夹位置):

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>

那么,为什么我打开bluetoothAdapter.startDiscovery()并发现接收器中未接收到设备?

感谢您的任何帮助或想法:)

ru9i0ody

ru9i0ody1#

您是否在Unity Asset Store中尝试过蓝牙插件?我在做同样的事情,但使用蓝牙经典(HC-06),我使用的插件从资产商店。我的应用程序将只使用BT模块,因此在Unity中的C#脚本中我写道:

device = new BluetoothDevice();
    device.Name = "HC-05";
    //device.MacAddress = "XX:XX:XX:XX:XX:XX";
            device.send (System.Text.Encoding.ASCII.GetBytes ("Hello\n"));
        }
    }
    public void connect() {
        statusText.text = "Status : ...";
        device.connect();

    }

    public void disconnect() {
        device.close();
    }

    public void sendHello() {
        if (device != null) {
    void Update() {
        if (device.IsReading) {

            byte [] msg = device.read ();

            if (msg != null ) {

                string content = System.Text.ASCIIEncoding.ASCII.GetString (msg);

                statusText.text = "MSG : " + content;

            } 
        }
    }
}

相关问题