简体   繁体   English

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

[英]How to discover Bluetooth devices in a Unity Android Plugin?

I am developing a Unity game for Android , which uses a Android Plugin to be able to send data via Bluetooth during the game.我正在为Android开发Unity游戏,它使用Android 插件能够在游戏期间通过蓝牙发送数据。 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):在我的插件 class (它还处理发送数据等,工作正常):

 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.方法 startBluetoothDeviceDiscovery 肯定会被调用(由按钮触发),并且 bluetoothAdapter 已经设置,而不是 null。 Bluetooth on the device was always ON.设备上的蓝牙始终处于开启状态。

Also inside the Plugin class :也在插件 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.基本上,在发现新设备时,Android 文档所建议的内容。 https://developer.android.com/guide/topics/connectivity/bluetooth#DiscoverDevices 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 :我想,也许周围没有设备,所以我希望在发现开始时已经触发接收器,所以我试图寻找BluetoothAdapter.ACTION_DISCOVERY_STARTED

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

And in my Receiver at onReceive :在我的接收器 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.每次单击按钮并调用方法startBluetoothDeviceDiscovery()时都应该调用它,因为它总是启动蓝牙发现,但它仍然不起作用。

My last guess was, that due to the Plugin not being an activity, the BroadcastReceiver can not be accessed.我最后的猜测是,由于插件不是活动,BroadcastReceiver 无法访问。 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() .因此,我将操作更改为BluetoothAdapter.ACTION_STATE_CHANGED ,然后当我打开和关闭设备上的蓝牙时,它突然在onReceive()中收到。

My Manifest (copied to the correct Unity folder location):我的清单(复制到正确的 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>

So why is my turning on bluetoothAdapter.startDiscovery() and finding devices not received in the receiver?那么为什么我打开bluetoothAdapter.startDiscovery()并发现接收器中未收到的设备?

Thanks for ANY help or ideas:)感谢您的任何帮助或想法:)

Have you tried Bluetooth plugin in Unity Asset Store?您是否在 Unity Asset Store 中尝试过蓝牙插件? I'm doing same thing but using Bluetooth Classic (HC-06) and I'm using plugin from Asset store.我正在做同样的事情,但使用的是蓝牙经典(HC-06)并且我使用的是资产商店的插件。 My app will only use that BT module so in C# script in Unity I wrote:我的应用程序将仅使用该 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;

            } 
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM