移动端蓝牙设备正常骑行
This commit is contained in:
parent
d6c3ea2007
commit
3de8b1d555
Binary file not shown.
@ -10,14 +10,15 @@ public class BluetoothDeviceScript : MonoBehaviour
|
|||||||
|
|
||||||
public List<string> DiscoveredDeviceList;
|
public List<string> DiscoveredDeviceList;
|
||||||
|
|
||||||
public Action InitializedAction;
|
public Action InitializedAction { get; set; }
|
||||||
public Action DeinitializedAction;
|
public Action DeinitializedAction;
|
||||||
public Action<string> ErrorAction;
|
public Action<string> ErrorAction;
|
||||||
public Action<string> ServiceAddedAction;
|
public Action<string> ServiceAddedAction;
|
||||||
|
public Action<int> BleStatusChangedAction;
|
||||||
public Action StartedAdvertisingAction;
|
public Action StartedAdvertisingAction;
|
||||||
public Action StoppedAdvertisingAction;
|
public Action StoppedAdvertisingAction;
|
||||||
public Action<string, string> DiscoveredPeripheralAction;
|
public Action<string, string> DiscoveredPeripheralAction;
|
||||||
public Action<string, string, int, byte[]> DiscoveredPeripheralWithAdvertisingInfoAction;
|
public Action<string, string, int, string[]> DiscoveredPeripheralWithAdvertisingInfoAction;
|
||||||
public Action<BluetoothLEHardwareInterface.iBeaconData> DiscoveredBeaconAction;
|
public Action<BluetoothLEHardwareInterface.iBeaconData> DiscoveredBeaconAction;
|
||||||
public Action<string, string> RetrievedConnectedPeripheralAction;
|
public Action<string, string> RetrievedConnectedPeripheralAction;
|
||||||
public Action<string, byte[]> PeripheralReceivedWriteDataAction;
|
public Action<string, byte[]> PeripheralReceivedWriteDataAction;
|
||||||
@ -71,6 +72,7 @@ public class BluetoothDeviceScript : MonoBehaviour
|
|||||||
const string deviceDidUpdateValueForCharacteristic = "DidUpdateValueForCharacteristic";
|
const string deviceDidUpdateValueForCharacteristic = "DidUpdateValueForCharacteristic";
|
||||||
const string deviceLog = "Log";
|
const string deviceLog = "Log";
|
||||||
const string deviceRequestMtu = "MtuChanged";
|
const string deviceRequestMtu = "MtuChanged";
|
||||||
|
const string bleStatusChanged = "BleStatusChanged";
|
||||||
|
|
||||||
public void OnBluetoothMessage (string message)
|
public void OnBluetoothMessage (string message)
|
||||||
{
|
{
|
||||||
@ -78,7 +80,17 @@ public class BluetoothDeviceScript : MonoBehaviour
|
|||||||
{
|
{
|
||||||
char[] delim = new char[] { '~' };
|
char[] delim = new char[] { '~' };
|
||||||
string[] parts = message.Split (delim);
|
string[] parts = message.Split (delim);
|
||||||
|
if (parts[0].Equals("DeviceStateChanged"))
|
||||||
|
{
|
||||||
|
//throw new Exception("test");
|
||||||
|
}
|
||||||
|
if (parts[0].Equals(bleStatusChanged))
|
||||||
|
{
|
||||||
|
if (BleStatusChangedAction != null)
|
||||||
|
{
|
||||||
|
BleStatusChangedAction(int.Parse(parts[1]));
|
||||||
|
}
|
||||||
|
}
|
||||||
for (int i = 0; i < parts.Length; ++i)
|
for (int i = 0; i < parts.Length; ++i)
|
||||||
BluetoothLEHardwareInterface.Log(string.Format("Part: {0} - {1}", i, parts[i]));
|
BluetoothLEHardwareInterface.Log(string.Format("Part: {0} - {1}", i, parts[i]));
|
||||||
|
|
||||||
@ -152,11 +164,22 @@ public class BluetoothDeviceScript : MonoBehaviour
|
|||||||
int rssi = 0;
|
int rssi = 0;
|
||||||
if (!int.TryParse (parts[3], out rssi))
|
if (!int.TryParse (parts[3], out rssi))
|
||||||
rssi = 0;
|
rssi = 0;
|
||||||
|
List<string> uuidList = new List<string>();
|
||||||
|
if (parts[4].Contains(","))
|
||||||
|
{
|
||||||
|
string[] s = parts[4].Split(',');
|
||||||
|
foreach (var o in s)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(o))
|
||||||
|
{
|
||||||
|
uuidList.Add(o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//// parse the base 64 encoded data that is the 5th value
|
||||||
|
//byte[] bytes = System.Convert.FromBase64String (parts[4]);
|
||||||
|
|
||||||
// parse the base 64 encoded data that is the 5th value
|
DiscoveredPeripheralWithAdvertisingInfoAction (parts[1], parts[2], rssi, uuidList.ToArray());
|
||||||
byte[] bytes = System.Convert.FromBase64String (parts[4]);
|
|
||||||
|
|
||||||
DiscoveredPeripheralWithAdvertisingInfoAction (parts[1], parts[2], rssi, bytes);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -266,7 +266,7 @@ public class BluetoothLEHardwareInterface
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BluetoothDeviceScript Initialize (bool asCentral, bool asPeripheral, Action action, Action<string> errorAction)
|
public static BluetoothDeviceScript Initialize (bool asCentral, bool asPeripheral, Action action, Action<string> errorAction, Action<int> statusAction)
|
||||||
{
|
{
|
||||||
bluetoothDeviceScript = null;
|
bluetoothDeviceScript = null;
|
||||||
|
|
||||||
@ -289,6 +289,7 @@ public class BluetoothLEHardwareInterface
|
|||||||
|
|
||||||
if (bluetoothDeviceScript != null)
|
if (bluetoothDeviceScript != null)
|
||||||
{
|
{
|
||||||
|
bluetoothDeviceScript.BleStatusChangedAction = statusAction;
|
||||||
bluetoothDeviceScript.InitializedAction = action;
|
bluetoothDeviceScript.InitializedAction = action;
|
||||||
bluetoothDeviceScript.ErrorAction = errorAction;
|
bluetoothDeviceScript.ErrorAction = errorAction;
|
||||||
}
|
}
|
||||||
@ -475,7 +476,7 @@ public class BluetoothLEHardwareInterface
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ScanForPeripheralsWithServices (string[] serviceUUIDs, Action<string, string> action = null, Action<string, string, int, byte[]> actionAdvertisingInfo = null, bool rssiOnly = false, bool clearPeripheralList = true, int recordType = 0xFF)
|
public static void ScanForPeripheralsWithServices (string[] serviceUUIDs, Action<string, string> action = null, Action<string, string, int, string[]> actionAdvertisingInfo = null, bool rssiOnly = false, bool clearPeripheralList = true, int recordType = 0xFF)
|
||||||
{
|
{
|
||||||
#if !UNITY_EDITOR_OSX || !EXPERIMENTAL_MACOS_EDITOR
|
#if !UNITY_EDITOR_OSX || !EXPERIMENTAL_MACOS_EDITOR
|
||||||
if (!Application.isEditor)
|
if (!Application.isEditor)
|
||||||
|
|||||||
@ -123,7 +123,7 @@ namespace Assets.Scripts.Devices.Ble
|
|||||||
Debug.Log("断开设备" + this.Name);
|
Debug.Log("断开设备" + this.Name);
|
||||||
|
|
||||||
//App.MainDeviceAdapter.PrintStatus();
|
//App.MainDeviceAdapter.PrintStatus();
|
||||||
|
this.State = DeviceState.Disconnected;
|
||||||
|
|
||||||
this.hwInterface.DisconnectPeripheral(this.peripheralInfo, () => {
|
this.hwInterface.DisconnectPeripheral(this.peripheralInfo, () => {
|
||||||
//App.MainDeviceAdapter.PrintStatus();
|
//App.MainDeviceAdapter.PrintStatus();
|
||||||
|
|||||||
@ -13,7 +13,7 @@ using UnityEngine;
|
|||||||
|
|
||||||
namespace Assets.Scripts.Ble
|
namespace Assets.Scripts.Ble
|
||||||
{
|
{
|
||||||
public sealed class BleMobileInterface: IBleWinHwInterface
|
public sealed class BleMobileInterface : IBleWinHwInterface
|
||||||
{
|
{
|
||||||
private static BleMobileInterface hwInterface;
|
private static BleMobileInterface hwInterface;
|
||||||
private BleMobileThread bleMobileThread;
|
private BleMobileThread bleMobileThread;
|
||||||
@ -68,7 +68,7 @@ namespace Assets.Scripts.Ble
|
|||||||
this.bluetoothStateChanged -= value;
|
this.bluetoothStateChanged -= value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private BleState nativeState;
|
private BleState nativeState;
|
||||||
public BleState BleState
|
public BleState BleState
|
||||||
{
|
{
|
||||||
@ -78,7 +78,7 @@ namespace Assets.Scripts.Ble
|
|||||||
}
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if(this.nativeState != value)
|
if (this.nativeState != value)
|
||||||
{
|
{
|
||||||
this.nativeState = value;
|
this.nativeState = value;
|
||||||
this.bluetoothStateChanged?.Invoke(this, this.nativeState);
|
this.bluetoothStateChanged?.Invoke(this, this.nativeState);
|
||||||
@ -102,58 +102,61 @@ namespace Assets.Scripts.Ble
|
|||||||
private BleMobileInterface()
|
private BleMobileInterface()
|
||||||
{
|
{
|
||||||
bleMobileThread = new BleMobileThread();
|
bleMobileThread = new BleMobileThread();
|
||||||
|
bleMobileThread.ManagerInitialized += BleMobileThread_ManagerInitialized;
|
||||||
|
bleMobileThread.ManagerStatusChanged += ManagerStatusChanged;
|
||||||
bleMobileThread.ScanInfoReceived += WatcherScanInfoReceived;
|
bleMobileThread.ScanInfoReceived += WatcherScanInfoReceived;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WatcherScanInfoReceived(BleMobileThread sender, string address, string name, int rssi,string type)
|
private void BleMobileThread_ManagerInitialized(BleMobileThread thread)
|
||||||
{
|
{
|
||||||
//if (address != 224160707349234)
|
this.BleState = BleState.On;
|
||||||
//{
|
}
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
private void ManagerStatusChanged(BleMobileThread sender, WclBleManagerStatus status)
|
||||||
|
{
|
||||||
|
this.BleState = BleMobileInterface.StateFromNativeState(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WatcherScanInfoReceived(BleMobileThread sender, string address, string name, int rssi,string[] uuids)
|
||||||
|
{
|
||||||
SensorType sensor = SensorType.None;
|
SensorType sensor = SensorType.None;
|
||||||
List<Guid> services = new List<Guid>();
|
List<Guid> services = new List<Guid>();
|
||||||
if (type.Equals("trainer"))
|
|
||||||
|
if (uuids != null && uuids.Length > 0)
|
||||||
{
|
{
|
||||||
sensor = SensorType.Trainer;
|
foreach (var item in uuids)
|
||||||
}
|
{
|
||||||
else
|
services.Add(new Guid(item));
|
||||||
{
|
}
|
||||||
sensor = SensorType.HeartRate;
|
foreach (var item in ServiceUuids.Services)
|
||||||
}
|
{
|
||||||
//if (service != null)
|
if (!uuids.Contains(item.IdGuid.ToString()))
|
||||||
//{
|
{
|
||||||
// services = new List<Guid> { service.Value };
|
continue;
|
||||||
// foreach (var item in ServiceUuids.Services)
|
}
|
||||||
// {
|
if (item.IdByteArray == ServiceUuids.Ftms)
|
||||||
// if (item.IdGuid != service.Value)
|
{
|
||||||
// {
|
sensor = SensorType.Trainer;
|
||||||
// continue;
|
}
|
||||||
// }
|
else if (item.IdByteArray == ServiceUuids.HeartRate)
|
||||||
// if (item.IdByteArray == ServiceUuids.Ftms)
|
{
|
||||||
// {
|
sensor = SensorType.HeartRate;
|
||||||
// sensor = SensorType.Trainer;
|
}
|
||||||
// }
|
else if (item.IdByteArray == ServiceUuids.CyclingPower)
|
||||||
// else if (item.IdByteArray == ServiceUuids.HeartRate)
|
{
|
||||||
// {
|
sensor = SensorType.Power;
|
||||||
// sensor = SensorType.HeartRate;
|
//sensor = SensorType.Trainer;
|
||||||
// }
|
}
|
||||||
// else if (item.IdByteArray == ServiceUuids.CyclingPower)
|
else if (item.IdByteArray == ServiceUuids.CyclingSpeedCadence)
|
||||||
// {
|
{
|
||||||
// sensor = SensorType.Power;
|
sensor = SensorType.SpeedCadence;
|
||||||
// //sensor = SensorType.Trainer;
|
}
|
||||||
// }
|
else if (item.IdByteArray == ServiceUuids.TacxBle)
|
||||||
// else if (item.IdByteArray == ServiceUuids.CyclingSpeedCadence)
|
{
|
||||||
// {
|
sensor = SensorType.Trainer;
|
||||||
// sensor = SensorType.SpeedCadence;
|
}
|
||||||
// }
|
}
|
||||||
// else if (item.IdByteArray == ServiceUuids.TacxBle)
|
};
|
||||||
// {
|
|
||||||
// sensor = SensorType.Trainer;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//};
|
|
||||||
|
|
||||||
var addressStr = address;
|
var addressStr = address;
|
||||||
if (!pCache.ContainsKey(addressStr))
|
if (!pCache.ContainsKey(addressStr))
|
||||||
@ -174,10 +177,10 @@ namespace Assets.Scripts.Ble
|
|||||||
//Debug.Log(sensor);
|
//Debug.Log(sensor);
|
||||||
//pCache[address.ToString()].SensorType = sensor;
|
//pCache[address.ToString()].SensorType = sensor;
|
||||||
|
|
||||||
//foreach (var item in services)
|
foreach (var item in uuids)
|
||||||
//{
|
{
|
||||||
// pCache[addressStr].TryAddService(item);
|
pCache[addressStr].TryAddService(new Guid(item));
|
||||||
//}
|
}
|
||||||
|
|
||||||
pCache[addressStr].Index++;
|
pCache[addressStr].Index++;
|
||||||
if (pCache[addressStr].SensorType == SensorType.Power && pCache[addressStr].Services.Any(s => s.Equals(ServiceUuids.Get(ServiceUuids.TacxBle).IdGuid)))
|
if (pCache[addressStr].SensorType == SensorType.Power && pCache[addressStr].Services.Any(s => s.Equals(ServiceUuids.Get(ServiceUuids.TacxBle).IdGuid)))
|
||||||
@ -198,7 +201,6 @@ namespace Assets.Scripts.Ble
|
|||||||
{
|
{
|
||||||
_discoveredCallback?.Invoke(pCache[addressStr]);
|
_discoveredCallback?.Invoke(pCache[addressStr]);
|
||||||
}
|
}
|
||||||
this.BleState = BleState.On;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//private WclBleGattThread SetUpGattClient(BlePeripheralInfo peripheral)
|
//private WclBleGattThread SetUpGattClient(BlePeripheralInfo peripheral)
|
||||||
@ -220,21 +222,6 @@ namespace Assets.Scripts.Ble
|
|||||||
List<BleCharacteristicInfo> characteristilist = new List<BleCharacteristicInfo>();
|
List<BleCharacteristicInfo> characteristilist = new List<BleCharacteristicInfo>();
|
||||||
public void ConnectPeripheral(BlePeripheralInfo info, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse> callback)
|
public void ConnectPeripheral(BlePeripheralInfo info, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse> callback)
|
||||||
{
|
{
|
||||||
//this.callbacks.Add(info, callback);
|
|
||||||
//WclBleGattThread wclBleGattThread = this.wclBleMainThread.GetGattThread(info);
|
|
||||||
|
|
||||||
//if (wclBleGattThread == null)
|
|
||||||
//{
|
|
||||||
|
|
||||||
// wclBleGattThread = this.SetUpGattClient(info);
|
|
||||||
// this.ConnectInternal(wclBleGattThread);
|
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
//else
|
|
||||||
//{
|
|
||||||
// this.ConnectInternal(wclBleGattThread);
|
|
||||||
//}\
|
|
||||||
|
|
||||||
BleResponse s = new BleResponse();
|
BleResponse s = new BleResponse();
|
||||||
s.IsSuccess = true;
|
s.IsSuccess = true;
|
||||||
s.Error = null;
|
s.Error = null;
|
||||||
@ -243,48 +230,49 @@ namespace Assets.Scripts.Ble
|
|||||||
{
|
{
|
||||||
callback?.Invoke(self, info, s);
|
callback?.Invoke(self, info, s);
|
||||||
}, (address, service) => {
|
}, (address, service) => {
|
||||||
var l = servicelist.Where(c => c.Id.ToString().Equals(service)).ToList();
|
if (servicesCallbacks.Where(c => c.Key.Address == address).Any())
|
||||||
if (l.Count == 0)
|
|
||||||
{
|
{
|
||||||
servicelist.Add(new WinBleServiceInfo(info,new Guid(service)));
|
|
||||||
}
|
var serviceCallback = servicesCallbacks.Where(c => c.Key.Address == address).FirstOrDefault();
|
||||||
}, (address, service, characteristic) =>
|
List<BleServiceInfo> servicelist = new List<BleServiceInfo>();
|
||||||
{
|
servicelist.Add(new WinBleServiceInfo(serviceCallback.Key,new Guid(service)));
|
||||||
var l = characteristilist.Where(c => c.Id.ToString().Equals(characteristic)).ToList();
|
BleResponse<List<BleServiceInfo>> response = new BleResponse<List<BleServiceInfo>>
|
||||||
if (l.Count == 0)
|
{
|
||||||
{
|
IsSuccess = true,
|
||||||
//characteristilist.Add(new BleCharacteristicInfo new WinBleServiceInfo(info, new Guid(characteristic)));
|
Error = null,
|
||||||
|
Data = servicelist,
|
||||||
|
};
|
||||||
|
serviceCallback.Value?.Invoke(this, serviceCallback.Key, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}, (address, service, characteristic) =>
|
||||||
|
{
|
||||||
|
if (characteristicsDiscoveredCallbacks.Where(c => c.Key.Peripheral.Address == address).Any())
|
||||||
|
{
|
||||||
|
var characteristicCallback = characteristicsDiscoveredCallbacks.Where(c => c.Key.Peripheral.Address == address).FirstOrDefault();
|
||||||
|
List<BleCharacteristicInfo> characteristiclist = new List<BleCharacteristicInfo>();
|
||||||
|
characteristiclist.Add(new WinBleCharacteristicInfo(new Guid(characteristic), new WinBleServiceInfo(characteristicCallback.Key.Peripheral, new Guid(service)),0));
|
||||||
|
BleResponse<List<BleCharacteristicInfo>> response = new BleResponse<List<BleCharacteristicInfo>>
|
||||||
|
{
|
||||||
|
IsSuccess = true,
|
||||||
|
Error = null,
|
||||||
|
Data = characteristiclist,
|
||||||
|
};
|
||||||
|
characteristicCallback.Value?.Invoke(this, characteristicCallback.Key, response);
|
||||||
|
}
|
||||||
}, null);
|
}, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ConnectInternal(WclBleGattThread gattClient)
|
//设备断开连接
|
||||||
{
|
|
||||||
//Task.Run(() =>
|
|
||||||
//{
|
|
||||||
//int num = gattClient.Connect();
|
|
||||||
//Debug.Log("连接设备返回" + num);
|
|
||||||
|
|
||||||
|
|
||||||
//});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback)
|
public void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback)
|
||||||
{
|
{
|
||||||
//var gattThread = this.wclBleMainThread.GetGattThread(peripheral);
|
BluetoothLEHardwareInterface.DisconnectPeripheral(peripheral.Address, (address) =>
|
||||||
//if(gattThread != null && gattThread.CanLoadWork)
|
{
|
||||||
//{
|
//peripheralDisconnectedEvent(this, peripheral, null, true);
|
||||||
// this.callbacks.Remove(peripheral);
|
callback?.Invoke();
|
||||||
// this.servicesCallbacks.Remove(peripheral);
|
});
|
||||||
// this.characteristicNotificationCallbacks.Remove(peripheral);
|
|
||||||
|
|
||||||
// this.disconnectedCallback.Add(peripheral, callback);
|
|
||||||
// gattThread.Discounect();
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static BleMobileInterface GetInterface()
|
public static BleMobileInterface GetInterface()
|
||||||
{
|
{
|
||||||
if(hwInterface == null)
|
if(hwInterface == null)
|
||||||
@ -294,223 +282,73 @@ namespace Assets.Scripts.Ble
|
|||||||
|
|
||||||
return hwInterface;
|
return hwInterface;
|
||||||
}
|
}
|
||||||
|
//扫描设备
|
||||||
public void StartScan(Action<BleAdvertisementInfo> discoveredCallBack)
|
public void StartScan(Action<BleAdvertisementInfo> discoveredCallBack)
|
||||||
{
|
{
|
||||||
pCache.Clear();
|
pCache.Clear();
|
||||||
_discoveredCallback = discoveredCallBack;
|
_discoveredCallback = discoveredCallBack;
|
||||||
bleMobileThread.StartWatcher();
|
bleMobileThread.StartWatcher();
|
||||||
}
|
}
|
||||||
|
//发现服务列表
|
||||||
private void ManagerStatusChanged(WclBleMainThread sender, WclBleManagerStatus status)
|
|
||||||
{
|
|
||||||
//this.BleState = BleMobileInterface.StateFromNativeState(status);
|
|
||||||
//Debug.Log("win hw:" + status);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GattConnected(WclBleGattThread gattClient, BleResponse response)
|
|
||||||
{
|
|
||||||
//Debug.Log($"gatt connected { response.ToString() }");
|
|
||||||
//if (!response.IsSuccess)
|
|
||||||
//{
|
|
||||||
// gattClient.Stop();
|
|
||||||
|
|
||||||
// this.callbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response);
|
|
||||||
// this.callbacks.Remove(gattClient.Peripheral);
|
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
//this.callbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response);
|
|
||||||
//this.callbacks.Remove(gattClient.Peripheral);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CleanUpPeripheral(BlePeripheralInfo peripheralInfo)
|
|
||||||
{
|
|
||||||
//this.callbacks.Clear()
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GattDisconnected(WclBleGattThread gattClient, BleResponse response)
|
|
||||||
{
|
|
||||||
//Debug.Log($"gatt disconnected { gattClient.Peripheral.Name }");
|
|
||||||
|
|
||||||
//this.callbacks.Remove(gattClient.Peripheral);
|
|
||||||
//this.servicesCallbacks.Remove(gattClient.Peripheral);
|
|
||||||
//this.characteristicNotificationCallbacks.Remove(gattClient.Peripheral);
|
|
||||||
////App.MainDeviceAdapter.PrintStatus();
|
|
||||||
|
|
||||||
//var manualDisconnect = disconnectedCallback.ContainsKey(gattClient.Peripheral);
|
|
||||||
//this.peripheralDisconnectedEvent(this, gattClient.Peripheral, response, manualDisconnect);
|
|
||||||
|
|
||||||
////App.MainDeviceAdapter.PrintStatus();
|
|
||||||
//if (disconnectedCallback.ContainsKey(gattClient.Peripheral))
|
|
||||||
//{
|
|
||||||
// disconnectedCallback[gattClient.Peripheral].Invoke();
|
|
||||||
// disconnectedCallback.Remove(gattClient.Peripheral);
|
|
||||||
//}
|
|
||||||
////this.pCache.Remove(gattClient.Peripheral.Address);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
private void GattServicesDiscovered(WclBleGattThread gattClient, BleResponse<List<BleServiceInfo>> response)
|
|
||||||
{
|
|
||||||
////Debug.Log("services discovered");
|
|
||||||
////this.callbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response);
|
|
||||||
|
|
||||||
////foreach (var item in response.Data)
|
|
||||||
////{
|
|
||||||
//// Debug.Log(item.ToString());
|
|
||||||
////}
|
|
||||||
|
|
||||||
//if (this.servicesCallbacks.ContainsKey(gattClient.Peripheral))
|
|
||||||
//{
|
|
||||||
// this.servicesCallbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response);
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GattCharacteristicsDiscovered(WclBleGattThread gattClient, BleServiceInfo service, BleResponse<List<BleCharacteristicInfo>> response)
|
|
||||||
{
|
|
||||||
//Debug.Log("characteristics discovered");
|
|
||||||
|
|
||||||
if (this.characteristicsDiscoveredCallbacks.ContainsKey(service))
|
|
||||||
{
|
|
||||||
this.characteristicsDiscoveredCallbacks[service].Invoke(this, service, response);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GattCharacteristicSubscribed(WclBleGattThread gattClient, BleCharacteristicInfo characteristic, BleResponse response)
|
|
||||||
{
|
|
||||||
////Debug.Log("characteristics subscribed");
|
|
||||||
//if (this.characteristicNotificationCallbacks.ContainsKey(gattClient.Peripheral))
|
|
||||||
//{
|
|
||||||
// this.characteristicNotificationCallbacks[gattClient.Peripheral].Invoke(this, characteristic, response);
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
private void GattCharacteristicRead(WclBleGattThread gattClient, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
|
|
||||||
{
|
|
||||||
//Debug.Log("characteristic read");
|
|
||||||
|
|
||||||
//if (this.characteristicReadCallbacks.ContainsKey(characteristic))
|
|
||||||
//{
|
|
||||||
// this.characteristicReadCallbacks[characteristic].Invoke(this, characteristic, response);
|
|
||||||
// this.characteristicReadCallbacks.Remove(characteristic);
|
|
||||||
//}
|
|
||||||
|
|
||||||
//this.characteristicReadEvent.Invoke(this, characteristic, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GattCharacteristicWrote(WclBleGattThread gattClient, BleCharacteristicInfo characteristic, BleResponse<BleCharacteristicWriteType> response)
|
|
||||||
{
|
|
||||||
Debug.Log("characteristic wrote");
|
|
||||||
}
|
|
||||||
private void GattCharacteristicChanged(WclBleGattThread gattClient, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
|
|
||||||
{
|
|
||||||
////Debug.Log("characteristic changed");
|
|
||||||
//if(this.wclBleMainThread.GetGattThread(characteristic.Peripheral) != null)
|
|
||||||
//{
|
|
||||||
// this.characteristicReadEvent.Invoke(this, characteristic, response);
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DiscoverServices(BlePeripheralInfo peripheral, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse<List<BleServiceInfo>>> callback)
|
public void DiscoverServices(BlePeripheralInfo peripheral, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse<List<BleServiceInfo>>> callback)
|
||||||
{
|
{
|
||||||
this.servicesCallbacks.Add(peripheral, callback);
|
this.servicesCallbacks.Add(peripheral, callback);
|
||||||
if (servicelist.Count > 0)
|
|
||||||
{
|
|
||||||
BleResponse<List<BleServiceInfo>> response = new BleResponse<List<BleServiceInfo>>
|
|
||||||
{
|
|
||||||
IsSuccess = true,
|
|
||||||
Error = null,
|
|
||||||
Data = (List<BleServiceInfo>)servicelist,
|
|
||||||
};
|
|
||||||
|
|
||||||
callback.Invoke(this, peripheral, response);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
//发现特征值
|
||||||
public void DiscoverCharacteristic(BleServiceInfo service, CharacteristicsDiscoveredCallback callback)
|
public void DiscoverCharacteristic(BleServiceInfo service, CharacteristicsDiscoveredCallback callback)
|
||||||
{
|
{
|
||||||
//WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(service.Peripheral);
|
this.characteristicsDiscoveredCallbacks.Add(service, callback);
|
||||||
//if(gattThread == null)
|
|
||||||
//{
|
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
//if (!this.characteristicsDiscoveredCallbacks.ContainsKey(service))
|
|
||||||
//{
|
|
||||||
// this.characteristicsDiscoveredCallbacks.Add(service, callback);
|
|
||||||
//}
|
|
||||||
//int num = gattThread.DiscoverCharacteristics(service);
|
|
||||||
//if (WclBleErrors.IsSuccessCode(num))
|
|
||||||
//{
|
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
//BleResponse<List<BleCharacteristicInfo>> response = new BleResponse<List<BleCharacteristicInfo>>
|
|
||||||
//{
|
|
||||||
// IsSuccess = false,
|
|
||||||
// Error = new BleHwInterfaceError(new int?(num), "WclBleGattClientErrorDomain", string.Format("Error discovering characteristics - {0}", num))
|
|
||||||
//};
|
|
||||||
//this.GattCharacteristicsDiscovered(gattThread, service, response);
|
|
||||||
}
|
}
|
||||||
|
//订阅特征值
|
||||||
public void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse> callback)
|
public void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse> callback)
|
||||||
{
|
{
|
||||||
//WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(characteristic.Peripheral);
|
this.characteristicNotificationCallbacks.Add(characteristic.Peripheral, callback);
|
||||||
|
BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(characteristic.Peripheral.Address, characteristic.Service.ToString(), characteristic.Id.ToString(), null, (deviceAddress, characteristric, bytes) =>
|
||||||
//this.characteristicNotificationCallbacks.Add(gattThread.Peripheral, callback);
|
{
|
||||||
//int num = gattThread.SubscribeCharacteristic(characteristic);
|
if (characteristicReadEvent != null)
|
||||||
//if (WclBleErrors.IsSuccessCode(num))
|
{
|
||||||
//{
|
BleResponse<byte[]> response = new BleResponse<byte[]>
|
||||||
// return;
|
{
|
||||||
//}
|
IsSuccess = true,
|
||||||
//BleResponse<List<BleServiceInfo>> response = new BleResponse<List<BleServiceInfo>>
|
Error = null,
|
||||||
//{
|
Data = bytes,
|
||||||
// IsSuccess = false,
|
};
|
||||||
// Error = new BleHwInterfaceError(new int?(num), "WclBleGattClientErrorDomain", string.Format("Error subscribing characteristic - {0}", num))
|
characteristicReadEvent.Invoke(this, characteristic, response);
|
||||||
//};
|
}
|
||||||
//this.GattCharacteristicSubscribed(gattThread, characteristic, response);
|
});
|
||||||
}
|
}
|
||||||
|
//写入特征值
|
||||||
public void WriteCharacteristic(BleCharacteristicInfo characteristic, byte[] data)
|
public void WriteCharacteristic(BleCharacteristicInfo characteristic, byte[] data)
|
||||||
{
|
{
|
||||||
//var gattThread = this.wclBleMainThread.GetGattThread(characteristic.Peripheral);
|
BluetoothLEHardwareInterface.WriteCharacteristic(characteristic.Peripheral.Address, characteristic.Service.Id.ToString(), characteristic.Id.ToString(), data, data.Length, false, (characteristicUUID) => {
|
||||||
//if(gattThread == null)
|
|
||||||
//{
|
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
|
|
||||||
//int num = gattThread.WriteCharacteristic(characteristic, data);
|
BluetoothLEHardwareInterface.Log("Write Succeeded");
|
||||||
//if (WclBleErrors.IsSuccessCode(num))
|
});
|
||||||
//{
|
|
||||||
// Debug.Log("设置命令成功");
|
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
//this.wclBleMainThread.ManagerStatusChanged -= ManagerStatusChanged;
|
bleMobileThread.ManagerInitialized -= BleMobileThread_ManagerInitialized;
|
||||||
//this.wclBleMainThread.ScanInfoReceived -= WatcherScanInfoReceived;
|
bleMobileThread.ManagerStatusChanged -= ManagerStatusChanged;
|
||||||
//this.wclBleMainThread.Stop();
|
bleMobileThread.ScanInfoReceived -= WatcherScanInfoReceived;
|
||||||
//this.wclBleMainThread = null;
|
bleMobileThread.Stop();
|
||||||
//hwInterface = null;
|
bleMobileThread = null;
|
||||||
//pCache.Clear();
|
hwInterface = null;
|
||||||
|
pCache.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ReadCharacteristic(BleCharacteristicInfo characteristic, CharacteristicReadCallback callback)
|
public void ReadCharacteristic(BleCharacteristicInfo characteristic, CharacteristicReadCallback callback)
|
||||||
{
|
{
|
||||||
//WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(characteristic.Peripheral);
|
BluetoothLEHardwareInterface.ReadCharacteristic(characteristic.Peripheral.Address, characteristic.Service.Id.ToString(), characteristic.Id.ToString(), (c, bytes) => {
|
||||||
//if(gattThread == null)
|
BleResponse<byte[]> response = new BleResponse<byte[]>
|
||||||
//{
|
{
|
||||||
// return;
|
IsSuccess = true,
|
||||||
//}
|
Error = null,
|
||||||
//this.characteristicReadCallbacks.Add(characteristic, callback);
|
Data = bytes,
|
||||||
//int num = gattThread.ReadCharacteristicValue(characteristic);
|
};
|
||||||
//if (WclBleErrors.IsSuccessCode(num))
|
callback?.Invoke(this, characteristic, response);
|
||||||
//{
|
});
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BleState StateFromNativeState(WclBleManagerStatus status)
|
private static BleState StateFromNativeState(WclBleManagerStatus status)
|
||||||
|
|||||||
@ -3,64 +3,78 @@ using System.Timers;
|
|||||||
|
|
||||||
namespace Assets.Scripts.Devices.Ble
|
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 delegate void WclInitializedDelegate(BleMobileThread sender);
|
||||||
public BleMobileThread.WclAdvertisementPacketDelegate ScanInfoReceived;
|
private BleMobileThread.WclInitializedDelegate managerInitialized;
|
||||||
private Timer timer { get; set; }
|
public event BleMobileThread.WclInitializedDelegate ManagerInitialized
|
||||||
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)
|
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)
|
add
|
||||||
//{
|
{
|
||||||
// startScan = false;
|
this.managerStatusChanged += value;
|
||||||
// BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, null, null,(address, name, rssi, bytes) =>
|
}
|
||||||
// {
|
remove
|
||||||
|
{
|
||||||
|
this.managerStatusChanged -= value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ScanInfoReceived?.Invoke(this, address, name, rssi);
|
internal BleMobileThread() {
|
||||||
// }, true);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -56,7 +56,7 @@ namespace Assets.Scenes.Ride.Scripts
|
|||||||
weight = App.CurrentUser.Weight;
|
weight = App.CurrentUser.Weight;
|
||||||
bicycleWeight = App.CurrentUser.BicycleWeight;
|
bicycleWeight = App.CurrentUser.BicycleWeight;
|
||||||
//#if UNITY_EDITOR
|
//#if UNITY_EDITOR
|
||||||
power = 500;
|
//power = 500;
|
||||||
//#endif
|
//#endif
|
||||||
mainController.TrackResistance(currentSlope * App.RideSetting.Sensitivity / 100);
|
mainController.TrackResistance(currentSlope * App.RideSetting.Sensitivity / 100);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user