Skip to content

Commit da2e67b

Browse files
authored
Merge pull request #51 from teach310/feature/central_manager_init_options
Feature/central manager init options
2 parents 0e4ba1b + d84a533 commit da2e67b

36 files changed

Lines changed: 696 additions & 10 deletions

Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Central.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public class SampleDebug_Central : MonoBehaviour, ICBCentralManagerDelegate, ICB
1818

1919
void Start()
2020
{
21-
_centralManager = new CBCentralManager(this);
21+
var initOptions = new CBCentralInitOptions() { ShowPowerAlert = true };
22+
_centralManager = new CBCentralManager(this, initOptions);
2223
}
2324

2425
public void DidDiscoverPeripheral(CBCentralManager central, CBPeripheral peripheral, int rssi)
Binary file not shown.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using CoreBluetooth.Foundation;
2+
3+
namespace CoreBluetooth
4+
{
5+
public class CBCentralInitOptions
6+
{
7+
public static readonly string ShowPowerAlertKey = "kCBInitOptionShowPowerAlert";
8+
public bool? ShowPowerAlert { get; set; } = null;
9+
10+
public CBCentralInitOptions()
11+
{
12+
}
13+
14+
internal NSMutableDictionary ToNativeDictionary()
15+
{
16+
var dict = new NSMutableDictionary();
17+
if (ShowPowerAlert.HasValue)
18+
{
19+
using var value = new NSNumber(ShowPowerAlert.Value);
20+
dict.SetValue(ShowPowerAlertKey, value.Handle);
21+
}
22+
return dict;
23+
}
24+
}
25+
}

Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralInitOptions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralManager.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,17 @@ public ICBCentralManagerDelegate Delegate
4242

4343
NativeCentralManagerProxy _nativeCentralManagerProxy;
4444

45-
public CBCentralManager(ICBCentralManagerDelegate centralDelegate = null)
45+
public CBCentralManager(ICBCentralManagerDelegate centralDelegate = null, CBCentralInitOptions options = null)
4646
{
47-
_handle = SafeNativeCentralManagerHandle.Create();
47+
if (options == null)
48+
{
49+
_handle = SafeNativeCentralManagerHandle.Create();
50+
}
51+
else
52+
{
53+
using var optionsDict = options.ToNativeDictionary();
54+
_handle = SafeNativeCentralManagerHandle.Create(optionsDict.Handle);
55+
}
4856
Delegate = centralDelegate;
4957
_nativeCentralManagerProxy = new NativeCentralManagerProxy(_handle, this);
5058
}

Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
3+
namespace CoreBluetooth.Foundation
4+
{
5+
internal class NSMutableDictionary : IDisposable
6+
{
7+
public SafeNSMutableDictionaryHandle Handle { get; private set; }
8+
9+
public NSMutableDictionary()
10+
{
11+
Handle = NativeMethods.ns_mutable_dictionary_new();
12+
}
13+
14+
public IntPtr GetValue(SafeNSObjectHandle key)
15+
{
16+
ExceptionUtils.ThrowObjectDisposedExceptionIf(Handle.IsInvalid, this);
17+
if (key.IsInvalid)
18+
throw new ArgumentNullException(nameof(key));
19+
20+
return NativeMethods.ns_mutable_dictionary_get_value(Handle, key);
21+
}
22+
23+
public bool TryGetValue(SafeNSObjectHandle key, out IntPtr value)
24+
{
25+
value = GetValue(key);
26+
return value != IntPtr.Zero;
27+
}
28+
29+
public void SetValue(SafeNSObjectHandle key, SafeNSObjectHandle value)
30+
{
31+
ExceptionUtils.ThrowObjectDisposedExceptionIf(Handle.IsInvalid, this);
32+
if (key.IsInvalid)
33+
throw new ArgumentNullException(nameof(key));
34+
35+
NativeMethods.ns_mutable_dictionary_set_value(Handle, key, value);
36+
}
37+
38+
public void SetValue(string key, SafeNSObjectHandle value)
39+
{
40+
using var nsString = new NSString(key);
41+
SetValue(nsString.Handle, value);
42+
}
43+
44+
public void Dispose()
45+
{
46+
if (Handle != null && !Handle.IsInvalid)
47+
Handle.Dispose();
48+
}
49+
}
50+
}

Packages/com.teach310.core-bluetooth-for-unity/Runtime/Foundation/NSMutableDictionary.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
3+
namespace CoreBluetooth.Foundation
4+
{
5+
public class NSNumber : IDisposable
6+
{
7+
internal SafeNSNumberHandle Handle { get; private set; }
8+
9+
internal NSNumber(bool value)
10+
{
11+
Handle = NativeMethods.ns_number_new_bool(value);
12+
}
13+
14+
internal NSNumber(int value)
15+
{
16+
Handle = NativeMethods.ns_number_new_int(value);
17+
}
18+
19+
internal NSNumber(SafeNSNumberHandle handle)
20+
{
21+
Handle = handle;
22+
}
23+
24+
internal NSNumber(IntPtr handle)
25+
{
26+
Handle = new SafeNSNumberHandle(handle);
27+
}
28+
29+
public bool BoolValue()
30+
{
31+
ExceptionUtils.ThrowObjectDisposedExceptionIf(Handle.IsInvalid, this);
32+
return NativeMethods.ns_number_bool_value(Handle);
33+
}
34+
35+
public int IntValue()
36+
{
37+
ExceptionUtils.ThrowObjectDisposedExceptionIf(Handle.IsInvalid, this);
38+
return NativeMethods.ns_number_int_value(Handle);
39+
}
40+
41+
public void Dispose()
42+
{
43+
if (Handle != null && !Handle.IsInvalid)
44+
Handle.Dispose();
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)