diff --git a/Assets/Plugins/Android/unityandroidbluetoothlelib.jar b/Assets/Plugins/Android/unityandroidbluetoothlelib.jar index 588e1621..ec45ca2a 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 102148b9..d571e0a4 100644 --- a/Assets/Plugins/BluetoothDeviceScript.cs +++ b/Assets/Plugins/BluetoothDeviceScript.cs @@ -23,8 +23,9 @@ public class BluetoothDeviceScript : MonoBehaviour public Action RetrievedConnectedPeripheralAction; public Action PeripheralReceivedWriteDataAction; public Action ConnectedPeripheralAction; - public Action ConnectedDisconnectPeripheralAction; + public Action ConnectedDisconnectPeripheralAction { get; set; } public Action DisconnectedPeripheralAction; + public Action DeviceBleStatusDisconnectedAction; public Action DiscoveredServiceAction; public Action DiscoveredCharacteristicAction; public Action DidWriteCharacteristicAction; @@ -80,11 +81,14 @@ public class BluetoothDeviceScript : MonoBehaviour { char[] delim = new char[] { '~' }; string[] parts = message.Split (delim); - if (parts[0].Equals("DeviceStateChanged")) - { - //throw new Exception("test"); - } - if (parts[0].Equals(bleStatusChanged)) + if (parts[0].Equals("DeviceStateChanged")) + { + if (DeviceBleStatusDisconnectedAction != null && parts[1].Equals("0")) + { + DeviceBleStatusDisconnectedAction(parts[2]); + } + } + if (parts[0].Equals(bleStatusChanged)) { if (BleStatusChangedAction != null) { @@ -231,12 +235,12 @@ public class BluetoothDeviceScript : MonoBehaviour { if (parts.Length >= 2) { - if (ConnectedDisconnectPeripheralAction != null) - ConnectedDisconnectPeripheralAction (parts[1]); + if (ConnectedDisconnectPeripheralAction != null) + ConnectedDisconnectPeripheralAction(parts[1]); - if (DisconnectedPeripheralAction != null) - DisconnectedPeripheralAction (parts[1]); - } + if (DisconnectedPeripheralAction != null) + DisconnectedPeripheralAction(parts[1]); + } } else if (message.Length >= deviceDiscoveredService.Length && message.Substring (0, deviceDiscoveredService.Length) == deviceDiscoveredService) { diff --git a/Assets/Plugins/BluetoothHardwareInterface.cs b/Assets/Plugins/BluetoothHardwareInterface.cs index 4c80d913..674d2410 100644 --- a/Assets/Plugins/BluetoothHardwareInterface.cs +++ b/Assets/Plugins/BluetoothHardwareInterface.cs @@ -26,6 +26,7 @@ using UnityEngine; using System; using System.Runtime.InteropServices; using System.Collections.Generic; +using System.Threading; #if UNITY_2018_3_OR_NEWER #if UNITY_ANDROID @@ -605,6 +606,7 @@ public class BluetoothLEHardwareInterface public static void ConnectToPeripheral (string name, Action connectAction, Action serviceAction, Action characteristicAction, Action disconnectAction = null) { + #if !UNITY_EDITOR_OSX || !EXPERIMENTAL_MACOS_EDITOR if (!Application.isEditor) { @@ -614,7 +616,8 @@ public class BluetoothLEHardwareInterface bluetoothDeviceScript.ConnectedPeripheralAction = connectAction; bluetoothDeviceScript.DiscoveredServiceAction = serviceAction; bluetoothDeviceScript.DiscoveredCharacteristicAction = characteristicAction; - bluetoothDeviceScript.ConnectedDisconnectPeripheralAction = disconnectAction; + //bluetoothDeviceScript.ConnectedDisconnectPeripheralAction = disconnectAction; + bluetoothDeviceScript.DeviceBleStatusDisconnectedAction = disconnectAction; } #if EXPERIMENTAL_MACOS_EDITOR && (UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX) diff --git a/Assets/Plugins/iOS/UnityBluetoothLE.h.meta b/Assets/Plugins/iOS/UnityBluetoothLE.h.meta index 4bab6c82..4702e528 100644 --- a/Assets/Plugins/iOS/UnityBluetoothLE.h.meta +++ b/Assets/Plugins/iOS/UnityBluetoothLE.h.meta @@ -12,19 +12,19 @@ PluginImporter: validateReferences: 1 platformData: - first: - '': Linux + : Linux second: enabled: 0 settings: CPU: x86 - first: - '': OSXIntel + : OSXIntel second: enabled: 0 settings: CPU: AnyCPU - first: - '': OSXIntel64 + : OSXIntel64 second: enabled: 0 settings: diff --git a/Assets/Plugins/iOS/libWeChatSDK.a.meta b/Assets/Plugins/iOS/libWeChatSDK.a.meta index 4514869d..db5b0492 100644 --- a/Assets/Plugins/iOS/libWeChatSDK.a.meta +++ b/Assets/Plugins/iOS/libWeChatSDK.a.meta @@ -47,7 +47,7 @@ PluginImporter: second: enabled: 0 settings: - CPU: x86_64 + CPU: AnyCPU - first: Standalone: OSXUniversal second: diff --git a/Assets/Scripts/Devices/Ble/BleDevice.cs b/Assets/Scripts/Devices/Ble/BleDevice.cs index beadb367..aea62baa 100644 --- a/Assets/Scripts/Devices/Ble/BleDevice.cs +++ b/Assets/Scripts/Devices/Ble/BleDevice.cs @@ -103,7 +103,6 @@ namespace Assets.Scripts.Devices.Ble hwInterface.DiscoverServices(this.peripheralInfo, ServicesDiscoveredAction);//, this.ServicesDiscoveredAction); return; } - this.State = DeviceState.Disconnected; if (response.Error != null) { diff --git a/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs b/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs index b32faaae..8b12fe79 100644 --- a/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs +++ b/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs @@ -94,7 +94,7 @@ namespace Assets.Scripts.Devices.Ble { return; } - +#if !(UNITY_ANDROID || UNITY_IOS) var device111 = discoveredDevices.Last().Value; if (device111 != null && device111.State == Ant.DeviceState.Disconnected) { @@ -103,8 +103,10 @@ namespace Assets.Scripts.Devices.Ble //TODO:取消注释,自动连接设备 //Debug.Log("自动连接" + device111.Id); device111.Connect(); + Thread.Sleep(2000); } } +#endif //discoveredDevices.Add(device.Peripheral.Address, new BleDevice(device.Peripheral, hwInterface, device.SensorType)); @@ -119,7 +121,7 @@ namespace Assets.Scripts.Devices.Ble public override void StopScan() { - + hwInterface.StopScan(); } public override void Dispose() diff --git a/Assets/Scripts/Devices/Ble/Interfaces/IBleWinHwInterface.cs b/Assets/Scripts/Devices/Ble/Interfaces/IBleWinHwInterface.cs index dc500079..2be4426b 100644 --- a/Assets/Scripts/Devices/Ble/Interfaces/IBleWinHwInterface.cs +++ b/Assets/Scripts/Devices/Ble/Interfaces/IBleWinHwInterface.cs @@ -19,6 +19,7 @@ namespace Assets.Scripts.Devices.Ble.Interfaces void WriteCharacteristic(BleCharacteristicInfo characteristic, byte[] data); void ReadCharacteristic(BleCharacteristicInfo characteristic, CharacteristicReadCallback callback); void Dispose(); + void StopScan(); BleState BleState { get; set; } Dictionary pCache { get; set; } event BluetoothStateChangedCallback BluetoothStateChangedEvent; diff --git a/Assets/Scripts/Devices/Ble/Win/BleWinHwInterface.cs b/Assets/Scripts/Devices/Ble/Win/BleWinHwInterface.cs index 794db60f..a8a4f73e 100644 --- a/Assets/Scripts/Devices/Ble/Win/BleWinHwInterface.cs +++ b/Assets/Scripts/Devices/Ble/Win/BleWinHwInterface.cs @@ -477,6 +477,10 @@ namespace Assets.Scripts.Ble hwInterface = null; pCache.Clear(); } + public void StopScan() + { + + } public void ReadCharacteristic(BleCharacteristicInfo characteristic, CharacteristicReadCallback callback) { diff --git a/Assets/Scripts/Devices/Ble/Win/WclBleMainThread.cs b/Assets/Scripts/Devices/Ble/Win/WclBleMainThread.cs index 78d461e6..c614d4a3 100644 --- a/Assets/Scripts/Devices/Ble/Win/WclBleMainThread.cs +++ b/Assets/Scripts/Devices/Ble/Win/WclBleMainThread.cs @@ -80,7 +80,7 @@ namespace Assets.Scripts.Devices.Ble.Win { this.wclWatcher.StopScan(); } - //this.StopGattThreads(); + this.StopGattThreads(); if (this.wclBleManager.Active) { this.wclBleManager.ManagerStatusChanged = null; @@ -119,6 +119,14 @@ namespace Assets.Scripts.Devices.Ble.Win status, }); } + public void StopGattThreads() + { + Debug.Log("停止thread"); + foreach (var item in this.gattClients.Values) + { + item.Stop(); + } + } public override bool Stop() { diff --git a/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs b/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs index b18fa19f..b447ce4c 100644 --- a/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs +++ b/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs @@ -203,21 +203,6 @@ namespace Assets.Scripts.Ble } } - //private WclBleGattThread SetUpGattClient(BlePeripheralInfo peripheral) - //{ - // WclBleGattThread wclBleGattThread = this.wclBleMainThread.CreateGattThread(peripheral); - // wclBleGattThread.GattConnected += this.GattConnected; - // wclBleGattThread.GattDisconnected += this.GattDisconnected; - // wclBleGattThread.GattServicesDiscovered += this.GattServicesDiscovered; - // wclBleGattThread.GattCharacteristicsDiscovered += this.GattCharacteristicsDiscovered; - // wclBleGattThread.GattCharacteristicSubscribed += this.GattCharacteristicSubscribed; - // wclBleGattThread.GattCharacteristicRead += this.GattCharacteristicRead; - // wclBleGattThread.GattCharacteristicWrote += this.GattCharacteristicWrote; - // wclBleGattThread.GattCharacteristicChanged += this.GattCharacteristicChanged; - // wclBleGattThread.Start(); - // return wclBleGattThread; - //} - List servicelist = new List(); List characteristilist = new List(); public void ConnectPeripheral(BlePeripheralInfo info, Action callback) @@ -230,45 +215,81 @@ namespace Assets.Scripts.Ble { callback?.Invoke(self, info, s); }, (address, service) => { - if (servicesCallbacks.Where(c => c.Key.Address == address).Any()) - { - - var serviceCallback = servicesCallbacks.Where(c => c.Key.Address == address).FirstOrDefault(); - List servicelist = new List(); - servicelist.Add(new WinBleServiceInfo(serviceCallback.Key,new Guid(service))); - BleResponse> response = new BleResponse> - { - IsSuccess = true, - Error = null, - Data = servicelist, - }; - serviceCallback.Value?.Invoke(this, serviceCallback.Key, response); - } - + ServicesDiscovered(address, service); }, (address, service, characteristic) => { - if (characteristicsDiscoveredCallbacks.Where(c => c.Key.Peripheral.Address == address).Any()) - { - var characteristicCallback = characteristicsDiscoveredCallbacks.Where(c => c.Key.Peripheral.Address == address).FirstOrDefault(); - List characteristiclist = new List(); - characteristiclist.Add(new WinBleCharacteristicInfo(new Guid(characteristic), new WinBleServiceInfo(characteristicCallback.Key.Peripheral, new Guid(service)),0)); - BleResponse> response = new BleResponse> - { - IsSuccess = true, - Error = null, - Data = characteristiclist, - }; - characteristicCallback.Value?.Invoke(this, characteristicCallback.Key, response); - } - }, null); + CharacteristicsDiscovered(address, service, characteristic); + }, (address)=> { + PeripheralDisconnected(address, info); + }); } + #region 私有方法 + //服务发现 + private void ServicesDiscovered(string address, string service) + { + if (servicesCallbacks.Where(c => c.Key.Address == address).Any()) + { + + var serviceCallback = servicesCallbacks.Where(c => c.Key.Address == address).FirstOrDefault(); + List servicelist = new List(); + servicelist.Add(new WinBleServiceInfo(serviceCallback.Key, new Guid(service))); + BleResponse> response = new BleResponse> + { + IsSuccess = true, + Error = null, + Data = servicelist, + }; + serviceCallback.Value?.Invoke(this, serviceCallback.Key, response); + } + } + //特征值发现 + private void CharacteristicsDiscovered(string address, string service, string characteristic) + { + if (characteristicsDiscoveredCallbacks.Where(c => c.Key.Peripheral.Address == address).Any()) + { + var characteristicCallback = characteristicsDiscoveredCallbacks.Where(c => c.Key.Peripheral.Address == address).FirstOrDefault(); + List characteristiclist = new List(); + characteristiclist.Add(new WinBleCharacteristicInfo(new Guid(characteristic), new WinBleServiceInfo(characteristicCallback.Key.Peripheral, new Guid(service)), 0)); + BleResponse> response = new BleResponse> + { + IsSuccess = true, + Error = null, + Data = characteristiclist, + }; + characteristicCallback.Value?.Invoke(this, characteristicCallback.Key, response); + } + } + //设备主动断开处理 + private void PeripheralDisconnected(string address, BlePeripheralInfo info) + { + var characteristicCallback = characteristicNotificationCallbacks.Where(c => c.Key.MatchAddress(address)); + if (characteristicCallback.Any()) + { + characteristicNotificationCallbacks.Remove(characteristicCallback.First().Key); + } + + var serviceCallback = servicesCallbacks.Where(c => c.Key.MatchAddress(address)); + if (serviceCallback.Any()) + { + servicesCallbacks.Remove(serviceCallback.First().Key); + } + + var characteristicsDiscoveredCallback = characteristicsDiscoveredCallbacks.Where(c => c.Key.MatchAddress(address)); + if (serviceCallback.Any()) + { + characteristicsDiscoveredCallbacks.Remove(characteristicsDiscoveredCallback.First().Key); + } + + peripheralDisconnectedEvent(this, info, null, false); + } + #endregion //设备断开连接 public void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback) { BluetoothLEHardwareInterface.DisconnectPeripheral(peripheral.Address, (address) => { - //peripheralDisconnectedEvent(this, peripheral, null, true); + peripheralDisconnectedEvent(this, peripheral, null, true); callback?.Invoke(); }); } @@ -289,6 +310,12 @@ namespace Assets.Scripts.Ble _discoveredCallback = discoveredCallBack; bleMobileThread.StartWatcher(); } + + public void StopScan() + { + bleMobileThread.Stop(); + } + //发现服务列表 public void DiscoverServices(BlePeripheralInfo peripheral, Action>> callback) { @@ -332,7 +359,7 @@ namespace Assets.Scripts.Ble bleMobileThread.ManagerInitialized -= BleMobileThread_ManagerInitialized; bleMobileThread.ManagerStatusChanged -= ManagerStatusChanged; bleMobileThread.ScanInfoReceived -= WatcherScanInfoReceived; - bleMobileThread.Stop(); + bleMobileThread.Dispose(); bleMobileThread = null; hwInterface = null; pCache.Clear(); diff --git a/Assets/Scripts/Devices/Ble/mobile/BleMobileThread.cs b/Assets/Scripts/Devices/Ble/mobile/BleMobileThread.cs index 701ee78c..a083fcc7 100644 --- a/Assets/Scripts/Devices/Ble/mobile/BleMobileThread.cs +++ b/Assets/Scripts/Devices/Ble/mobile/BleMobileThread.cs @@ -77,5 +77,10 @@ namespace Assets.Scripts.Devices.Ble { BluetoothLEHardwareInterface.StopScan(); } + public void Dispose() + { + BluetoothLEHardwareInterface.StopScan(); + BluetoothLEHardwareInterface.DisconnectAll(); + } } } diff --git a/Assets/Scripts/Devices/MainDeviceAdapter.cs b/Assets/Scripts/Devices/MainDeviceAdapter.cs index a5f84d70..d3246a5e 100644 --- a/Assets/Scripts/Devices/MainDeviceAdapter.cs +++ b/Assets/Scripts/Devices/MainDeviceAdapter.cs @@ -47,7 +47,10 @@ namespace Assets.Scripts.Devices public void StopScan() { - + adapters.ForEach(item => + { + item.StopScan(); + }); } public DeviceAdapterState GetState(ConnectionInterface connectionInterface) diff --git a/Assets/Scripts/Scenes/MainController.cs b/Assets/Scripts/Scenes/MainController.cs index d2b2276c..d613d25a 100644 --- a/Assets/Scripts/Scenes/MainController.cs +++ b/Assets/Scripts/Scenes/MainController.cs @@ -33,6 +33,7 @@ public class MainController : BaseScene //transform.Find("ModalPanel").GetComponent().sizeDelta = new Vector2(844, 390); #endif App.MainDeviceAdapter.StartScan(); +#endif } // Start is called before the first frame update diff --git a/Assets/Scripts/UI/Prefab/Panel/DeviceController.cs b/Assets/Scripts/UI/Prefab/Panel/DeviceController.cs index 20113156..30496149 100644 --- a/Assets/Scripts/UI/Prefab/Panel/DeviceController.cs +++ b/Assets/Scripts/UI/Prefab/Panel/DeviceController.cs @@ -90,7 +90,6 @@ public class DeviceController : PFUIPanel protected override void Awake() { base.Awake(); - //Debug.Log("device awake"); } @@ -109,6 +108,7 @@ public class DeviceController : PFUIPanel { //UIManager.ShowHomePanel(); //UIManager.CloseModal(); + App.MainDeviceAdapter.StopScan(); this.Close(); })); //UIManager.AddEvent(mReturnBtn.gameObject, EventTriggerType.PointerUp, (e) => @@ -191,7 +191,9 @@ public class DeviceController : PFUIPanel cg.alpha = 0; base.Show(); App.MainDeviceAdapter.ClearDevice(); - +#if UNITY_ANDROID || UNITY_IOS + App.MainDeviceAdapter.StartScan(); +#endif cg.DOFade(1f, 0.3f); } diff --git a/Assets/Shatalmic/plugin.unitypackage.meta b/Assets/Shatalmic/plugin.unitypackage.meta new file mode 100644 index 00000000..c11cbc4e --- /dev/null +++ b/Assets/Shatalmic/plugin.unitypackage.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 1a70f37e2f13e4df0ac477f3c3eb451b +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: