移动端蓝牙设备正常骑行

This commit is contained in:
lishuo committed 2021-08-24 18:34:41 +08:00
1 parent d6c3ea2007
commit 3de8b1d555
7 files changed
+226 -350

No files matched your search

@@ -3,64 +3,78 @@ using System.Timers;
namespace Assets.Scripts.Devices.Ble
{
public class BleMobileThread
internal 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()
public delegate void WclInitializedDelegate(BleMobileThread sender);
private BleMobileThread.WclInitializedDelegate managerInitialized;
public event BleMobileThread.WclInitializedDelegate ManagerInitialized
{
if (timer.Enabled)
add
{
timer.Stop();
this.managerInitialized += value;
}
remove
{
this.managerInitialized -= value;
}
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
public delegate void WclAdvertisementPacketDelegate(BleMobileThread sender, string address, string name, int rssi, string[] uuids);
public BleMobileThread.WclAdvertisementPacketDelegate ScanInfoReceived;
internal delegate void ManagerStatusChangedCallback(BleMobileThread thread, WclBleManagerStatus status);
private BleMobileThread.ManagerStatusChangedCallback managerStatusChanged;
public event BleMobileThread.ManagerStatusChangedCallback ManagerStatusChanged
{
//if (startScan)
//{
// startScan = false;
// BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, null, null,(address, name, rssi, bytes) =>
// {
add
{
this.managerStatusChanged += value;
}
remove
{
this.managerStatusChanged -= value;
}
}
// ScanInfoReceived?.Invoke(this, address, name, rssi);
// }, true);
//}
}
internal BleMobileThread() {
var self = this;
//初始蓝牙
BluetoothLEHardwareInterface.Initialize(true, false, () => {
managerInitialized?.Invoke(self);
},
(error) => {
BluetoothLEHardwareInterface.Log("Error: " + error);
if (error.Contains("Bluetooth LE Not Enabled"))
BluetoothLEHardwareInterface.BluetoothEnable(true);
}, (status) => {
var statusEnum = WclBleManagerStatus.RadioOn;
switch (status)
{
case 13:
statusEnum = WclBleManagerStatus.RadioOff;
break;
case 11:
statusEnum = WclBleManagerStatus.RadioOn;
break;
}
if (managerStatusChanged != null)
{
managerStatusChanged.Invoke(self, statusEnum);
}
});
}
public void StartWatcher() {
BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, null, (address, name, rssi, bytes) =>
{
ScanInfoReceived?.Invoke(this, address, name, rssi, bytes);
}, true);
}
public void Stop()
{
BluetoothLEHardwareInterface.StopScan();
}
}
}