移动端蓝牙框架初步导入
This commit is contained in:
1 parent
f6740d31bb
commit
6fab61f97d
39 files changed
+4293
-238
No files matched your search
@@ -37,5 +37,10 @@ namespace Assets.Scripts.Devices.Ble
|
||||
{
|
||||
return base.ToString() + "\n" + this.Data;
|
||||
}
|
||||
}
|
||||
|
||||
public static implicit operator BleResponse<T>(BleResponse<List<BleServiceInfo>> v)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ namespace Assets.Scripts.Ble
|
||||
public sealed class BleMobileInterface: IBleWinHwInterface
|
||||
{
|
||||
private static BleMobileInterface hwInterface;
|
||||
private WclBleMainThread wclBleMainThread;
|
||||
private BleMobileThread bleMobileThread;
|
||||
|
||||
private Dictionary<string, BleAdvertisementInfo> _pCache;
|
||||
public Dictionary<string, BleAdvertisementInfo> pCache
|
||||
@@ -101,70 +101,71 @@ namespace Assets.Scripts.Ble
|
||||
|
||||
private BleMobileInterface()
|
||||
{
|
||||
wclBleMainThread = new WclBleMainThread();
|
||||
wclBleMainThread.ManagerStatusChanged += ManagerStatusChanged;
|
||||
wclBleMainThread.ScanInfoReceived += WatcherScanInfoReceived;
|
||||
//wclBleMainThread.gatt
|
||||
wclBleMainThread.Start();
|
||||
|
||||
bleMobileThread = new BleMobileThread();
|
||||
bleMobileThread.ScanInfoReceived += WatcherScanInfoReceived;
|
||||
}
|
||||
|
||||
private void WatcherScanInfoReceived(WclBleMainThread sender, long address, string name, int rssi, CPPBridge.WclBleAdvertisementType packetType, Guid? service)
|
||||
private void WatcherScanInfoReceived(BleMobileThread sender, string address, string name, int rssi,string type)
|
||||
{
|
||||
//if(address != 224160707349234)
|
||||
//if (address != 224160707349234)
|
||||
//{
|
||||
//return;
|
||||
// return;
|
||||
//}
|
||||
//Debug.Log($"address:{ address }, name:{ name }, service:{ (service == null ? "" : service.Value.ToString()) }");
|
||||
|
||||
|
||||
SensorType sensor = SensorType.None;
|
||||
List<Guid> services = new List<Guid>();
|
||||
if (service != null)
|
||||
{
|
||||
services = new List<Guid> { service.Value };
|
||||
foreach(var item in ServiceUuids.Services)
|
||||
{
|
||||
if(item.IdGuid != service.Value)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if(item.IdByteArray == ServiceUuids.Ftms)
|
||||
{
|
||||
sensor = SensorType.Trainer;
|
||||
}
|
||||
else if(item.IdByteArray == ServiceUuids.HeartRate)
|
||||
{
|
||||
sensor = SensorType.HeartRate;
|
||||
}
|
||||
else if(item.IdByteArray == ServiceUuids.CyclingPower)
|
||||
{
|
||||
sensor = SensorType.Power;
|
||||
//sensor = SensorType.Trainer;
|
||||
}
|
||||
else if(item.IdByteArray == ServiceUuids.CyclingSpeedCadence)
|
||||
{
|
||||
sensor = SensorType.SpeedCadence;
|
||||
}
|
||||
else if(item.IdByteArray == ServiceUuids.TacxBle)
|
||||
{
|
||||
sensor = SensorType.Trainer;
|
||||
}
|
||||
}
|
||||
};
|
||||
var addressStr = address.ToString("X12");
|
||||
if (type.Equals("trainer"))
|
||||
{
|
||||
sensor = SensorType.Trainer;
|
||||
}
|
||||
else
|
||||
{
|
||||
sensor = SensorType.HeartRate;
|
||||
}
|
||||
//if (service != null)
|
||||
//{
|
||||
// services = new List<Guid> { service.Value };
|
||||
// foreach (var item in ServiceUuids.Services)
|
||||
// {
|
||||
// if (item.IdGuid != service.Value)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
// if (item.IdByteArray == ServiceUuids.Ftms)
|
||||
// {
|
||||
// sensor = SensorType.Trainer;
|
||||
// }
|
||||
// else if (item.IdByteArray == ServiceUuids.HeartRate)
|
||||
// {
|
||||
// sensor = SensorType.HeartRate;
|
||||
// }
|
||||
// else if (item.IdByteArray == ServiceUuids.CyclingPower)
|
||||
// {
|
||||
// sensor = SensorType.Power;
|
||||
// //sensor = SensorType.Trainer;
|
||||
// }
|
||||
// else if (item.IdByteArray == ServiceUuids.CyclingSpeedCadence)
|
||||
// {
|
||||
// sensor = SensorType.SpeedCadence;
|
||||
// }
|
||||
// else if (item.IdByteArray == ServiceUuids.TacxBle)
|
||||
// {
|
||||
// sensor = SensorType.Trainer;
|
||||
// }
|
||||
// }
|
||||
//};
|
||||
|
||||
var addressStr = address;
|
||||
if (!pCache.ContainsKey(addressStr))
|
||||
{
|
||||
var device = new BleAdvertisementInfo(new WinBlePeripheralInfo(addressStr, name), rssi, true, services, null, sensor);
|
||||
pCache.Add(addressStr, device);
|
||||
|
||||
//WclBleGattThread gattClient = this.SetUpGattClient(device.Peripheral);
|
||||
//this.ConnectInternal(gattClient);
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(name))
|
||||
{
|
||||
(pCache[addressStr].Peripheral as WinBlePeripheralInfo).SetName(name);
|
||||
}
|
||||
if(sensor == SensorType.None)
|
||||
if (sensor == SensorType.None)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -172,19 +173,19 @@ namespace Assets.Scripts.Ble
|
||||
pCache[addressStr].Rssi = rssi;
|
||||
//Debug.Log(sensor);
|
||||
//pCache[address.ToString()].SensorType = sensor;
|
||||
|
||||
foreach (var item in services)
|
||||
{
|
||||
pCache[addressStr].TryAddService(item);
|
||||
}
|
||||
|
||||
|
||||
//foreach (var item in services)
|
||||
//{
|
||||
// pCache[addressStr].TryAddService(item);
|
||||
//}
|
||||
|
||||
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)))
|
||||
{
|
||||
pCache[addressStr].SensorType = SensorType.Trainer;
|
||||
//Debug.Log("纠正为trainer, "+ pCache[address.ToString()].Index);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (pCache[addressStr].SensorType == SensorType.Power)
|
||||
{
|
||||
@@ -193,51 +194,77 @@ namespace Assets.Scripts.Ble
|
||||
_discoveredCallback?.Invoke(pCache[addressStr]);
|
||||
}
|
||||
}
|
||||
else if(pCache[addressStr].SensorType != SensorType.None)
|
||||
else if (pCache[addressStr].SensorType != SensorType.None)
|
||||
{
|
||||
_discoveredCallback?.Invoke(pCache[addressStr]);
|
||||
}
|
||||
this.BleState = BleState.On;
|
||||
}
|
||||
|
||||
private WclBleGattThread SetUpGattClient(BlePeripheralInfo peripheral)
|
||||
{
|
||||
WclBleGattThread wclBleGattThread = this.wclBleMainThread.CreateGattThread(peripheral);
|
||||
wclBleGattThread.GattConnected += this.GattConnected;
|
||||
wclBleGattThread.GattDisconnected += this.GattDisconnected;
|
||||
wclBleGattThread.GattServicesDiscovered += this.GattServicesDiscovered;
|
||||
wclBleGattThread.GattCharacteristicsDiscovered += this.GattCharacteristicsDiscovered;
|
||||
wclBleGattThread.GattCharacteristicSubscribed += this.GattCharacteristicSubscribed;
|
||||
wclBleGattThread.GattCharacteristicRead += this.GattCharacteristicRead;
|
||||
wclBleGattThread.GattCharacteristicWrote += this.GattCharacteristicWrote;
|
||||
wclBleGattThread.GattCharacteristicChanged += this.GattCharacteristicChanged;
|
||||
wclBleGattThread.Start();
|
||||
return wclBleGattThread;
|
||||
}
|
||||
//private WclBleGattThread SetUpGattClient(BlePeripheralInfo peripheral)
|
||||
//{
|
||||
// WclBleGattThread wclBleGattThread = this.wclBleMainThread.CreateGattThread(peripheral);
|
||||
// wclBleGattThread.GattConnected += this.GattConnected;
|
||||
// wclBleGattThread.GattDisconnected += this.GattDisconnected;
|
||||
// wclBleGattThread.GattServicesDiscovered += this.GattServicesDiscovered;
|
||||
// wclBleGattThread.GattCharacteristicsDiscovered += this.GattCharacteristicsDiscovered;
|
||||
// wclBleGattThread.GattCharacteristicSubscribed += this.GattCharacteristicSubscribed;
|
||||
// wclBleGattThread.GattCharacteristicRead += this.GattCharacteristicRead;
|
||||
// wclBleGattThread.GattCharacteristicWrote += this.GattCharacteristicWrote;
|
||||
// wclBleGattThread.GattCharacteristicChanged += this.GattCharacteristicChanged;
|
||||
// wclBleGattThread.Start();
|
||||
// return wclBleGattThread;
|
||||
//}
|
||||
|
||||
List<BleServiceInfo> servicelist = new List<BleServiceInfo>();
|
||||
List<BleCharacteristicInfo> characteristilist = new List<BleCharacteristicInfo>();
|
||||
public void ConnectPeripheral(BlePeripheralInfo info, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse> callback)
|
||||
{
|
||||
this.callbacks.Add(info, callback);
|
||||
WclBleGattThread wclBleGattThread = this.wclBleMainThread.GetGattThread(info);
|
||||
//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);
|
||||
//}\
|
||||
|
||||
if(wclBleGattThread == null)
|
||||
BleResponse s = new BleResponse();
|
||||
s.IsSuccess = true;
|
||||
s.Error = null;
|
||||
var self = this;
|
||||
BluetoothLEHardwareInterface.ConnectToPeripheral(info.Address, (address) =>
|
||||
{
|
||||
callback?.Invoke(self, info, s);
|
||||
}, (address, service) => {
|
||||
var l = servicelist.Where(c => c.Id.ToString().Equals(service)).ToList();
|
||||
if (l.Count == 0)
|
||||
{
|
||||
servicelist.Add(new WinBleServiceInfo(info,new Guid(service)));
|
||||
}
|
||||
}, (address, service, characteristic) =>
|
||||
{
|
||||
var l = characteristilist.Where(c => c.Id.ToString().Equals(characteristic)).ToList();
|
||||
if (l.Count == 0)
|
||||
{
|
||||
//characteristilist.Add(new BleCharacteristicInfo new WinBleServiceInfo(info, new Guid(characteristic)));
|
||||
}
|
||||
|
||||
wclBleGattThread = this.SetUpGattClient(info);
|
||||
this.ConnectInternal(wclBleGattThread);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.ConnectInternal(wclBleGattThread);
|
||||
}
|
||||
}, null);
|
||||
}
|
||||
|
||||
private void ConnectInternal(WclBleGattThread gattClient)
|
||||
{
|
||||
//Task.Run(() =>
|
||||
//{
|
||||
int num = gattClient.Connect();
|
||||
Debug.Log("连接设备返回" + num);
|
||||
//int num = gattClient.Connect();
|
||||
//Debug.Log("连接设备返回" + num);
|
||||
|
||||
|
||||
//});
|
||||
@@ -245,16 +272,16 @@ namespace Assets.Scripts.Ble
|
||||
|
||||
public void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback)
|
||||
{
|
||||
var gattThread = this.wclBleMainThread.GetGattThread(peripheral);
|
||||
if(gattThread != null && gattThread.CanLoadWork)
|
||||
{
|
||||
this.callbacks.Remove(peripheral);
|
||||
this.servicesCallbacks.Remove(peripheral);
|
||||
this.characteristicNotificationCallbacks.Remove(peripheral);
|
||||
//var gattThread = this.wclBleMainThread.GetGattThread(peripheral);
|
||||
//if(gattThread != null && gattThread.CanLoadWork)
|
||||
//{
|
||||
// this.callbacks.Remove(peripheral);
|
||||
// this.servicesCallbacks.Remove(peripheral);
|
||||
// this.characteristicNotificationCallbacks.Remove(peripheral);
|
||||
|
||||
this.disconnectedCallback.Add(peripheral, callback);
|
||||
gattThread.Discounect();
|
||||
}
|
||||
// this.disconnectedCallback.Add(peripheral, callback);
|
||||
// gattThread.Discounect();
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
@@ -272,29 +299,29 @@ namespace Assets.Scripts.Ble
|
||||
{
|
||||
pCache.Clear();
|
||||
_discoveredCallback = discoveredCallBack;
|
||||
this.wclBleMainThread.StartWatcher();
|
||||
bleMobileThread.StartWatcher();
|
||||
}
|
||||
|
||||
private void ManagerStatusChanged(WclBleMainThread sender, WclBleManagerStatus status)
|
||||
{
|
||||
this.BleState = BleMobileInterface.StateFromNativeState(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();
|
||||
//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);
|
||||
// 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)
|
||||
@@ -304,41 +331,41 @@ namespace Assets.Scripts.Ble
|
||||
|
||||
private void GattDisconnected(WclBleGattThread gattClient, BleResponse response)
|
||||
{
|
||||
Debug.Log($"gatt disconnected { gattClient.Peripheral.Name }");
|
||||
//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();
|
||||
//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);
|
||||
//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);
|
||||
////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);
|
||||
////Debug.Log("services discovered");
|
||||
////this.callbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response);
|
||||
|
||||
//foreach (var item in response.Data)
|
||||
////foreach (var item in response.Data)
|
||||
////{
|
||||
//// Debug.Log(item.ToString());
|
||||
////}
|
||||
|
||||
//if (this.servicesCallbacks.ContainsKey(gattClient.Peripheral))
|
||||
//{
|
||||
// Debug.Log(item.ToString());
|
||||
// this.servicesCallbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response);
|
||||
//}
|
||||
|
||||
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)
|
||||
@@ -353,23 +380,23 @@ namespace Assets.Scripts.Ble
|
||||
|
||||
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);
|
||||
}
|
||||
////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");
|
||||
//Debug.Log("characteristic read");
|
||||
|
||||
if (this.characteristicReadCallbacks.ContainsKey(characteristic))
|
||||
{
|
||||
this.characteristicReadCallbacks[characteristic].Invoke(this, characteristic, response);
|
||||
this.characteristicReadCallbacks.Remove(characteristic);
|
||||
}
|
||||
//if (this.characteristicReadCallbacks.ContainsKey(characteristic))
|
||||
//{
|
||||
// this.characteristicReadCallbacks[characteristic].Invoke(this, characteristic, response);
|
||||
// this.characteristicReadCallbacks.Remove(characteristic);
|
||||
//}
|
||||
|
||||
this.characteristicReadEvent.Invoke(this, characteristic, response);
|
||||
//this.characteristicReadEvent.Invoke(this, characteristic, response);
|
||||
}
|
||||
|
||||
private void GattCharacteristicWrote(WclBleGattThread gattClient, BleCharacteristicInfo characteristic, BleResponse<BleCharacteristicWriteType> response)
|
||||
@@ -378,119 +405,112 @@ namespace Assets.Scripts.Ble
|
||||
}
|
||||
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);
|
||||
}
|
||||
////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)
|
||||
{
|
||||
WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(peripheral);
|
||||
if(gattThread == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.servicesCallbacks.Add(peripheral, callback);
|
||||
int num = gattThread.DiscoverServices();
|
||||
if (WclBleErrors.IsSuccessCode(num))
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
BleResponse<List<BleServiceInfo>> response = new BleResponse<List<BleServiceInfo>>
|
||||
{
|
||||
IsSuccess = false,
|
||||
Error = new BleHwInterfaceError(new int?(num), "WclBleGattClientErrorDomain", string.Format("Error discovering services - {0}", num))
|
||||
};
|
||||
this.GattServicesDiscovered(gattThread, response);
|
||||
}
|
||||
|
||||
public void DiscoverCharacteristic(BleServiceInfo service, CharacteristicsDiscoveredCallback callback)
|
||||
{
|
||||
WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(service.Peripheral);
|
||||
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);
|
||||
//WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(service.Peripheral);
|
||||
//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)
|
||||
{
|
||||
WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(characteristic.Peripheral);
|
||||
//WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(characteristic.Peripheral);
|
||||
|
||||
this.characteristicNotificationCallbacks.Add(gattThread.Peripheral, callback);
|
||||
int num = gattThread.SubscribeCharacteristic(characteristic);
|
||||
if (WclBleErrors.IsSuccessCode(num))
|
||||
{
|
||||
return;
|
||||
}
|
||||
BleResponse<List<BleServiceInfo>> response = new BleResponse<List<BleServiceInfo>>
|
||||
{
|
||||
IsSuccess = false,
|
||||
Error = new BleHwInterfaceError(new int?(num), "WclBleGattClientErrorDomain", string.Format("Error subscribing characteristic - {0}", num))
|
||||
};
|
||||
this.GattCharacteristicSubscribed(gattThread, characteristic, response);
|
||||
//this.characteristicNotificationCallbacks.Add(gattThread.Peripheral, callback);
|
||||
//int num = gattThread.SubscribeCharacteristic(characteristic);
|
||||
//if (WclBleErrors.IsSuccessCode(num))
|
||||
//{
|
||||
// return;
|
||||
//}
|
||||
//BleResponse<List<BleServiceInfo>> response = new BleResponse<List<BleServiceInfo>>
|
||||
//{
|
||||
// IsSuccess = false,
|
||||
// Error = new BleHwInterfaceError(new int?(num), "WclBleGattClientErrorDomain", string.Format("Error subscribing characteristic - {0}", num))
|
||||
//};
|
||||
//this.GattCharacteristicSubscribed(gattThread, characteristic, response);
|
||||
}
|
||||
|
||||
public void WriteCharacteristic(BleCharacteristicInfo characteristic, byte[] data)
|
||||
{
|
||||
var gattThread = this.wclBleMainThread.GetGattThread(characteristic.Peripheral);
|
||||
if(gattThread == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
//var gattThread = this.wclBleMainThread.GetGattThread(characteristic.Peripheral);
|
||||
//if(gattThread == null)
|
||||
//{
|
||||
// return;
|
||||
//}
|
||||
|
||||
int num = gattThread.WriteCharacteristic(characteristic, data);
|
||||
if (WclBleErrors.IsSuccessCode(num))
|
||||
{
|
||||
Debug.Log("设置命令成功");
|
||||
return;
|
||||
}
|
||||
//int num = gattThread.WriteCharacteristic(characteristic, data);
|
||||
//if (WclBleErrors.IsSuccessCode(num))
|
||||
//{
|
||||
// Debug.Log("设置命令成功");
|
||||
// return;
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this.wclBleMainThread.ManagerStatusChanged -= ManagerStatusChanged;
|
||||
this.wclBleMainThread.ScanInfoReceived -= WatcherScanInfoReceived;
|
||||
this.wclBleMainThread.Stop();
|
||||
this.wclBleMainThread = null;
|
||||
hwInterface = null;
|
||||
pCache.Clear();
|
||||
//this.wclBleMainThread.ManagerStatusChanged -= ManagerStatusChanged;
|
||||
//this.wclBleMainThread.ScanInfoReceived -= WatcherScanInfoReceived;
|
||||
//this.wclBleMainThread.Stop();
|
||||
//this.wclBleMainThread = null;
|
||||
//hwInterface = null;
|
||||
//pCache.Clear();
|
||||
}
|
||||
|
||||
public void ReadCharacteristic(BleCharacteristicInfo characteristic, CharacteristicReadCallback callback)
|
||||
{
|
||||
WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(characteristic.Peripheral);
|
||||
if(gattThread == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
this.characteristicReadCallbacks.Add(characteristic, callback);
|
||||
int num = gattThread.ReadCharacteristicValue(characteristic);
|
||||
if (WclBleErrors.IsSuccessCode(num))
|
||||
{
|
||||
return;
|
||||
}
|
||||
//WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(characteristic.Peripheral);
|
||||
//if(gattThread == null)
|
||||
//{
|
||||
// return;
|
||||
//}
|
||||
//this.characteristicReadCallbacks.Add(characteristic, callback);
|
||||
//int num = gattThread.ReadCharacteristicValue(characteristic);
|
||||
//if (WclBleErrors.IsSuccessCode(num))
|
||||
//{
|
||||
// return;
|
||||
//}
|
||||
}
|
||||
|
||||
private static BleState StateFromNativeState(WclBleManagerStatus status)
|
||||
@@ -521,5 +541,21 @@ namespace Assets.Scripts.Ble
|
||||
base.Name = name;
|
||||
}
|
||||
}
|
||||
|
||||
private class WinBleServiceInfo : BleServiceInfo
|
||||
{
|
||||
// Token: 0x06003F86 RID: 16262 RVA: 0x000EA27F File Offset: 0x000E847F
|
||||
public WinBleServiceInfo(BlePeripheralInfo peripheral, Guid id) : base(id, peripheral)
|
||||
{
|
||||
}
|
||||
}
|
||||
private class WinBleCharacteristicInfo : BleCharacteristicInfo
|
||||
{
|
||||
// Token: 0x06003F86 RID: 16262 RVA: 0x000EA27F File Offset: 0x000E847F
|
||||
public WinBleCharacteristicInfo(Guid id, BleServiceInfo service, BleCharacteristicProperties properties) : base(id,service, properties)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
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);
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cec5e50ac67fde54196ea58f5b1b790b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -15,10 +15,12 @@ namespace Assets.Scripts.Devices
|
||||
private List<DeviceAdapter> adapters = new List<DeviceAdapter>();
|
||||
public MainDeviceAdapter()
|
||||
{
|
||||
#if !(UNITY_IOS || UNITY_ANDROID)
|
||||
this.CreateAntAdapter();
|
||||
#if UNITY_IOS || UNITY_TVOS || UNITY_ANDROID
|
||||
this.CreateBleAdapter();
|
||||
#endif
|
||||
#else
|
||||
this.CreateBleAdapter();
|
||||
this.CreateAntAdapter();
|
||||
#endif
|
||||
}
|
||||
|
||||
private void CreateAntAdapter()
|
||||
@@ -29,7 +31,7 @@ namespace Assets.Scripts.Devices
|
||||
private void CreateBleAdapter()
|
||||
{
|
||||
#if UNITY_IOS || UNITY_TVOS || UNITY_ANDROID
|
||||
adapters.Add(new BleDeviceAdapter(BleWinHwInterface.GetInterface()));
|
||||
adapters.Add(new BleDeviceAdapter(BleMobileInterface.GetInterface()));
|
||||
#else
|
||||
adapters.Add(new BleDeviceAdapter(BleWinHwInterface.GetInterface()));
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user