128 lines
4.7 KiB
C#
Raw Normal View History

2021-08-20 18:45:16 +08:00
using Assets.Scripts.Ble;
using System.Timers;
namespace Assets.Scripts.Devices.Ble
{
2021-08-24 18:34:41 +08:00
internal class BleMobileThread
2021-08-20 18:45:16 +08:00
{
2021-08-24 18:34:41 +08:00
public delegate void WclInitializedDelegate(BleMobileThread sender);
private BleMobileThread.WclInitializedDelegate managerInitialized;
public event BleMobileThread.WclInitializedDelegate ManagerInitialized
{
add
{
this.managerInitialized += value;
}
remove
{
this.managerInitialized -= value;
}
}
public delegate void WclAdvertisementPacketDelegate(BleMobileThread sender, string address, string name, int rssi, string[] uuids);
2021-08-20 18:45:16 +08:00
public BleMobileThread.WclAdvertisementPacketDelegate ScanInfoReceived;
2021-08-24 18:34:41 +08:00
internal delegate void ManagerStatusChangedCallback(BleMobileThread thread, WclBleManagerStatus status);
private BleMobileThread.ManagerStatusChangedCallback managerStatusChanged;
public event BleMobileThread.ManagerStatusChangedCallback ManagerStatusChanged
{
add
{
this.managerStatusChanged += value;
}
remove
{
this.managerStatusChanged -= value;
}
}
2021-09-13 18:21:43 +08:00
WclBleManagerStatus statusEnum = WclBleManagerStatus.RadioOff;
2021-08-24 18:34:41 +08:00
internal BleMobileThread() {
var self = this;
2021-08-20 18:45:16 +08:00
//初始蓝牙
BluetoothLEHardwareInterface.Initialize(true, false, () => {
2021-09-13 18:21:43 +08:00
statusEnum = WclBleManagerStatus.RadioOn;
2021-08-24 18:34:41 +08:00
managerInitialized?.Invoke(self);
2021-08-20 18:45:16 +08:00
},
(error) => {
2021-09-13 18:21:43 +08:00
statusEnum = WclBleManagerStatus.RadioOff;
2021-08-20 18:45:16 +08:00
BluetoothLEHardwareInterface.Log("Error: " + error);
if (error.Contains("Bluetooth LE Not Enabled"))
BluetoothLEHardwareInterface.BluetoothEnable(true);
2021-08-24 18:34:41 +08:00
}, (status) => {
2021-09-13 18:21:43 +08:00
statusEnum = WclBleManagerStatus.RadioOn;
2021-08-24 18:34:41 +08:00
switch (status)
{
case 13:
statusEnum = WclBleManagerStatus.RadioOff;
break;
case 11:
statusEnum = WclBleManagerStatus.RadioOn;
break;
}
if (managerStatusChanged != null)
{
managerStatusChanged.Invoke(self, statusEnum);
}
2021-08-20 18:45:16 +08:00
});
}
2021-08-24 18:34:41 +08:00
2021-08-20 18:45:16 +08:00
public void StartWatcher() {
2021-09-13 18:21:43 +08:00
var self = this;
if (statusEnum == WclBleManagerStatus.RadioOff)
{
BluetoothLEHardwareInterface.BluetoothEnable(true);
BluetoothLEHardwareInterface.Initialize(true, false, () =>
{
managerInitialized?.Invoke(self);
BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(ServiceUuids.GetServiceUuidList().ToArray(), null, (address, name, rssi, bytes) =>
{
ScanInfoReceived?.Invoke(self, address, name, rssi, bytes);
}, true);
},
(error) =>
{
statusEnum = WclBleManagerStatus.RadioOff;
BluetoothLEHardwareInterface.Log("Error: " + error);
if (error.Contains("Bluetooth LE Not Enabled"))
BluetoothLEHardwareInterface.BluetoothEnable(true);
}, (status) =>
{
statusEnum = WclBleManagerStatus.RadioOn;
switch (status)
{
case 13:
statusEnum = WclBleManagerStatus.RadioOff;
break;
case 11:
statusEnum = WclBleManagerStatus.RadioOn;
break;
}
if (managerStatusChanged != null)
{
managerStatusChanged.Invoke(self, statusEnum);
}
});
}
else
2021-08-20 18:45:16 +08:00
{
2021-09-13 18:21:43 +08:00
BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(ServiceUuids.GetServiceUuidList().ToArray(), null, (address, name, rssi, bytes) =>
{
ScanInfoReceived?.Invoke(self, address, name, rssi, bytes);
}, true);
}
2021-08-20 18:45:16 +08:00
}
2021-08-24 18:34:41 +08:00
public void Stop()
2021-08-20 18:45:16 +08:00
{
2021-08-24 18:34:41 +08:00
BluetoothLEHardwareInterface.StopScan();
2021-08-20 18:45:16 +08:00
}
2021-08-26 19:33:43 +08:00
public void Dispose()
{
BluetoothLEHardwareInterface.StopScan();
BluetoothLEHardwareInterface.DisconnectAll();
}
2021-08-20 18:45:16 +08:00
}
}