2021-08-20 18:45:16 +08:00
|
|
|
|
using Assets.Scripts.Ble;
|
|
|
|
|
|
using System.Timers;
|
2022-06-14 18:37:58 +08:00
|
|
|
|
using UnityEngine;
|
2021-08-20 18:45:16 +08:00
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-06-14 18:37:58 +08:00
|
|
|
|
WclBleManagerStatus statusEnum = WclBleManagerStatus.RadioOn;
|
2021-08-24 18:34:41 +08:00
|
|
|
|
internal BleMobileThread() {
|
2022-06-10 19:31:07 +08:00
|
|
|
|
Initialize();//初始蓝牙
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void StartWatcher() {
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
if (statusEnum == WclBleManagerStatus.RadioOff)
|
|
|
|
|
|
{
|
2022-06-14 18:37:58 +08:00
|
|
|
|
//Initialize();
|
|
|
|
|
|
BluetoothLEHardwareInterface.BluetoothEnable(true);
|
2022-06-10 19:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(ServiceUuids.GetServiceUuidList().ToArray(), null, (address, name, rssi, bytes) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ScanInfoReceived?.Invoke(self, address, name, rssi, bytes);
|
|
|
|
|
|
}, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public void Initialize()
|
|
|
|
|
|
{
|
2022-06-14 18:37:58 +08:00
|
|
|
|
Debug.Log("Initialize");
|
2021-08-24 18:34:41 +08:00
|
|
|
|
var self = this;
|
2022-06-10 19:31:07 +08:00
|
|
|
|
BluetoothLEHardwareInterface.BluetoothEnable(true);
|
|
|
|
|
|
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);
|
2022-06-10 19:31:07 +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
|
|
|
|
},
|
2022-06-10 19:31:07 +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);
|
2022-06-10 19:31:07 +08:00
|
|
|
|
}, (status) =>
|
|
|
|
|
|
{
|
2021-09-13 18:21:43 +08:00
|
|
|
|
statusEnum = WclBleManagerStatus.RadioOn;
|
2021-08-24 18:34:41 +08:00
|
|
|
|
switch (status)
|
|
|
|
|
|
{
|
2022-06-10 19:31:07 +08:00
|
|
|
|
case 10:
|
|
|
|
|
|
statusEnum = WclBleManagerStatus.RadioOff;
|
|
|
|
|
|
break;
|
2021-08-24 18:34:41 +08:00
|
|
|
|
case 13:
|
|
|
|
|
|
statusEnum = WclBleManagerStatus.RadioOff;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 11:
|
2022-06-10 19:31:07 +08:00
|
|
|
|
statusEnum = WclBleManagerStatus.RadioOff;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 12:
|
2021-08-24 18:34:41 +08:00
|
|
|
|
statusEnum = WclBleManagerStatus.RadioOn;
|
|
|
|
|
|
break;
|
2022-06-10 19:31:07 +08:00
|
|
|
|
default:
|
|
|
|
|
|
statusEnum = WclBleManagerStatus.RadioOff;
|
|
|
|
|
|
break;
|
2021-08-24 18:34:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (managerStatusChanged != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
managerStatusChanged.Invoke(self, statusEnum);
|
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|