美文网首页
Unity连接低功耗蓝牙设备

Unity连接低功耗蓝牙设备

作者: 玄策丶 | 来源:发表于2024-05-07 17:34 被阅读0次

一、前言

工作中需求Unity 连接一个低功耗蓝牙模块收发信息的功能。
使用《Bluetooth LE for iOS tvOS and Android 2.55》插件实现。
导入插件打包apk时会报错,应该是AndroidManifest文件里面的错误,使用下面代码替换

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unity3d.player"
    android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0">
    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:anyDensity="true"/>
    
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/>
    <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
    
    <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30"/>
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

    <application
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:debuggable="true">
        <activity android:name="com.unity3d.player.UnityPlayerProxyActivity"
                  android:label="@string/app_name"
                  android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
            
        </activity>
        <activity android:name="com.unity3d.player.UnityPlayerActivity"
                  android:label="@string/app_name"
                  android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
                  android:label="@string/app_name"
                  android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
            <meta-data android:name="android.app.lib_name" android:value="unity" />
            <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
        </activity>
    </application>
</manifest>

二、实现

1、初始化

BluetoothLEHardwareInterface.Initialize(true, false, () =>
        {
            Debug.Log("蓝牙初始化成功");           //初始化成功执行回调
            Invoke("FindDevice", 0.1f);
        }, (error) =>
        {
            Debug.Log("蓝牙初始化失败请打开蓝牙");  //初始化失败的回调响应
        });

2、扫描设备,通过设备名称找到蓝牙模块设备

public void FindDevice()
    {
        Debug.Log("开始搜索蓝牙设备");
        BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null,
            (address, name) =>
                {
                    if (name.Contains(DeviceName))
                    {
                        //扫描处理,加入设备列表              
                        Debug.Log("找到所需设备");
                        DeviceAddress = address;
                        ConnectDevice();
                    }
                },
                null);
    }

3、连接蓝牙设备,通过我们需要找的ServiceUUID和CharacteristicUUID连接

public void ConnectDevice()
    {     
        Debug.Log("连接蓝牙设备");
        BluetoothLEHardwareInterface.StopScan();
        BluetoothLEHardwareInterface.ConnectToPeripheral(DeviceAddress, null, null,
            (address, serviceUUID, characteristicUUID) =>
            {
                Debug.Log("连接蓝牙成功");
                SubscribeCharacteristicWithDeviceAddress();
            });
    }

4、订阅服务
订阅蓝牙服务之后我们就可以进行我们需要的数据操作了,OnCharacteristicNotification会返回接收到的数据,其实具体的数据接发模式还有很多,这里看我的项目需求和设备类型是这样的,更多的可以去查蓝牙的模式。

private void SubscribeCharacteristicWithDeviceAddress()
    {
        Debug.Log("订阅服务");
        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress
            (DeviceAddress, ServiceUUID, CharacteristicUUID, 
(notifyAddress, notifyCharacteristic) =>
                        {
                            Debug.Log(1);
                            //通过UUID读取信息
                            BluetoothLEHardwareInterface.ReadCharacteristic(DeviceAddress, ServiceUUID,    ButtonUUID, (characteristic, bytes) =>
                            {
                                 Debug.Log(bytes);
                            });
                        }, 
(address, characteristicUUID, bytes) =>
                        {
                            if (_state != States.None)
                            {
                                Debug.Log(2);
                            }
                           Debug.Log(bytes);
                        }););
    }

5、写入蓝牙模块信息

public void btnClick()
    {
        SendByte(new byte[] { 0xA7, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });
    }
void SendByte(byte[] data)
    {
        Debug.Log("总:"+ data.Length);
        for (int i = 0; i < data.Length; i++)
        {
            Debug.Log(i + " : " + data[i]);
        }

        BluetoothLEHardwareInterface.WriteCharacteristic(DeviceAddress, ServiceUUID, writeId, data, data.Length, false, (characteristicUUID) =>
        {
            StatusMessage = "发送";
            BluetoothLEHardwareInterface.Log("Write Succeeded");
        });
    }

