蓝牙停止扫描和一些bug修复

This commit is contained in:
lishuo committed 2021-08-26 19:33:43 +08:00
1 parent 0139cbc173
commit d8927ceca5
16 files changed
+137 -71

No files matched your search

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