一言蛇、前言
工作中需求Unity 連接一個(gè)低功耗藍(lán)牙模塊收發(fā)信息的功能瑟匆。
使用《Bluetooth LE for iOS tvOS and Android 2.55》插件實(shí)現(xiàn)赖淤。
導(dǎo)入插件打包apk時(shí)會(huì)報(bào)錯(cuò),應(yīng)該是AndroidManifest文件里面的錯(cuò)誤,使用下面代碼替換
<?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>
二尿赚、實(shí)現(xiàn)
1添忘、初始化
BluetoothLEHardwareInterface.Initialize(true, false, () =>
{
Debug.Log("藍(lán)牙初始化成功"); //初始化成功執(zhí)行回調(diào)
Invoke("FindDevice", 0.1f);
}, (error) =>
{
Debug.Log("藍(lán)牙初始化失敗請(qǐng)打開(kāi)藍(lán)牙"); //初始化失敗的回調(diào)響應(yīng)
});
2采呐、掃描設(shè)備,通過(guò)設(shè)備名稱找到藍(lán)牙模塊設(shè)備
public void FindDevice()
{
Debug.Log("開(kāi)始搜索藍(lán)牙設(shè)備");
BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null,
(address, name) =>
{
if (name.Contains(DeviceName))
{
//掃描處理搁骑,加入設(shè)備列表
Debug.Log("找到所需設(shè)備");
DeviceAddress = address;
ConnectDevice();
}
},
null);
}
3斧吐、連接藍(lán)牙設(shè)備又固,通過(guò)我們需要找的ServiceUUID和CharacteristicUUID連接
public void ConnectDevice()
{
Debug.Log("連接藍(lán)牙設(shè)備");
BluetoothLEHardwareInterface.StopScan();
BluetoothLEHardwareInterface.ConnectToPeripheral(DeviceAddress, null, null,
(address, serviceUUID, characteristicUUID) =>
{
Debug.Log("連接藍(lán)牙成功");
SubscribeCharacteristicWithDeviceAddress();
});
}
4、訂閱服務(wù)
訂閱藍(lán)牙服務(wù)之后我們就可以進(jìn)行我們需要的數(shù)據(jù)操作了煤率,OnCharacteristicNotification會(huì)返回接收到的數(shù)據(jù)仰冠,其實(shí)具體的數(shù)據(jù)接發(fā)模式還有很多,這里看我的項(xiàng)目需求和設(shè)備類型是這樣的蝶糯,更多的可以去查藍(lán)牙的模式洋只。
private void SubscribeCharacteristicWithDeviceAddress()
{
Debug.Log("訂閱服務(wù)");
BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress
(DeviceAddress, ServiceUUID, CharacteristicUUID,
(notifyAddress, notifyCharacteristic) =>
{
Debug.Log(1);
//通過(guò)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、寫入藍(lán)牙模塊信息
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昼捍、讀取藍(lán)牙模塊發(fā)送的信息
BluetoothLEHardwareInterface.ReadCharacteristic(DeviceAddress, ServiceUUID, notifyId , (characteristic, bytes) =>
{
Debug.Log(bytes);
});
7识虚、取消訂閱服務(wù)
BluetoothLEHardwareInterface.UnSubscribeCharacteristic(DeviceAddress, ServiceUUID, notifyId , null);
8、斷開(kāi)連接藍(lán)牙
BluetoothLEHardwareInterface.DisconnectPeripheral(DeviceAddress, (address) =>
{
Debug.Log("斷開(kāi)藍(lán)牙連接");
StatusMessage = "Device disconnected";
BluetoothLEHardwareInterface.DeInitialize(() =>
{
Debug.Log("取消初始化");
});
});
9妒茬、讀取藍(lán)牙信號(hào)強(qiáng)度担锤,可以設(shè)置2s執(zhí)行一次
BluetoothLEHardwareInterface.ReadRSSI(DeviceAddress, (address, rssi) =>
{
Debug.Log("rssi");
});
三、根據(jù)示例場(chǎng)景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.
//請(qǐng)注意乍钻,第一個(gè)參數(shù)是地址肛循,而不是名稱。由于向后兼容性银择,我沒(méi)有解決這個(gè)問(wèn)題多糠。
//還要注意,我注意到我沒(méi)有使用前兩個(gè)回調(diào)浩考。如果你不是在尋找特定的特征夹孔,你可以使用前兩個(gè)特征中的一個(gè),
//但請(qǐng)記住怀挠,設(shè)備會(huì)枚舉所有內(nèi)容析蝴,因此你需要有一個(gè)足夠大的超時(shí)害捕,以便在你嘗試訂閱或執(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");
});
}
}