67 lines
2.2 KiB
C#
67 lines
2.2 KiB
C#
|
|
using Assets.Scripts.Ble;
|
|||
|
|
using System.Timers;
|
|||
|
|
|
|||
|
|
namespace Assets.Scripts.Devices.Ble
|
|||
|
|
{
|
|||
|
|
public class BleMobileThread
|
|||
|
|
{
|
|||
|
|
public delegate void WclAdvertisementPacketDelegate(BleMobileThread sender, string address, string name, int rssi, string type);
|
|||
|
|
public BleMobileThread.WclAdvertisementPacketDelegate ScanInfoReceived;
|
|||
|
|
private Timer timer { get; set; }
|
|||
|
|
public BleMobileThread() {
|
|||
|
|
//初始蓝牙
|
|||
|
|
BluetoothLEHardwareInterface.Initialize(true, false, () => {
|
|||
|
|
|
|||
|
|
},
|
|||
|
|
(error) => {
|
|||
|
|
|
|||
|
|
BluetoothLEHardwareInterface.Log("Error: " + error);
|
|||
|
|
|
|||
|
|
if (error.Contains("Bluetooth LE Not Enabled"))
|
|||
|
|
BluetoothLEHardwareInterface.BluetoothEnable(true);
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
bool startScan = true;
|
|||
|
|
public void StartWatcher() {
|
|||
|
|
//if (timer == null)
|
|||
|
|
//{
|
|||
|
|
// timer = new Timer();
|
|||
|
|
// timer.Interval = 500;
|
|||
|
|
// timer.Elapsed += Timer_Elapsed;
|
|||
|
|
//}
|
|||
|
|
//if (!timer.Enabled)
|
|||
|
|
//{
|
|||
|
|
// timer.Start();
|
|||
|
|
//} "0000180D-0000-1000-8000-00805F9B34FB"
|
|||
|
|
var ss = new string[] {"00001826-0000-1000-8000-00805F9B34FB","0000180D-0000-1000-8000-00805F9B34FB", "6e40fec1-b5a3-f393-e0a9-e50e24dcca9e" };
|
|||
|
|
BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(ss, (s,a)=> {
|
|||
|
|
BluetoothLEHardwareInterface.Log(s);
|
|||
|
|
}, (address, name, rssi, bytes) =>
|
|||
|
|
{
|
|||
|
|
ScanInfoReceived?.Invoke(this, address, name, rssi,"trainer");
|
|||
|
|
}, true);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void StopWatcher()
|
|||
|
|
{
|
|||
|
|
if (timer.Enabled)
|
|||
|
|
{
|
|||
|
|
timer.Stop();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
|
|||
|
|
{
|
|||
|
|
//if (startScan)
|
|||
|
|
//{
|
|||
|
|
// startScan = false;
|
|||
|
|
// BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, null, null,(address, name, rssi, bytes) =>
|
|||
|
|
// {
|
|||
|
|
|
|||
|
|
// ScanInfoReceived?.Invoke(this, address, name, rssi);
|
|||
|
|
// }, true);
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|