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
3 changes: 2 additions & 1 deletion Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Central.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class SampleDebug_Central : MonoBehaviour, ICBCentralManagerDelegate, ICB

void Start()
{
_centralManager = new CBCentralManager(this);
var initOptions = new CBCentralInitOptions() { ShowPowerAlert = true };
_centralManager = new CBCentralManager(this, initOptions);
}

public void DidDiscoverPeripheral(CBCentralManager central, CBPeripheral peripheral, int rssi)
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CoreBluetooth.Foundation;

namespace CoreBluetooth
{
public class CBCentralInitOptions
{
public static readonly string ShowPowerAlertKey = "kCBInitOptionShowPowerAlert";
public bool? ShowPowerAlert { get; set; } = null;

public CBCentralInitOptions()
{
}

internal NSMutableDictionary ToNativeDictionary()
{
var dict = new NSMutableDictionary();
if (ShowPowerAlert.HasValue)
{
using var value = new NSNumber(ShowPowerAlert.Value);
dict.SetValue(ShowPowerAlertKey, value.Handle);
}
return dict;
}
}
}

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 @@ -42,9 +42,17 @@ public ICBCentralManagerDelegate Delegate

NativeCentralManagerProxy _nativeCentralManagerProxy;

public CBCentralManager(ICBCentralManagerDelegate centralDelegate = null)
public CBCentralManager(ICBCentralManagerDelegate centralDelegate = null, CBCentralInitOptions options = null)
{
_handle = SafeNativeCentralManagerHandle.Create();
if (options == null)
{
_handle = SafeNativeCentralManagerHandle.Create();
}
else
{
using var optionsDict = options.ToNativeDictionary();
_handle = SafeNativeCentralManagerHandle.Create(optionsDict.Handle);
}
Delegate = centralDelegate;
_nativeCentralManagerProxy = new NativeCentralManagerProxy(_handle, this);
}
Expand Down

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
@@ -0,0 +1,50 @@
using System;

namespace CoreBluetooth.Foundation
{
internal class NSMutableDictionary : IDisposable
{
public SafeNSMutableDictionaryHandle Handle { get; private set; }

public NSMutableDictionary()
{
Handle = NativeMethods.ns_mutable_dictionary_new();
}

public IntPtr GetValue(SafeNSObjectHandle key)
{
ExceptionUtils.ThrowObjectDisposedExceptionIf(Handle.IsInvalid, this);
if (key.IsInvalid)
throw new ArgumentNullException(nameof(key));

return NativeMethods.ns_mutable_dictionary_get_value(Handle, key);
}

public bool TryGetValue(SafeNSObjectHandle key, out IntPtr value)
{
value = GetValue(key);
return value != IntPtr.Zero;
}

public void SetValue(SafeNSObjectHandle key, SafeNSObjectHandle value)
{
ExceptionUtils.ThrowObjectDisposedExceptionIf(Handle.IsInvalid, this);
if (key.IsInvalid)
throw new ArgumentNullException(nameof(key));

NativeMethods.ns_mutable_dictionary_set_value(Handle, key, value);
}

public void SetValue(string key, SafeNSObjectHandle value)
{
using var nsString = new NSString(key);
SetValue(nsString.Handle, value);
}

public void Dispose()
{
if (Handle != null && !Handle.IsInvalid)
Handle.Dispose();
}
}
}

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
@@ -0,0 +1,47 @@
using System;

namespace CoreBluetooth.Foundation
{
public class NSNumber : IDisposable
{
internal SafeNSNumberHandle Handle { get; private set; }

internal NSNumber(bool value)
{
Handle = NativeMethods.ns_number_new_bool(value);
}

internal NSNumber(int value)
{
Handle = NativeMethods.ns_number_new_int(value);
}

internal NSNumber(SafeNSNumberHandle handle)
{
Handle = handle;
}

internal NSNumber(IntPtr handle)
{
Handle = new SafeNSNumberHandle(handle);
}

public bool BoolValue()
{
ExceptionUtils.ThrowObjectDisposedExceptionIf(Handle.IsInvalid, this);
return NativeMethods.ns_number_bool_value(Handle);
}

public int IntValue()
{
ExceptionUtils.ThrowObjectDisposedExceptionIf(Handle.IsInvalid, this);
return NativeMethods.ns_number_int_value(Handle);
}

public void Dispose()
{
if (Handle != null && !Handle.IsInvalid)
Handle.Dispose();
}
}
}

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
@@ -0,0 +1,61 @@
using System;
using System.Runtime.InteropServices;

