Unity连接低功耗蓝牙设备

一、前言

工作中需求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");
        });
    }
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,723评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,485评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,998评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,323评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,355评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,079评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,389评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,019评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,519评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,971评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,100评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,738评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,293评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,289评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,517评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,547评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,834评论 2 345

推荐阅读更多精彩内容