移动端蓝牙设备正常骑行

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

+30 -7
View File
@@ -10,14 +10,15 @@ public class BluetoothDeviceScript : MonoBehaviour
public List<string> DiscoveredDeviceList;
public Action InitializedAction;
public Action InitializedAction { get; set; }
public Action DeinitializedAction;
public Action<string> ErrorAction;
public Action<string> ServiceAddedAction;
public Action<int> BleStatusChangedAction;
public Action StartedAdvertisingAction;
public Action StoppedAdvertisingAction;
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<string, string> RetrievedConnectedPeripheralAction;
public Action<string, byte[]> PeripheralReceivedWriteDataAction;
@@ -71,6 +72,7 @@ public class BluetoothDeviceScript : MonoBehaviour
const string deviceDidUpdateValueForCharacteristic = "DidUpdateValueForCharacteristic";
const string deviceLog = "Log";
const string deviceRequestMtu = "MtuChanged";
const string bleStatusChanged = "BleStatusChanged";
public void OnBluetoothMessage (string message)
{
@@ -78,7 +80,17 @@ public class BluetoothDeviceScript : MonoBehaviour
{
char[] delim = new char[] { '~' };
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)
BluetoothLEHardwareInterface.Log(string.Format("Part: {0} - {1}", i, parts[i]));
@@ -152,11 +164,22 @@ public class BluetoothDeviceScript : MonoBehaviour
int rssi = 0;
if (!int.TryParse (parts[3], out rssi))
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
byte[] bytes = System.Convert.FromBase64String (parts[4]);
DiscoveredPeripheralWithAdvertisingInfoAction (parts[1], parts[2], rssi, bytes);
DiscoveredPeripheralWithAdvertisingInfoAction (parts[1], parts[2], rssi, uuidList.ToArray());
}
}
}
+3 -2
View File
@@ -266,7 +266,7 @@ public class BluetoothLEHardwareInterface
#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;
@@ -289,6 +289,7 @@ public class BluetoothLEHardwareInterface
if (bluetoothDeviceScript != null)
{
bluetoothDeviceScript.BleStatusChangedAction = statusAction;
bluetoothDeviceScript.InitializedAction = action;
bluetoothDeviceScript.ErrorAction = errorAction;
}
@@ -475,7 +476,7 @@ public class BluetoothLEHardwareInterface
#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 (!Application.isEditor)