171 lines
5.2 KiB
C#
171 lines
5.2 KiB
C#
using Assets.Scripts.Ble;
|
|
using Assets.Scripts.Ble.CPPBridge;
|
|
using Assets.Scripts.Devices.Ble;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace Assets.Scripts.Devices.Ble.Win
|
|
{
|
|
internal class WclBleMainThread
|
|
{
|
|
internal delegate void ManagerStatusChangedCallback(WclBleMainThread thread, WclBleManagerStatus status);
|
|
|
|
private WclBleMainThread.ManagerStatusChangedCallback managerStatusChanged;
|
|
public event WclBleMainThread.ManagerStatusChangedCallback ManagerStatusChanged
|
|
{
|
|
add
|
|
{
|
|
this.managerStatusChanged += value;
|
|
}
|
|
remove
|
|
{
|
|
this.managerStatusChanged -= value;
|
|
}
|
|
}
|
|
|
|
private WclBleManager wclBleManager;
|
|
private WclBleWatcher wclWatcher;
|
|
private readonly object locker = new object();
|
|
private readonly Dictionary<BlePeripheralInfo, WclBleGattThread> gattClients = new Dictionary<BlePeripheralInfo, WclBleGattThread>();
|
|
internal WclBleMainThread()
|
|
{
|
|
|
|
}
|
|
private Thread wThread;
|
|
|
|
public bool Start()
|
|
{
|
|
//this.wThread = new Thread(new ThreadStart(this.SetUpWorkerThread));
|
|
//this.wThread.Start();
|
|
//Task.Run(() =>
|
|
//{
|
|
// this.SetUpWorkerThread();
|
|
//});
|
|
|
|
SetUpWorkerThread();
|
|
return true;
|
|
}
|
|
|
|
private void SetUpWorkerThread()
|
|
{
|
|
wclBleManager = new WclBleManager
|
|
{
|
|
ManagerStatusChanged = new WclBleManager.ManagerStatusChangedCallback(this.OnManagerStatusChanged)
|
|
|
|
};
|
|
var num = wclBleManager.Open();
|
|
if (num != 0)
|
|
{
|
|
Debug.LogWarning(string.Format("WCL BLE open failed: 0x{0:X8}", num));
|
|
}
|
|
|
|
wclWatcher = new WclBleWatcher
|
|
{
|
|
InfoPacketReceived = new WclBleWatcher.InfoPacketReceivedEvent(OnInfoPacketReceived),
|
|
UuidPacketReceived = new WclBleWatcher.UuidPacketReceivedEvent(OnUuidPacketReceived)
|
|
};
|
|
//Debug.Log("bbb");
|
|
}
|
|
|
|
private void OnManagerStatusChanged(WclBleManager manager, WclBleManagerStatus status)
|
|
{
|
|
//Debug.Log(status);
|
|
//WclBleMainThread.SafeInvoke(this.managerStatusChanged, this, status);
|
|
this.managerStatusChanged(this, status);
|
|
}
|
|
|
|
private void OnInfoPacketReceived(object sender, long address, long timestamp, sbyte rssi, string name, WclBleAdvertisementType packetType, byte flags)
|
|
{
|
|
this.scanInfoReceived(this, address, name, rssi, packetType, null);
|
|
}
|
|
|
|
// Token: 0x060020E7 RID: 8423 RVA: 0x000889ED File Offset: 0x00086BED
|
|
private void OnUuidPacketReceived(object sender, long address, long timestamp, sbyte rssi, Guid uuid)
|
|
{
|
|
this.scanInfoReceived(this, address, null, rssi, WclBleAdvertisementType.Unknown, new Guid?(uuid));
|
|
}
|
|
|
|
private static void SafeInvoke(WclBleMainThread.ManagerStatusChangedCallback callback, WclBleMainThread thread, WclBleManagerStatus status)
|
|
{
|
|
callback.Method.Invoke(callback.Target, new object[] {
|
|
thread,
|
|
status,
|
|
});
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
Debug.Log("停止thread");
|
|
foreach (var item in this.gattClients.Values)
|
|
{
|
|
item.Stop();
|
|
}
|
|
|
|
wclBleManager.Dispose();
|
|
}
|
|
|
|
public string Test()
|
|
{
|
|
return wclBleManager.Test();
|
|
}
|
|
|
|
public WclBleGattThread GetGattThread(BlePeripheralInfo info)
|
|
{
|
|
lock (locker)
|
|
{
|
|
if (this.gattClients.ContainsKey(info))
|
|
{
|
|
return this.gattClients[info];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public bool StartWatcher()
|
|
{
|
|
this.wclWatcher.StartScan(this.wclBleManager.Radio);
|
|
return true;
|
|
}
|
|
|
|
|
|
public WclBleGattThread CreateGattThread(BlePeripheralInfo info)
|
|
{
|
|
lock (locker)
|
|
{
|
|
if (gattClients.ContainsKey(info))
|
|
{
|
|
throw new ArgumentException("gatt thread 已经创建");
|
|
}
|
|
|
|
WclBleGattThread wclBleGattThread = new WclBleGattThread(info, this.wclBleManager.Radio);
|
|
this.gattClients[info] = wclBleGattThread;
|
|
|
|
return wclBleGattThread;
|
|
}
|
|
}
|
|
|
|
public event WclBleMainThread.WclAdvertisementPacketDelegate ScanInfoReceived
|
|
{
|
|
add
|
|
{
|
|
this.scanInfoReceived += value;
|
|
}
|
|
remove
|
|
{
|
|
this.scanInfoReceived -= value;
|
|
}
|
|
}
|
|
|
|
private WclBleMainThread.WclAdvertisementPacketDelegate scanInfoReceived;
|
|
|
|
public delegate void WclAdvertisementPacketDelegate(WclBleMainThread sender, long address, string name, int rssi, WclBleAdvertisementType packetType, Guid? service);
|
|
|
|
}
|
|
}
|