namespace CoreBluetooth.Foundation
{
public class NSString : IDisposable
{
internal SafeNSStringHandle Handle { get; private set; }

public NSString(string str)
{
if (str is null)
throw new ArgumentNullException(nameof(str));

Handle = NativeMethods.ns_string_new(str);
}

internal NSString(SafeNSStringHandle handle)
{
Handle = handle;
}

internal NSString(IntPtr handle)
{
Handle = new SafeNSStringHandle(handle);
}

public int LengthOfBytesUtf8()
{
ExceptionUtils.ThrowObjectDisposedExceptionIf(Handle.IsInvalid, this);
return NativeMethods.ns_string_length_of_bytes_utf8(Handle);
}

public override string ToString()
{
ExceptionUtils.ThrowObjectDisposedExceptionIf(Handle.IsInvalid, this);
return HandleToString(Handle);
}

public void Dispose()
{
if (Handle != null && !Handle.IsInvalid)
Handle.Dispose();
}

internal static string HandleToString(SafeNSStringHandle handle)
{
if (handle.IsInvalid)
return null;

NativeMethods.ns_string_get_cstring_and_length(handle, out IntPtr ptr, out int length);
if (ptr == IntPtr.Zero)
return null;

if (length == 0)
return string.Empty;

return Marshal.PtrToStringUTF8(ptr, length);
}
}
}

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
@@ -0,0 +1,47 @@
using System;
using System.Runtime.InteropServices;

namespace CoreBluetooth.Foundation
{
internal static class NativeMethods
{
#if UNITY_IOS && !UNITY_EDITOR
const string DLL_NAME = "__Internal";
#else
const string DLL_NAME = "libCoreBluetoothForUnity";
#endif

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern void ns_object_release(IntPtr handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern SafeNSNumberHandle ns_number_new_bool([MarshalAs(UnmanagedType.I1)] bool value);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern SafeNSNumberHandle ns_number_new_int(int value);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern bool ns_number_bool_value(SafeNSNumberHandle handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern int ns_number_int_value(SafeNSNumberHandle handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern SafeNSStringHandle ns_string_new([MarshalAs(UnmanagedType.LPStr)] string str);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern int ns_string_length_of_bytes_utf8(SafeNSStringHandle handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern void ns_string_get_cstring_and_length(SafeNSStringHandle handle, out IntPtr ptr, out int length);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern SafeNSMutableDictionaryHandle ns_mutable_dictionary_new();

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr ns_mutable_dictionary_get_value(SafeNSMutableDictionaryHandle handle, SafeNSObjectHandle key);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern void ns_mutable_dictionary_set_value(SafeNSMutableDictionaryHandle handle, SafeNSObjectHandle key, SafeNSObjectHandle value);
}
}

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
@@ -0,0 +1,10 @@
using System;

namespace CoreBluetooth.Foundation
{
internal class SafeNSMutableDictionaryHandle : SafeNSObjectHandle
{
public SafeNSMutableDictionaryHandle() : base() { }
public SafeNSMutableDictionaryHandle(IntPtr handle) : base(handle) { }
}
}

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
@@ -0,0 +1,10 @@
using System;

namespace CoreBluetooth.Foundation
{
internal class SafeNSNumberHandle : SafeNSObjectHandle
{
public SafeNSNumberHandle() : base() { }
public SafeNSNumberHandle(IntPtr handle) : base(handle) { }
}
}

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

Loading