6、读取蓝牙模块发送的信息

BluetoothLEHardwareInterface.ReadCharacteristic(DeviceAddress, ServiceUUID,    notifyId , (characteristic, bytes) =>
                            {
                                 Debug.Log(bytes);
                            });

7、取消订阅服务

 BluetoothLEHardwareInterface.UnSubscribeCharacteristic(DeviceAddress, ServiceUUID, notifyId , null);

8、断开连接蓝牙

BluetoothLEHardwareInterface.DisconnectPeripheral(DeviceAddress, (address) =>
                            {
                                Debug.Log("断开蓝牙连接");
                                StatusMessage = "Device disconnected";
                                BluetoothLEHardwareInterface.DeInitialize(() =>
                                {
                                     Debug.Log("取消初始化");
                                });
                            });

9、读取蓝牙信号强度,可以设置2s执行一次

BluetoothLEHardwareInterface.ReadRSSI(DeviceAddress, (address, rssi) =>
                        {
                            Debug.Log("rssi");
                        });

三、根据示例场景StartingExample修改代码

using UnityEngine;
using UnityEngine.UI;

public class StartingExample : MonoBehaviour
{
    string DeviceName = "YX_202308070464";
    string ServiceUUID = "0000FFB0-0000-1000-8000-00805F9B34FB";
    string notifyId = "0000FFB2-0000-1000-8000-00805F9B34FB";
    string writeId = "0000FFB1-0000-1000-8000-00805F9B34FB";

    enum States
    {
        None,
        Scan,
        ScanRSSI,
        ReadRSSI,
        Connect,
        RequestMTU,
        Subscribe,
        Unsubscribe,
        Disconnect,
    }

    private bool _connected = false;
    private float _timeout = 0f;
    private States _state = States.None;
    private string _deviceAddress;
    private bool _foundButtonUUID = false;
    private bool _foundLedUUID = false;
    private bool _rssiOnly = false;
    private int _rssi = 0;

    public Text StatusText;
    public Text ButtonPositionText;

    private string StatusMessage
    {
        set
        {
            BluetoothLEHardwareInterface.Log(value);
            StatusText.text = value;
        }
    }

    void Reset()
    {
        _connected = false;
        _timeout = 0f;
        _state = States.None;
        _deviceAddress = null;
        _foundButtonUUID = false;
        _foundLedUUID = false;
        _rssi = 0;
    }

    void SetState(States newState, float timeout)
    {
        _state = newState;
        _timeout = timeout;
    }

    void StartProcess()
    {
        Reset();
        BluetoothLEHardwareInterface.Initialize(true, false, () =>
        {

            SetState(States.Scan, 0.1f);

        }, (error) =>
        {

            StatusMessage = "Error during initialize: " + error;
        });
    }

    void Start()
    {
        StartProcess();
    }

    private void ProcessButton(byte[] bytes)
    {
        if (bytes[0] == 0x00)
            ButtonPositionText.text = "Not Pushed";
        else
            ButtonPositionText.text = "Pushed";
    }


