diff --git a/Assets/Plugins/Android/unityandroidbluetoothlelib.jar b/Assets/Plugins/Android/unityandroidbluetoothlelib.jar index 21f7f248..8731dae3 100644 Binary files a/Assets/Plugins/Android/unityandroidbluetoothlelib.jar and b/Assets/Plugins/Android/unityandroidbluetoothlelib.jar differ diff --git a/Assets/Plugins/BluetoothDeviceScript.cs b/Assets/Plugins/BluetoothDeviceScript.cs index 14d1b0d6..7685952f 100644 --- a/Assets/Plugins/BluetoothDeviceScript.cs +++ b/Assets/Plugins/BluetoothDeviceScript.cs @@ -24,6 +24,7 @@ public class BluetoothDeviceScript : MonoBehaviour public Action PeripheralReceivedWriteDataAction; public Action ConnectedPeripheralAction; public Action ConnectedDisconnectPeripheralAction { get; set; } + public Action ConnectErrorAction { get; set; } public Action DisconnectedPeripheralAction; public Action DeviceBleStatusDisconnectedAction; public Action 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) diff --git a/Assets/Plugins/BluetoothHardwareInterface.cs b/Assets/Plugins/BluetoothHardwareInterface.cs index 14b9cf82..93c85514 100644 --- a/Assets/Plugins/BluetoothHardwareInterface.cs +++ b/Assets/Plugins/BluetoothHardwareInterface.cs @@ -604,7 +604,7 @@ public class BluetoothLEHardwareInterface #endif } - public static void ConnectToPeripheral (string name, Action connectAction, Action serviceAction, Action characteristicAction, Action disconnectAction = null) + public static void ConnectToPeripheral (string name, Action connectAction, Action serviceAction, Action characteristicAction, Action disconnectAction = null, Action 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) diff --git a/Assets/Scripts/Devices/Ble/BleDevice.cs b/Assets/Scripts/Devices/Ble/BleDevice.cs index cccf4679..7228e351 100644 --- a/Assets/Scripts/Devices/Ble/BleDevice.cs +++ b/Assets/Scripts/Devices/Ble/BleDevice.cs @@ -106,6 +106,7 @@ namespace Assets.Scripts.Devices.Ble if (response.Error != null) { + this.State = DeviceState.Disconnected; if (response.Error.Code == WclBleErrors.WCL_E_BLUETOOTH_LE_DEVICE_NOT_FOUND) { Debug.Log("未找到设备"); diff --git a/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs b/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs index 58d55173..b2c1a994 100644 --- a/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs +++ b/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs @@ -9,6 +9,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Timers; using UnityEngine; namespace Assets.Scripts.Ble @@ -210,30 +211,41 @@ namespace Assets.Scripts.Ble List servicelist = new List(); List characteristilist = new List(); + public void ConnectPeripheral(BlePeripheralInfo info, Action callback) { - if (!this.callbacks.ContainsKey(info)) + var exsit = this.callbacks.Where(c => c.Key.Address == info.Address).Any(); + if (!exsit) { this.callbacks.Add(info, callback); } + else + { + var obj = this.callbacks.Where(c => c.Key.Address == info.Address).FirstOrDefault(); + this.callbacks.Remove(obj.Key); + this.callbacks.Add(info, callback); + } Debug.Log("try connect device" + info.Name + this.callbacks.Count.ToString()); + var self = this; if (this.callbacks.Count == 1) { - BleResponse s = new BleResponse(); - s.IsSuccess = true; - s.Error = null; - var self = this; BluetoothLEHardwareInterface.ConnectToPeripheral(info.Address, (address) => { + BleResponse s = new BleResponse + { + IsSuccess = true, + Error = null + }; callback?.Invoke(self, info, s); this.callbacks.Remove(info); - Debug.Log("连接成功!"+info.Name); + Debug.Log("连接成功!" + info.Name); if (this.callbacks.Any()) { this.ConnectPeripheral(this.callbacks.First().Key, this.callbacks.First().Value); } }, (address, service) => { + Debug.Log("发现服务"); ServicesDiscovered(address, service); }, (address, service, characteristic) => { @@ -242,9 +254,25 @@ namespace Assets.Scripts.Ble { Debug.Log("disconnect device:" + address); PeripheralDisconnected(address, info); - }); + }, + (address, error) => + { + BleResponse s = new BleResponse + { + IsSuccess = false, + Error = new BleHwInterfaceError(error) + }; + callback?.Invoke(self, info, s); + this.callbacks.Remove(info); + if (this.callbacks.Any()) + { + this.ConnectPeripheral(this.callbacks.First().Key, this.callbacks.First().Value); + } + } + ); } } + #region 私有方法 //服务发现 private void ServicesDiscovered(string address, string service) @@ -305,7 +333,7 @@ namespace Assets.Scripts.Ble } var characteristicsDiscoveredCallback = characteristicsDiscoveredCallbacks.Where(c => c.Key.MatchAddress(address)); - if (serviceCallback.Any()) + if (characteristicsDiscoveredCallback.Any()) { characteristicsDiscoveredCallbacks.Remove(characteristicsDiscoveredCallback.First().Key); }