|
@@ -13,16 +13,16 @@ namespace SmartBowSDK
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
public class BleWinHelper : MonoBehaviour
|
|
public class BleWinHelper : MonoBehaviour
|
|
|
{
|
|
{
|
|
|
- private static string LogTag = "BleWinHelper-Log: ";
|
|
|
|
|
- private static void Log(string text)
|
|
|
|
|
|
|
+ public string LogTag = "BleWinHelper-Log: ";
|
|
|
|
|
+ private void Log(string text)
|
|
|
{
|
|
{
|
|
|
Debug.Log(LogTag + text);
|
|
Debug.Log(LogTag + text);
|
|
|
}
|
|
}
|
|
|
- private static void Warn(string text)
|
|
|
|
|
|
|
+ private void Warn(string text)
|
|
|
{
|
|
{
|
|
|
Debug.LogWarning(LogTag + text);
|
|
Debug.LogWarning(LogTag + text);
|
|
|
}
|
|
}
|
|
|
- private static void Error(string text)
|
|
|
|
|
|
|
+ private void Error(string text)
|
|
|
{
|
|
{
|
|
|
Debug.Log(LogTag + text);
|
|
Debug.Log(LogTag + text);
|
|
|
}
|
|
}
|
|
@@ -40,7 +40,7 @@ namespace SmartBowSDK
|
|
|
private bool isSubscribing = false;
|
|
private bool isSubscribing = false;
|
|
|
|
|
|
|
|
private string lastError = null;
|
|
private string lastError = null;
|
|
|
-
|
|
|
|
|
|
|
+ [SerializeField]
|
|
|
private string selectedDeviceId = null;
|
|
private string selectedDeviceId = null;
|
|
|
|
|
|
|
|
private Dictionary<string, Dictionary<string, string>> deviceList = new Dictionary<string, Dictionary<string, string>>();
|
|
private Dictionary<string, Dictionary<string, string>> deviceList = new Dictionary<string, Dictionary<string, string>>();
|
|
@@ -51,12 +51,13 @@ namespace SmartBowSDK
|
|
|
private float _receiveDataTime = 0;
|
|
private float _receiveDataTime = 0;
|
|
|
private float _heartBeatInterval = 0;
|
|
private float _heartBeatInterval = 0;
|
|
|
|
|
|
|
|
|
|
+
|
|
|
private Action OnConnected;
|
|
private Action OnConnected;
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
/// 主动调用Disconnect()不会触发该委托
|
|
/// 主动调用Disconnect()不会触发该委托
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
private Action OnConnectionFailed;
|
|
private Action OnConnectionFailed;
|
|
|
- private Action<byte[]> OnCharacteristicChanged;
|
|
|
|
|
|
|
+ private static Action<string, byte[]> OnCharacteristicChanged;
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
/// 注册window对象
|
|
/// 注册window对象
|
|
@@ -75,17 +76,24 @@ namespace SmartBowSDK
|
|
|
|
|
|
|
|
GameObject obj = new GameObject("BleWinHelper"+ logTip);
|
|
GameObject obj = new GameObject("BleWinHelper"+ logTip);
|
|
|
obj.transform.SetParent(o.transform);
|
|
obj.transform.SetParent(o.transform);
|
|
|
|
|
+
|
|
|
|
|
+ BleWinHelper bleWinHelper = obj.AddComponent<BleWinHelper>();
|
|
|
//日志名字
|
|
//日志名字
|
|
|
- LogTag = "BleWinHelper-" + logTip + ": ";
|
|
|
|
|
-
|
|
|
|
|
- BleWinHelper comp = obj.AddComponent<BleWinHelper>();
|
|
|
|
|
- bluetoothWindows.Connect = comp.Connect;
|
|
|
|
|
- bluetoothWindows.Disconnect = comp.Disconnect;
|
|
|
|
|
- bluetoothWindows.Write = comp.Write;
|
|
|
|
|
- comp.OnConnected = () => bluetoothWindows.OnConnected?.Invoke();
|
|
|
|
|
- comp.OnConnectionFailed = () => bluetoothWindows.OnConnectionFailed?.Invoke();
|
|
|
|
|
- comp.OnCharacteristicChanged = (bytes) => bluetoothWindows.OnCharacteristicChanged?.Invoke(bytes);
|
|
|
|
|
- return comp;
|
|
|
|
|
|
|
+ bleWinHelper.LogTag = "BleWinHelper-" + logTip + ": ";
|
|
|
|
|
+ bluetoothWindows.Connect = bleWinHelper.Connect;
|
|
|
|
|
+ bluetoothWindows.Disconnect = bleWinHelper.Disconnect;
|
|
|
|
|
+ bluetoothWindows.Write = bleWinHelper.Write;
|
|
|
|
|
+ bleWinHelper.OnConnected = () => bluetoothWindows.OnConnected?.Invoke();
|
|
|
|
|
+ bleWinHelper.OnConnectionFailed = () => bluetoothWindows.OnConnectionFailed?.Invoke();
|
|
|
|
|
+ //多个定义共用一个OnCharacteristicChanged
|
|
|
|
|
+ OnCharacteristicChanged += (deviceID,bytes) => {
|
|
|
|
|
+ if (deviceID == bleWinHelper.selectedDeviceId) {
|
|
|
|
|
+
|
|
|
|
|
+ bluetoothWindows.OnCharacteristicChanged?.Invoke(deviceID, bytes);
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ return bleWinHelper;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -201,7 +209,8 @@ namespace SmartBowSDK
|
|
|
byte[] bytes = new byte[res.size];
|
|
byte[] bytes = new byte[res.size];
|
|
|
Array.Copy(res.buf, bytes, res.size);
|
|
Array.Copy(res.buf, bytes, res.size);
|
|
|
_receiveDataTime = Time.realtimeSinceStartup;
|
|
_receiveDataTime = Time.realtimeSinceStartup;
|
|
|
- OnCharacteristicChanged?.Invoke(bytes);
|
|
|
|
|
|
|
+ OnCharacteristicChanged?.Invoke(res.deviceId, bytes);
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
{
|
|
{
|
|
@@ -211,7 +220,8 @@ namespace SmartBowSDK
|
|
|
{
|
|
{
|
|
|
Error(res.msg);
|
|
Error(res.msg);
|
|
|
lastError = res.msg;
|
|
lastError = res.msg;
|
|
|
- if (lastError.Contains("SendDataAsync") && isSubscribed)
|
|
|
|
|
|
|
+ //对应设备才断开
|
|
|
|
|
+ if (lastError.Contains("SendDataAsync") && lastError.Contains(selectedDeviceId) && isSubscribed)
|
|
|
{
|
|
{
|
|
|
HandleConnectFail();
|
|
HandleConnectFail();
|
|
|
}
|
|
}
|
|
@@ -222,11 +232,15 @@ namespace SmartBowSDK
|
|
|
void LateUpdate()
|
|
void LateUpdate()
|
|
|
{
|
|
{
|
|
|
if (
|
|
if (
|
|
|
- _heartBeatInterval > 0 &&
|
|
|
|
|
- isSubscribed &&
|
|
|
|
|
|
|
+ _heartBeatInterval > 0 &&
|
|
|
|
|
+ isSubscribed &&
|
|
|
Time.realtimeSinceStartup - _connectedTime >= _heartBeatInterval &&
|
|
Time.realtimeSinceStartup - _connectedTime >= _heartBeatInterval &&
|
|
|
Time.realtimeSinceStartup - _receiveDataTime >= _heartBeatInterval
|
|
Time.realtimeSinceStartup - _receiveDataTime >= _heartBeatInterval
|
|
|
- ) HandleConnectFail();
|
|
|
|
|
|
|
+ )
|
|
|
|
|
+ {
|
|
|
|
|
+ HandleConnectFail();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private bool Connect()
|
|
private bool Connect()
|
|
@@ -382,101 +396,5 @@ namespace SmartBowSDK
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- public class BleApi
|
|
|
|
|
- {
|
|
|
|
|
- // dll calls
|
|
|
|
|
- public enum ScanStatus { PROCESSING, AVAILABLE, FINISHED };
|
|
|
|
|
-
|
|
|
|
|
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
|
|
|
- public struct DeviceUpdate
|
|
|
|
|
- {
|
|
|
|
|
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
|
|
|
|
|
- public string id;
|
|
|
|
|
- [MarshalAs(UnmanagedType.I1)]
|
|
|
|
|
- public bool isConnectable;
|
|
|
|
|
- [MarshalAs(UnmanagedType.I1)]
|
|
|
|
|
- public bool isConnectableUpdated;
|
|
|
|
|
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
|
|
|
|
|
- public string name;
|
|
|
|
|
- [MarshalAs(UnmanagedType.I1)]
|
|
|
|
|
- public bool nameUpdated;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- [DllImport("BleWinrtDll.dll", EntryPoint = "StartDeviceScan")]
|
|
|
|
|
- public static extern void StartDeviceScan();
|
|
|
|
|
-
|
|
|
|
|
- [DllImport("BleWinrtDll.dll", EntryPoint = "PollDevice")]
|
|
|
|
|
- public static extern ScanStatus PollDevice(ref DeviceUpdate device, bool block);
|
|
|
|
|
-
|
|
|
|
|
- [DllImport("BleWinrtDll.dll", EntryPoint = "StopDeviceScan")]
|
|
|
|
|
- public static extern void StopDeviceScan();
|
|
|
|
|
-
|
|
|
|
|
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
|
|
|
- public struct Service
|
|
|
|
|
- {
|
|
|
|
|
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
|
|
|
|
|
- public string uuid;
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- [DllImport("BleWinrtDll.dll", EntryPoint = "ScanServices", CharSet = CharSet.Unicode)]
|
|
|
|
|
- public static extern void ScanServices(string deviceId);
|
|
|
|
|
-
|
|
|
|
|
- [DllImport("BleWinrtDll.dll", EntryPoint = "PollService")]
|
|
|
|
|
- public static extern ScanStatus PollService(out Service service, bool block);
|
|
|
|
|
-
|
|
|
|
|
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
|
|
|
- public struct Characteristic
|
|
|
|
|
- {
|
|
|
|
|
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
|
|
|
|
|
- public string uuid;
|
|
|
|
|
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
|
|
|
|
|
- public string userDescription;
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- [DllImport("BleWinrtDll.dll", EntryPoint = "ScanCharacteristics", CharSet = CharSet.Unicode)]
|
|
|
|
|
- public static extern void ScanCharacteristics(string deviceId, string serviceId);
|
|
|
|
|
-
|
|
|
|
|
- [DllImport("BleWinrtDll.dll", EntryPoint = "PollCharacteristic")]
|
|
|
|
|
- public static extern ScanStatus PollCharacteristic(out Characteristic characteristic, bool block);
|
|
|
|
|
-
|
|
|
|
|
- [DllImport("BleWinrtDll.dll", EntryPoint = "SubscribeCharacteristic", CharSet = CharSet.Unicode)]
|
|
|
|
|
- public static extern bool SubscribeCharacteristic(string deviceId, string serviceId, string characteristicId, bool block);
|
|
|
|
|
-
|
|
|
|
|
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
|
|
|
- public struct BLEData
|
|
|
|
|
- {
|
|
|
|
|
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)]
|
|
|
|
|
- public byte[] buf;
|
|
|
|
|
- [MarshalAs(UnmanagedType.I2)]
|
|
|
|
|
- public short size;
|
|
|
|
|
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
|
|
|
|
- public string deviceId;
|
|
|
|
|
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
|
|
|
|
- public string serviceUuid;
|
|
|
|
|
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
|
|
|
|
- public string characteristicUuid;
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- [DllImport("BleWinrtDll.dll", EntryPoint = "PollData")]
|
|
|
|
|
- public static extern bool PollData(out BLEData data, bool block);
|
|
|
|
|
-
|
|
|
|
|
- [DllImport("BleWinrtDll.dll", EntryPoint = "SendData")]
|
|
|
|
|
- public static extern bool SendData(in BLEData data, bool block);
|
|
|
|
|
-
|
|
|
|
|
- [DllImport("BleWinrtDll.dll", EntryPoint = "Quit")]
|
|
|
|
|
- public static extern void Quit();
|
|
|
|
|
-
|
|
|
|
|
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
|
|
|
- public struct ErrorMessage
|
|
|
|
|
- {
|
|
|
|
|
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
|
|
|
|
|
- public string msg;
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- [DllImport("BleWinrtDll.dll", EntryPoint = "GetError")]
|
|
|
|
|
- public static extern void GetError(out ErrorMessage buf);
|
|
|
|
|
-
|
|
|
|
|
- [DllImport("BleWinrtDll.dll", EntryPoint = "Disconnect", CharSet = CharSet.Unicode)]
|
|
|
|
|
- public static extern void Disconnect(string deviceId);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+
|
|
|
}
|
|
}
|