Merge remote-tracking branch 'origin/dev_mobile_ble' into dev_mobile

# Conflicts:
#	Assets/Scripts/Scenes/MainController.cs
This commit is contained in:
CaiYanPeng 2021-08-27 10:13:17 +08:00
commit 85130ce6c3
16 changed files with 135 additions and 69 deletions

View File

@ -23,8 +23,9 @@ public class BluetoothDeviceScript : MonoBehaviour
public Action<string, string> RetrievedConnectedPeripheralAction;
public Action<string, byte[]> PeripheralReceivedWriteDataAction;
public Action<string> ConnectedPeripheralAction;
public Action<string> ConnectedDisconnectPeripheralAction;
public Action<string> ConnectedDisconnectPeripheralAction { get; set; }
public Action<string> DisconnectedPeripheralAction;
public Action<string> DeviceBleStatusDisconnectedAction;
public Action<string, string> DiscoveredServiceAction;
public Action<string, string, string> DiscoveredCharacteristicAction;
public Action<string> 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)
{

View File

@ -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<string> connectAction, Action<string, string> serviceAction, Action<string, string, string> characteristicAction, Action<string> 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)

View File

@ -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:

View File

@ -47,7 +47,7 @@ PluginImporter:
second:
enabled: 0
settings:
CPU: x86_64
CPU: AnyCPU
- first:
Standalone: OSXUniversal
second:

View File

@ -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)
{

View File

@ -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()

View File

@ -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<string, BleAdvertisementInfo> pCache { get; set; }
event BluetoothStateChangedCallback BluetoothStateChangedEvent;

View File

@ -477,6 +477,10 @@ namespace Assets.Scripts.Ble
hwInterface = null;
pCache.Clear();
}
public void StopScan()
{
}
public void ReadCharacteristic(BleCharacteristicInfo characteristic, CharacteristicReadCallback callback)
{

View File

@ -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()
{

View File

@ -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<BleServiceInfo> servicelist = new List<BleServiceInfo>();
List<BleCharacteristicInfo> characteristilist = new List<BleCharacteristicInfo>();
public void ConnectPeripheral(BlePeripheralInfo info, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse> 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<BleServiceInfo> servicelist = new List<BleServiceInfo>();
servicelist.Add(new WinBleServiceInfo(serviceCallback.Key,new Guid(service)));
BleResponse<List<BleServiceInfo>> response = new BleResponse<List<BleServiceInfo>>
{
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<BleCharacteristicInfo> characteristiclist = new List<BleCharacteristicInfo>();
characteristiclist.Add(new WinBleCharacteristicInfo(new Guid(characteristic), new WinBleServiceInfo(characteristicCallback.Key.Peripheral, new Guid(service)),0));
BleResponse<List<BleCharacteristicInfo>> response = new BleResponse<List<BleCharacteristicInfo>>
{
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<BleServiceInfo> servicelist = new List<BleServiceInfo>();
servicelist.Add(new WinBleServiceInfo(serviceCallback.Key, new Guid(service)));
BleResponse<List<BleServiceInfo>> response = new BleResponse<List<BleServiceInfo>>
{
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<BleCharacteristicInfo> characteristiclist = new List<BleCharacteristicInfo>();
characteristiclist.Add(new WinBleCharacteristicInfo(new Guid(characteristic), new WinBleServiceInfo(characteristicCallback.Key.Peripheral, new Guid(service)), 0));
BleResponse<List<BleCharacteristicInfo>> response = new BleResponse<List<BleCharacteristicInfo>>
{
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<IBleWinHwInterface, BlePeripheralInfo, BleResponse<List<BleServiceInfo>>> 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();

View File

@ -77,5 +77,10 @@ namespace Assets.Scripts.Devices.Ble
{
BluetoothLEHardwareInterface.StopScan();
}
public void Dispose()
{
BluetoothLEHardwareInterface.StopScan();
BluetoothLEHardwareInterface.DisconnectAll();
}
}
}

View File

@ -47,7 +47,10 @@ namespace Assets.Scripts.Devices
public void StopScan()
{
adapters.ForEach(item =>
{
item.StopScan();
});
}
public DeviceAdapterState GetState(ConnectionInterface connectionInterface)

View File

@ -33,6 +33,7 @@ public class MainController : BaseScene
//transform.Find("ModalPanel").GetComponent<RectTransform>().sizeDelta = new Vector2(844, 390);
#endif
App.MainDeviceAdapter.StartScan();
#endif
}
// Start is called before the first frame update

View File

@ -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);
}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 1a70f37e2f13e4df0ac477f3c3eb451b
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: