设备连接稳定性

This commit is contained in:
lishuo authored and CaiYanPeng committed 2022-06-08 16:22:07 +08:00
1 parent c333086e30
commit 46b6c7fa01
9 files changed
+111 -62

No files matched your search

+8 -1
View File
@@ -24,6 +24,7 @@ public class BluetoothDeviceScript : MonoBehaviour
public Action<string, byte[]> PeripheralReceivedWriteDataAction;
public Action<string> ConnectedPeripheralAction;
public Action<string> ConnectedDisconnectPeripheralAction { get; set; }
public Action<string,string> ConnectErrorAction { get; set; }
public Action<string> DisconnectedPeripheralAction;
public Action<string> DeviceBleStatusDisconnectedAction;
public Action<string, string> DiscoveredServiceAction;
@@ -53,10 +54,11 @@ public class BluetoothDeviceScript : MonoBehaviour
void Update ()
{
}
const string connectionError = "ConnectionError";
const string deviceInitializedString = "Initialized";
const string deviceDeInitializedString = "DeInitialized";
const string deviceErrorString = "Error";
const string deviceConnectErrorString = "ConnectionError";
const string deviceServiceAdded = "ServiceAdded";
const string deviceStartedAdvertising = "StartedAdvertising";
const string deviceStoppedAdvertising = "StoppedAdvertising";
@@ -77,6 +79,7 @@ public class BluetoothDeviceScript : MonoBehaviour
public void OnBluetoothMessage (string message)
{
Debug.Log(message);
if (message != null)
{
char[] delim = new char[] { '~' };
@@ -242,6 +245,10 @@ public class BluetoothDeviceScript : MonoBehaviour
DisconnectedPeripheralAction(parts[1]);
}
}
else if (message.Length > connectionError.Length && message.Substring(0,connectionError.Length) == connectionError){
if (ConnectErrorAction != null)
ConnectErrorAction(parts[1], parts[2]);
}
else if (message.Length >= deviceDiscoveredService.Length && message.Substring (0, deviceDiscoveredService.Length) == deviceDiscoveredService)
{
if (parts.Length >= 3 && DiscoveredServiceAction != null)
+2 -1
View File
@@ -604,7 +604,7 @@ public class BluetoothLEHardwareInterface
#endif
}
public static void ConnectToPeripheral (string name, Action<string> connectAction, Action<string, string> serviceAction, Action<string, string, string> characteristicAction, Action<string> disconnectAction = null)
public static void ConnectToPeripheral (string name, Action<string> connectAction, Action<string, string> serviceAction, Action<string, string, string> characteristicAction, Action<string> disconnectAction = null, Action<string,string> errorAction = null)
{
#if !UNITY_EDITOR_OSX || !EXPERIMENTAL_MACOS_EDITOR
@@ -618,6 +618,7 @@ public class BluetoothLEHardwareInterface
bluetoothDeviceScript.DiscoveredCharacteristicAction = characteristicAction;
bluetoothDeviceScript.ConnectedDisconnectPeripheralAction = disconnectAction;
bluetoothDeviceScript.DeviceBleStatusDisconnectedAction = disconnectAction;
bluetoothDeviceScript.ConnectErrorAction = errorAction;
}
#if EXPERIMENTAL_MACOS_EDITOR && (UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX)