蓝牙停止扫描和一些bug修复
This commit is contained in:
1 parent
0139cbc173
commit
d8927ceca5
16 files changed
+137
-71
No files matched your search
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -477,6 +477,10 @@ namespace Assets.Scripts.Ble
|
||||
hwInterface = null;
|
||||
pCache.Clear();
|
||||
}
|
||||
public void StopScan()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void ReadCharacteristic(BleCharacteristicInfo characteristic, CharacteristicReadCallback callback)
|
||||
{
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -77,5 +77,10 @@ namespace Assets.Scripts.Devices.Ble
|
||||
{
|
||||
BluetoothLEHardwareInterface.StopScan();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
BluetoothLEHardwareInterface.StopScan();
|
||||
BluetoothLEHardwareInterface.DisconnectAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,10 @@ namespace Assets.Scripts.Devices
|
||||
|
||||
public void StopScan()
|
||||
{
|
||||
|
||||
adapters.ForEach(item =>
|
||||
{
|
||||
item.StopScan();
|
||||
});
|
||||
}
|
||||
|
||||
public DeviceAdapterState GetState(ConnectionInterface connectionInterface)
|
||||
|
||||
Reference in new issue
Block a user