Unity連接低功耗藍牙設(shè)備

一、前言

工作中需求Unity 連接一個低功耗藍牙模塊收發(fā)信息的功能。
使用《Bluetooth LE for iOS tvOS and Android 2.55》插件實現(xiàn)。
導(dǎo)入插件打包apk時會報錯,應(yīng)該是AndroidManifest文件里面的錯誤,使用下面代碼替換

<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"/>
<!--  Android 10(API 29)及以上可選后臺位置權(quán)限(如需后臺掃描)  -->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<!--  Android 12(API 31)及以上需要以下權(quán)限  -->
<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>

二、實現(xiàn)

1、初始化

BluetoothLEHardwareInterface.Initialize(true, false, () =>
        {
            Debug.Log("藍牙初始化成功");           //初始化成功執(zhí)行回調(diào)
            Invoke("FindDevice", 0.1f);
        }, (error) =>
        {
            Debug.Log("藍牙初始化失敗請打開藍牙");  //初始化失敗的回調(diào)響應(yīng)
        });

2、掃描設(shè)備,通過設(shè)備名稱找到藍牙模塊設(shè)備

public void FindDevice()
    {
        Debug.Log("開始搜索藍牙設(shè)備");
        BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null,
            (address, name) =>
                {
                    if (name.Contains(DeviceName))
                    {
                        //掃描處理,加入設(shè)備列表              
                        Debug.Log("找到所需設(shè)備");
                        DeviceAddress = address;
                        ConnectDevice();
                    }
                },
                null);
    }

3、連接藍牙設(shè)備,通過我們需要找的ServiceUUID和CharacteristicUUID連接

public void ConnectDevice()
    {     
        Debug.Log("連接藍牙設(shè)備");
        BluetoothLEHardwareInterface.StopScan();
        BluetoothLEHardwareInterface.ConnectToPeripheral(DeviceAddress, null, null,
            (address, serviceUUID, characteristicUUID) =>
            {
                Debug.Log("連接藍牙成功");
                SubscribeCharacteristicWithDeviceAddress();
            });
    }

4、訂閱服務(wù)
訂閱藍牙服務(wù)之后我們就可以進行我們需要的數(shù)據(jù)操作了,OnCharacteristicNotification會返回接收到的數(shù)據(jù),其實具體的數(shù)據(jù)接發(fā)模式還有很多,這里看我的項目需求和設(shè)備類型是這樣的,更多的可以去查藍牙的模式。

private void SubscribeCharacteristicWithDeviceAddress()
    {
        Debug.Log("訂閱服務(wù)");
        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 = "發(fā)送";
            BluetoothLEHardwareInterface.Log("Write Succeeded");
        });
    }

6、讀取藍牙模塊發(fā)送的信息

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

7、取消訂閱服務(wù)

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

8、斷開連接藍牙

BluetoothLEHardwareInterface.DisconnectPeripheral(DeviceAddress, (address) =>
                            {
                                Debug.Log("斷開藍牙連接");
                                StatusMessage = "Device disconnected";
                                BluetoothLEHardwareInterface.DeInitialize(() =>
                                {
                                     Debug.Log("取消初始化");
                                });
                            });

9、讀取藍牙信號強度,可以設(shè)置2s執(zhí)行一次

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

三、根據(jù)示例場景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.
                        //請注意,第一個參數(shù)是地址,而不是名稱。由于向后兼容性,我沒有解決這個問題。
                        //還要注意,我注意到我沒有使用前兩個回調(diào)。如果你不是在尋找特定的特征,你可以使用前兩個特征中的一個,
                        //但請記住,設(shè)備會枚舉所有內(nèi)容,因此你需要有一個足夠大的超時,以便在你嘗試訂閱或執(zhí)行任何其他操作之前完成枚舉。

                        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 = "發(fā)送";
            BluetoothLEHardwareInterface.Log("Write Succeeded");
        });
    }
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容