    void Update()
    {
        if (_timeout > 0f)
        {
            _timeout -= Time.deltaTime;
            if (_timeout <= 0f)
            {
                _timeout = 0f;

                switch (_state)
                {
                    case States.None:
                        break;

                    case States.Scan:
                        StatusMessage = "Scanning for " + DeviceName;
                        Debug.Log(1);
                        BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, 
                            (address, name) =>
                        {
                            // if your device does not advertise the rssi and manufacturer specific data
                            // then you must use this callback because the next callback only gets called
                            // if you have manufacturer specific data

                            if (!_rssiOnly)
                            {
                                if (name.Contains(DeviceName))
                                {
                                    StatusMessage = "Found " + name;
                                    Debug.Log(2);
                                    // found a device with the name we want
                                    // this example does not deal with finding more than one
                                    _deviceAddress = address;
                                    SetState(States.Connect, 0.5f);
                                }
                            }

                        }, 
                            (address, name, rssi, bytes) =>
                        {

                            // use this one if the device responses with manufacturer specific data and the rssi

                            if (name.Contains(DeviceName))
                            {
                                StatusMessage = "Found " + name;

                                if (_rssiOnly)
                                {
                                    _rssi = rssi;
                                }
                                else
                                {
                                    // found a device with the name we want
                                    // this example does not deal with finding more than one
                                    _deviceAddress = address;
                                    SetState(States.Connect, 0.5f);
                                }
                            }

                        }, _rssiOnly); // this last setting allows RFduino to send RSSI without having manufacturer data

                        if (_rssiOnly)
                            SetState(States.ScanRSSI, 0.5f);
                        break;

                    case States.ScanRSSI:
                        break;

                    case States.ReadRSSI:
                        StatusMessage = $"Call Read RSSI";
                        Debug.Log(4);
                        BluetoothLEHardwareInterface.ReadRSSI(_deviceAddress, (address, rssi) =>
                        {
                            StatusMessage = $"Read RSSI: {rssi}";
                        });

                        SetState(States.ReadRSSI, 2f);
                        break;

                    case States.Connect:
                        StatusMessage = "Connecting...";
                        Debug.Log(5);
                        // set these flags
                        _foundButtonUUID = false;
                        _foundLedUUID = false;

                        // note that the first parameter is the address, not the name. I have not fixed this because
                        // of backwards compatiblity.
                        // also note that I am not using the first 2 callbacks. If you are not looking for specific characteristics you can use one of
                        // the first 2, but keep in mind that the device will enumerate everything and so you will want to have a timeout
                        // large enough that it will be finished enumerating before you try to subscribe or do any other operations.
                        //请注意,第一个参数是地址,而不是名称。由于向后兼容性,我没有解决这个问题。
                        //还要注意,我注意到我没有使用前两个回调。如果你不是在寻找特定的特征,你可以使用前两个特征中的一个,
                        //但请记住,设备会枚举所有内容,因此你需要有一个足够大的超时,以便在你尝试订阅或执行任何其他操作之前完成枚举。

                        BluetoothLEHardwareInterface.ConnectToPeripheral(_deviceAddress, null, null, (address, serviceUUID, characteristicUUID) =>
                        {
                            StatusMessage = "Connected...";

                            BluetoothLEHardwareInterface.StopScan();

                            StatusMessage = address;

                            if (IsEqual(serviceUUID, ServiceUUID))
                            {
                                Debug.Log(6.5f);
                                StatusMessage = "Found Service UUID";

                                _foundButtonUUID = _foundButtonUUID || IsEqual(characteristicUUID, writeId);

                                Debug.Log(_foundButtonUUID);
                                // if we have found both characteristics that we are waiting for
                                // set the state. make sure there is enough timeout that if the
                                // device is still enumerating other characteristics it finishes
                                // before we try to subscribe
                                if (_foundButtonUUID /*&& _foundLedUUID*/)
                                {
                                    Debug.Log(6.6f);
                                    _connected = true;
                                    SetState(States.RequestMTU, 2f);
                                }
                            }
                        });
                        break;

                    case States.RequestMTU:
                        StatusMessage = "Requesting MTU";
                        Debug.Log(7);
                        BluetoothLEHardwareInterface.RequestMtu(_deviceAddress, 185, (address, newMTU) =>
                        {
                            StatusMessage = "MTU set to " + newMTU.ToString();
                            Debug.Log(8);
                            SetState(States.Subscribe, 0.1f);
                        });
                        break;

                    case States.Subscribe:
                        StatusMessage = "Subscribing to characteristics...";
                        Debug.Log(9);
                        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(_deviceAddress, ServiceUUID, writeId, null, null);
                        Debug.Log(10);
                        //(notifyAddress, notifyCharacteristic) =>
                        //{
                        //    Debug.Log(10);
                        //    StatusMessage = "Waiting for user action (1)...";
                        //    _state = States.None;

                        //    // read the initial state of the button
                        //    //BluetoothLEHardwareInterface.ReadCharacteristic(_deviceAddress, ServiceUUID, notifyId, (characteristic, bytes) =>
                        //    //{
                        //    //    ProcessButton(bytes);
                        //    //});

                        //    SetState(States.ReadRSSI, 1f);

                        //}, 
                        //(address, characteristicUUID, bytes) =>
                        //{
                        //    if (_state != States.None)
                        //    {
                        //        // some devices do not properly send the notification state change which calls
                        //        // the lambda just above this one so in those cases we don't have a great way to
                        //        // set the state other than waiting until we actually got some data back.
                        //        // The esp32 sends the notification above, but if yuor device doesn't you would have
                        //        // to send data like pressing the button on the esp32 as the sketch for this demo
                        //        // would then send data to trigger this.
                        //        StatusMessage = "Waiting for user action (2)...";
                        //        Debug.Log(11);
                        //        SetState(States.ReadRSSI, 1f);
                        //    }

                        //    // we received some data from the device
                        //    ProcessButton(bytes);
                        //});
                        break;

                    case States.Unsubscribe:
                        BluetoothLEHardwareInterface.UnSubscribeCharacteristic(_deviceAddress, ServiceUUID, writeId, null);
                        SetState(States.Disconnect, 4f);
                        break;

                    case States.Disconnect:
                        StatusMessage = "Commanded disconnect.";

                        if (_connected)
                        {
                            BluetoothLEHardwareInterface.DisconnectPeripheral(_deviceAddress, (address) =>
                            {
                                StatusMessage = "Device disconnected";
                                BluetoothLEHardwareInterface.DeInitialize(() =>
                                {
                                    _connected = false;
                                    _state = States.None;
                                });
                            });
                        }
                        else
                        {
                            BluetoothLEHardwareInterface.DeInitialize(() =>
                            {
                                _state = States.None;
                            });
                        }
                        break;
                }
            }
        }
    }

    private bool ledON = false;
    public void OnLED()
    {
        ledON = !ledON;
        if (ledON)
        {
            //SendByte(0x01);
        }
        else
        {
            //SendByte(0x00);
        }
    }

    
    string FullUUID(string uuid)
    {
        string fullUUID = uuid;
        if (fullUUID.Length == 4)
            fullUUID = "0000" + uuid + "-0000-1000-8000-00805f9b34fb";

        return fullUUID;
    }

    bool IsEqual(string uuid1, string uuid2)
    {
        if (uuid1.Length == 4)
            uuid1 = FullUUID(uuid1);
        if (uuid2.Length == 4)
            uuid2 = FullUUID(uuid2);

        return (uuid1.ToUpper().Equals(uuid2.ToUpper()));
    }

    public void setPsw()
    {
        SendByte(new byte[] { 0xA0, 0x0F, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 });
    }
    public void btnOpen()
    {
        SendByte(new byte[] { 0xA4, 0x09, 0xFF, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });
    }
    public void btnClose()
    {
        SendByte(new byte[] { 0xA4, 0x09, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });
    }
    public void btnClick()
    {
        Debug.Log(12);
        SendByte(new byte[] { 0xA7, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });

    }
    void SendByte(byte[] data)
    {
        Debug.Log("总:"+ data.Length);
        for (int i = 0; i < data.Length; i++)
        {
            Debug.Log(i + " : " + data[i]);
        }

        BluetoothLEHardwareInterface.WriteCharacteristic(_deviceAddress, ServiceUUID, writeId, data, data.Length, false, (characteristicUUID) =>
        {
            Debug.Log(13);
            StatusMessage = "发送";
            BluetoothLEHardwareInterface.Log("Write Succeeded");
        });
    }
}


相关文章

网友评论

      本文标题:Unity连接低功耗蓝牙设备

      本文链接:https://www.haomeiwen.com/subject/dvzrfjtx.html