diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Plugins/iOS/CoreBluetoothForUnity.framework/CoreBluetoothForUnity b/Packages/com.teach310.core-bluetooth-for-unity/Plugins/iOS/CoreBluetoothForUnity.framework/CoreBluetoothForUnity index 4d89332..cf3438e 100755 Binary files a/Packages/com.teach310.core-bluetooth-for-unity/Plugins/iOS/CoreBluetoothForUnity.framework/CoreBluetoothForUnity and b/Packages/com.teach310.core-bluetooth-for-unity/Plugins/iOS/CoreBluetoothForUnity.framework/CoreBluetoothForUnity differ diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Plugins/macOS/libCoreBluetoothForUnity.dylib b/Packages/com.teach310.core-bluetooth-for-unity/Plugins/macOS/libCoreBluetoothForUnity.dylib index aacb7ce..11dcae2 100755 Binary files a/Packages/com.teach310.core-bluetooth-for-unity/Plugins/macOS/libCoreBluetoothForUnity.dylib and b/Packages/com.teach310.core-bluetooth-for-unity/Plugins/macOS/libCoreBluetoothForUnity.dylib differ diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBATTRequest.cs b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBATTRequest.cs index 57ac96b..44008f3 100644 --- a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBATTRequest.cs +++ b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBATTRequest.cs @@ -1,4 +1,5 @@ using System; +using CoreBluetooth.Foundation; namespace CoreBluetooth { @@ -59,6 +60,8 @@ internal CBATTRequest(SafeNativeATTRequestHandle handle, NativeATTRequestProxy n _nativeATTRequest = nativeATTRequest; } + public override string ToString() => NSObject.ToString(this, Handle); + public void Dispose() { if (_disposed) return; diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentral.cs b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentral.cs index 045ab22..93fe438 100644 --- a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentral.cs +++ b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentral.cs @@ -1,4 +1,5 @@ using System; +using CoreBluetooth.Foundation; namespace CoreBluetooth { @@ -41,10 +42,7 @@ internal CBCentral(SafeNativeCentralHandle nativeCentral) _nativeCentral = new NativeCentralProxy(Handle); } - public override string ToString() - { - return $"CBCentral: identifier = {Identifier}, maximumUpdateValueLength = {MaximumUpdateValueLength}"; - } + public override string ToString() => NSObject.ToString(this, Handle); public void Dispose() { diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralManager.cs b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralManager.cs index ec54ff1..f9c6d40 100644 --- a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralManager.cs +++ b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralManager.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Threading; namespace CoreBluetooth diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBMutableCharacteristic.cs b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBMutableCharacteristic.cs index 47b318f..7e090a5 100644 --- a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBMutableCharacteristic.cs +++ b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBMutableCharacteristic.cs @@ -1,4 +1,5 @@ using System; +using CoreBluetooth.Foundation; namespace CoreBluetooth { @@ -65,12 +66,7 @@ CBAttributePermissions permissions _nativeMutableCharacteristic = new NativeMutableCharacteristicProxy(Handle); } - public override string ToString() - { - var valueText = Value == null ? "null" : $"{{length = {Value.Length}, bytes = {BitConverter.ToString(Value).Replace("-", "")}}}"; - var notifyingText = IsNotifying ? "YES" : "NO"; - return $"CBMutableCharacteristic: UUID = {UUID}, properties = {Properties}, value = {valueText}, notifying = {notifyingText}, permissions = {Permissions}"; - } + public override string ToString() => NSObject.ToString(this, Handle); public void Dispose() { diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBMutableService.cs b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBMutableService.cs index 4c38e04..d7ec6dd 100644 --- a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBMutableService.cs +++ b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBMutableService.cs @@ -1,4 +1,5 @@ using System; +using CoreBluetooth.Foundation; namespace CoreBluetooth { @@ -31,10 +32,7 @@ public CBMutableService(string uuid, bool isPrimary) : base(uuid) _nativeMutableService = new NativeMutableServiceProxy(Handle); } - public override string ToString() - { - return $"CBMutableService: UUID = {UUID}"; - } + public override string ToString() => NSObject.ToString(this, Handle); public void Dispose() { diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/AnyObject.cs b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/AnyObject.cs index 66921d9..97f26b8 100644 --- a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/AnyObject.cs +++ b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/AnyObject.cs @@ -11,5 +11,21 @@ public static void Release(IntPtr handle) NativeMethods.any_object_release(handle); } } + + public static string ToString(IntPtr handle) + { + if (handle == IntPtr.Zero) + { + return string.Empty; + } + + using var description = GetDescription(handle); + return description.ToString(); + } + + internal static NSString GetDescription(IntPtr handle) + { + return new NSString(NativeMethods.any_object_to_string(handle)); + } } } diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/NSObject.cs b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/NSObject.cs new file mode 100644 index 0000000..eaf5220 --- /dev/null +++ b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/NSObject.cs @@ -0,0 +1,33 @@ +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Text.RegularExpressions; + +namespace CoreBluetooth.Foundation +{ + public class NSObject + { + public static string ToString(T obj, SafeHandle handle) + { + return ToString(obj, handle.DangerousGetHandle()); + } + + public static string ToString(T obj, IntPtr handle) + { + if (handle == IntPtr.Zero) + { + return typeof(T).Name; + } + + using var description = AnyObject.GetDescription(handle); + string pattern = @"^<.*?:\s+0x[0-9a-f]+|>$"; + var content = Regex.Replace(description.ToString(), pattern, ""); + if (content == string.Empty) + { + return typeof(T).Name; + } + + return $"{obj.GetType().Name}:{content}"; + } + } +} diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/NSObject.cs.meta b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/NSObject.cs.meta new file mode 100644 index 0000000..a2cd121 --- /dev/null +++ b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/NSObject.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a375413ddc389454ebc3f5391154e508 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/NativeMethods.cs b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/NativeMethods.cs index 3751f03..4b8f4e9 100644 --- a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/NativeMethods.cs +++ b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/NativeMethods.cs @@ -14,6 +14,9 @@ internal static class NativeMethods [DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)] internal static extern void any_object_release(IntPtr handle); + [DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)] + internal static extern SafeNSStringHandle any_object_to_string(IntPtr handle); + [DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)] internal static extern SafeNSNumberHandle ns_number_new_bool([MarshalAs(UnmanagedType.I1)] bool value); diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Tests/Runtime/CBMutableCharacteristicTests.cs b/Packages/com.teach310.core-bluetooth-for-unity/Tests/Runtime/CBMutableCharacteristicTests.cs index 3045ad4..7e18cf7 100644 --- a/Packages/com.teach310.core-bluetooth-for-unity/Tests/Runtime/CBMutableCharacteristicTests.cs +++ b/Packages/com.teach310.core-bluetooth-for-unity/Tests/Runtime/CBMutableCharacteristicTests.cs @@ -61,7 +61,7 @@ public void Permissions() public void ToString_Output() { using var characteristic = new CBMutableCharacteristic(validUUID1, CBCharacteristicProperties.Broadcast, null, CBAttributePermissions.Readable); - Assert.That(characteristic.ToString(), Is.EqualTo($"CBMutableCharacteristic: UUID = {validUUID1}, properties = Broadcast, value = null, notifying = NO, permissions = Readable")); + Assert.That(characteristic.ToString(), Is.EqualTo($"CBMutableCharacteristic: UUID = {validUUID1}, Value = (null), Properties = 0x1, Permissions = 0x1, Descriptors = (null), SubscribedCentrals = (\n)")); } #endif } diff --git a/Packages/com.teach310.core-bluetooth-for-unity/package.json b/Packages/com.teach310.core-bluetooth-for-unity/package.json index cfe5159..bdd5ff6 100644 --- a/Packages/com.teach310.core-bluetooth-for-unity/package.json +++ b/Packages/com.teach310.core-bluetooth-for-unity/package.json @@ -1,6 +1,6 @@ { "name": "com.teach310.core-bluetooth-for-unity", - "version": "0.4.6", + "version": "0.4.7", "displayName": "CoreBluetooth", "description": "Provides native Apple CoreBluetooth integration for use with Unity.", "unity": "2022.3", diff --git a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UATTRequest.swift b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UATTRequest.swift index f952e9b..f673649 100644 --- a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UATTRequest.swift +++ b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UATTRequest.swift @@ -32,3 +32,9 @@ public class CB4UATTRequest { return request.offset } } + +extension CB4UATTRequest: CustomStringConvertible { + public var description: String { + return request.description + } +} diff --git a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UCentral.swift b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UCentral.swift index fb78a79..e220ac9 100644 --- a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UCentral.swift +++ b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UCentral.swift @@ -15,3 +15,9 @@ public class CB4UCentral { return central.maximumUpdateValueLength } } + +extension CB4UCentral: CustomStringConvertible { + public var description: String { + return central.description + } +} diff --git a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UCentralManager.swift b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UCentralManager.swift index 4a9cd2d..0ae4339 100644 --- a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UCentralManager.swift +++ b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UCentralManager.swift @@ -41,7 +41,7 @@ public class CB4UCentralManager : NSObject { public func cancelPeripheralConnection(peripheral: CB4UPeripheral) { centralManager.cancelPeripheralConnection(peripheral.peripheral) } - + public func retrievePeripherals(withIdentifiers identifiers: [UUID]) -> NSMutableArray { let peripherals = centralManager.retrievePeripherals(withIdentifiers: identifiers) let array = NSMutableArray() @@ -64,6 +64,10 @@ public class CB4UCentralManager : NSObject { public var isScanning: Bool { return centralManager.isScanning } + + override public var description: String { + return centralManager.description + } } extension CB4UCentralManager : CBCentralManagerDelegate { diff --git a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UMutableCharacteristic.swift b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UMutableCharacteristic.swift index 8e8e49b..ebe6ea6 100644 --- a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UMutableCharacteristic.swift +++ b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UMutableCharacteristic.swift @@ -11,7 +11,7 @@ public class CB4UMutableCharacteristic { characteristic.value = newValue } } - + public var valueLength: Int { return characteristic.value?.count ?? 0 } @@ -38,3 +38,9 @@ public class CB4UMutableCharacteristic { self.characteristic = characteristic } } + +extension CB4UMutableCharacteristic: CustomStringConvertible { + public var description: String { + return characteristic.description + } +} diff --git a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UMutableService.swift b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UMutableService.swift index 089eb9c..4ad595f 100644 --- a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UMutableService.swift +++ b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UMutableService.swift @@ -15,3 +15,9 @@ public class CB4UMutableService { } } } + +extension CB4UMutableService: CustomStringConvertible { + public var description: String { + return service.description + } +} diff --git a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UPeripheral.swift b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UPeripheral.swift index dafb882..9f17761 100644 --- a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UPeripheral.swift +++ b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UPeripheral.swift @@ -105,6 +105,10 @@ public class CB4UPeripheral : NSObject { public func readRSSI() { peripheral.readRSSI() } + + override public var description: String { + return peripheral.description + } } extension CB4UPeripheral : CBPeripheralDelegate { diff --git a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UPeripheralManager.swift b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UPeripheralManager.swift index 7e6f917..9503520 100644 --- a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UPeripheralManager.swift +++ b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UPeripheralManager.swift @@ -67,6 +67,10 @@ public class CB4UPeripheralManager : NSObject { public func respond(to request: CB4UATTRequest, withResult result: CBATTError.Code) { peripheralManager.respond(to: request.request, withResult: result) } + + override public var description: String { + return peripheralManager.description + } } extension CB4UPeripheralManager : CBPeripheralManagerDelegate { diff --git a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/FoundationForUnity/FoundationForUnity.swift b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/FoundationForUnity/FoundationForUnity.swift index 8ed0c31..228a561 100644 --- a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/FoundationForUnity/FoundationForUnity.swift +++ b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/FoundationForUnity/FoundationForUnity.swift @@ -5,6 +5,14 @@ public func any_object_release(_ handle: UnsafeRawPointer) { Unmanaged.fromOpaque(handle).release() } +@_cdecl("any_object_to_string") +public func any_object_to_string(_ handle: UnsafeRawPointer) -> UnsafeMutableRawPointer { + let instance = Unmanaged.fromOpaque(handle).takeUnretainedValue() + let str = String(describing: instance) + let nsstring = str as NSString + return Unmanaged.passRetained(nsstring).toOpaque() +} + @_cdecl("ns_number_new_bool") public func ns_number_new_bool(_ value: Bool) -> UnsafeMutableRawPointer { let instance = NSNumber(value: value)