Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CoreBluetooth.Foundation;

namespace CoreBluetooth
{
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CoreBluetooth.Foundation;

namespace CoreBluetooth
{
Expand Down Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace CoreBluetooth
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CoreBluetooth.Foundation;

namespace CoreBluetooth
{
Expand Down Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CoreBluetooth.Foundation;

namespace CoreBluetooth
{
Expand Down Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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>(T obj, SafeHandle handle)
{
return ToString(obj, handle.DangerousGetHandle());
}

public static string ToString<T>(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}";
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ public class CB4UATTRequest {
return request.offset
}
}

extension CB4UATTRequest: CustomStringConvertible {
public var description: String {
return request.description
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ public class CB4UCentral {
return central.maximumUpdateValueLength
}
}

extension CB4UCentral: CustomStringConvertible {
public var description: String {
return central.description
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class CB4UMutableCharacteristic {
characteristic.value = newValue
}
}

public var valueLength: Int {
return characteristic.value?.count ?? 0
}
Expand All @@ -38,3 +38,9 @@ public class CB4UMutableCharacteristic {
self.characteristic = characteristic
}
}

extension CB4UMutableCharacteristic: CustomStringConvertible {
public var description: String {
return characteristic.description
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ public class CB4UMutableService {
}
}
}

extension CB4UMutableService: CustomStringConvertible {
public var description: String {
return service.description
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public class CB4UPeripheral : NSObject {
public func readRSSI() {
peripheral.readRSSI()
}

override public var description: String {
return peripheral.description
}
}

extension CB4UPeripheral : CBPeripheralDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ public func any_object_release(_ handle: UnsafeRawPointer) {
Unmanaged<AnyObject>.fromOpaque(handle).release()
}

@_cdecl("any_object_to_string")
public func any_object_to_string(_ handle: UnsafeRawPointer) -> UnsafeMutableRawPointer {
let instance = Unmanaged<AnyObject>.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)
Expand Down