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
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace CoreBluetooth
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using UnityEngine;

namespace CoreBluetooth
{
internal class Initializer
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Initialize()
{
ServiceLocator.Instance.Register<INativeCentralManagerCallbacks>(new NativeCentralManagerCallbacks());
ServiceLocator.Instance.Register<INativePeripheralManagerCallbacks>(new NativePeripheralManagerCallbacks());
ServiceLocator.Instance.Register<INativePeripheralCallbacks>(new NativePeripheralCallbacks());
}
}
}

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

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,73 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace CoreBluetooth
{
internal class NativeCentralManagerCallbacks : INativeCentralManagerCallbacks
{
readonly static Dictionary<IntPtr, INativeCentralManagerDelegate> s_centralManagerDelegateMap = new Dictionary<IntPtr, INativeCentralManagerDelegate>();

public void Register(SafeNativeCentralManagerHandle handle, INativeCentralManagerDelegate centralManagerDelegate)
{
s_centralManagerDelegateMap[handle.DangerousGetHandle()] = centralManagerDelegate;
NativeMethods.cb4u_central_manager_register_handlers(
handle,
DidConnect,
DidDisconnectPeripheral,
DidFailToConnect,
DidDiscoverPeripheral,
DidUpdateState
);
}

public void Unregister(SafeNativeCentralManagerHandle handle)
{
s_centralManagerDelegateMap.Remove(handle.DangerousGetHandle());
}

static INativeCentralManagerDelegate GetDelegate(IntPtr centralPtr)
{
return s_centralManagerDelegateMap.GetValueOrDefault(centralPtr);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UCentralManagerDidConnectHandler))]
static void DidConnect(IntPtr centralPtr, IntPtr peripheralIdPtr)
{
GetDelegate(centralPtr)?.DidConnect(Marshal.PtrToStringUTF8(peripheralIdPtr));
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UCentralManagerDidDisconnectPeripheralHandler))]
static void DidDisconnectPeripheral(IntPtr centralPtr, IntPtr peripheralIdPtr, int errorCode)
{
GetDelegate(centralPtr)?.DidDisconnectPeripheral(
Marshal.PtrToStringUTF8(peripheralIdPtr),
CBError.CreateOrNullFromCode(errorCode)
);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UCentralManagerDidFailToConnectHandler))]
static void DidFailToConnect(IntPtr centralPtr, IntPtr peripheralIdPtr, int errorCode)
{
GetDelegate(centralPtr)?.DidFailToConnect(
Marshal.PtrToStringUTF8(peripheralIdPtr),
CBError.CreateOrNullFromCode(errorCode)
);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UCentralManagerDidDiscoverPeripheralHandler))]
static void DidDiscoverPeripheral(IntPtr centralPtr, IntPtr peripheralPtr, int rssi)
{
GetDelegate(centralPtr)?.DidDiscoverPeripheral(
new SafeNativePeripheralHandle(peripheralPtr),
rssi
);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UCentralManagerDidUpdateStateHandler))]
static void DidUpdateState(IntPtr centralPtr, CBManagerState state)
{
GetDelegate(centralPtr)?.DidUpdateState(state);
}
}
}

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,124 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace CoreBluetooth
{
internal class NativePeripheralCallbacks : INativePeripheralCallbacks
{
readonly static Dictionary<IntPtr, INativePeripheralDelegate> s_nativePeripheralDelegateMap = new Dictionary<IntPtr, INativePeripheralDelegate>();

public void Register(SafeNativePeripheralHandle handle, INativePeripheralDelegate peripheralDelegate)
{
s_nativePeripheralDelegateMap[handle.DangerousGetHandle()] = peripheralDelegate;
NativeMethods.cb4u_peripheral_register_handlers(
handle,
DidDiscoverServices,
DidDiscoverCharacteristics,
DidUpdateValueForCharacteristic,
DidWriteValueForCharacteristic,
IsReadyToSendWriteWithoutResponse,
DidUpdateNotificationStateForCharacteristic,
DidReadRSSI,
DidUpdateName,
DidModifyServices
);
}

public void Unregister(SafeNativePeripheralHandle handle)
{
s_nativePeripheralDelegateMap.Remove(handle.DangerousGetHandle());
}

static INativePeripheralDelegate GetDelegate(IntPtr peripheralPtr)
{
return s_nativePeripheralDelegateMap.GetValueOrDefault(peripheralPtr);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralDidDiscoverServicesHandler))]
public static void DidDiscoverServices(IntPtr peripheralPtr, IntPtr commaSeparatedServiceUUIDsPtr, int errorCode)
{
string commaSeparatedServiceUUIDs = Marshal.PtrToStringUTF8(commaSeparatedServiceUUIDsPtr);
GetDelegate(peripheralPtr)?.DidDiscoverServices(
commaSeparatedServiceUUIDs.Split(','),
CBError.CreateOrNullFromCode(errorCode)
);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralDidDiscoverCharacteristicsHandler))]
public static void DidDiscoverCharacteristics(IntPtr peripheralPtr, IntPtr serviceUUIDPtr, IntPtr commaSeparatedCharacteristicUUIDsPtr, int errorCode)
{
string commaSeparatedCharacteristicUUIDs = Marshal.PtrToStringUTF8(commaSeparatedCharacteristicUUIDsPtr);
GetDelegate(peripheralPtr)?.DidDiscoverCharacteristics(
Marshal.PtrToStringUTF8(serviceUUIDPtr),
commaSeparatedCharacteristicUUIDs.Split(','),
CBError.CreateOrNullFromCode(errorCode)
);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralDidUpdateValueForCharacteristicHandler))]
public static void DidUpdateValueForCharacteristic(IntPtr peripheralPtr, IntPtr serviceUUIDPtr, IntPtr characteristicUUIDPtr, IntPtr dataPtr, int dataLength, int errorCode)
{
var dataBytes = new byte[dataLength];
Marshal.Copy(dataPtr, dataBytes, 0, dataLength);

GetDelegate(peripheralPtr)?.DidUpdateValueForCharacteristic(
Marshal.PtrToStringUTF8(serviceUUIDPtr),
Marshal.PtrToStringUTF8(characteristicUUIDPtr),
dataBytes,
CBError.CreateOrNullFromCode(errorCode)
);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralDidWriteValueForCharacteristicHandler))]
public static void DidWriteValueForCharacteristic(IntPtr peripheralPtr, IntPtr serviceUUIDPtr, IntPtr characteristicUUIDPtr, int errorCode)
{
GetDelegate(peripheralPtr)?.DidWriteValueForCharacteristic(
Marshal.PtrToStringUTF8(serviceUUIDPtr),
Marshal.PtrToStringUTF8(characteristicUUIDPtr),
CBError.CreateOrNullFromCode(errorCode)
);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralIsReadyToSendWriteWithoutResponseHandler))]
public static void IsReadyToSendWriteWithoutResponse(IntPtr peripheralPtr)
{
GetDelegate(peripheralPtr)?.IsReadyToSendWriteWithoutResponse();
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralDidUpdateNotificationStateForCharacteristicHandler))]
public static void DidUpdateNotificationStateForCharacteristic(IntPtr peripheralPtr, IntPtr serviceUUIDPtr, IntPtr characteristicUUIDPtr, int notificationState, int errorCode)
{
GetDelegate(peripheralPtr)?.DidUpdateNotificationStateForCharacteristic(
Marshal.PtrToStringUTF8(serviceUUIDPtr),
Marshal.PtrToStringUTF8(characteristicUUIDPtr),
notificationState == 1,
CBError.CreateOrNullFromCode(errorCode)
);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralDidReadRSSIHandler))]
public static void DidReadRSSI(IntPtr peripheralPtr, int rssi, int errorCode)
{
GetDelegate(peripheralPtr)?.DidReadRSSI(
rssi,
CBError.CreateOrNullFromCode(errorCode)
);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralDidUpdateNameHandler))]
public static void DidUpdateName(IntPtr peripheralPtr)
{
GetDelegate(peripheralPtr)?.DidUpdateName();
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralDidModifyServicesHandler))]
public static void DidModifyServices(IntPtr peripheralPtr, IntPtr commaSeparatedServiceUUIDsPtr)
{
string commaSeparatedServiceUUIDs = Marshal.PtrToStringUTF8(commaSeparatedServiceUUIDsPtr);
GetDelegate(peripheralPtr)?.DidModifyServices(
commaSeparatedServiceUUIDs.Split(',')
);
}
}
}

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,100 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace CoreBluetooth
{
internal class NativePeripheralManagerCallbacks : INativePeripheralManagerCallbacks
{
readonly static Dictionary<IntPtr, INativePeripheralManagerDelegate> s_peripheralManagerDelegateMap = new Dictionary<IntPtr, INativePeripheralManagerDelegate>();

public void Register(SafeNativePeripheralManagerHandle handle, INativePeripheralManagerDelegate peripheralManagerDelegate)
{
s_peripheralManagerDelegateMap[handle.DangerousGetHandle()] = peripheralManagerDelegate;
NativeMethods.cb4u_peripheral_manager_register_handlers(
handle,
DidUpdateState,
DidAddService,
DidStartAdvertising,
DidSubscribeToCharacteristic,
DidUnsubscribeFromCharacteristic,
IsReadyToUpdateSubscribers,
DidReceiveReadRequest,
DidReceiveWriteRequests
);
}

public void Unregister(SafeNativePeripheralManagerHandle handle)
{
s_peripheralManagerDelegateMap.Remove(handle.DangerousGetHandle());
}

static INativePeripheralManagerDelegate GetDelegate(IntPtr peripheralPtr)
{
return s_peripheralManagerDelegateMap.GetValueOrDefault(peripheralPtr);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralManagerDidUpdateStateHandler))]
static void DidUpdateState(IntPtr peripheralPtr, CBManagerState state)
{
GetDelegate(peripheralPtr)?.DidUpdateState(state);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralManagerDidAddServiceHandler))]
static void DidAddService(IntPtr peripheralPtr, IntPtr serviceUUIDPtr, int errorCode)
{
GetDelegate(peripheralPtr)?.DidAddService(
Marshal.PtrToStringUTF8(serviceUUIDPtr),
CBError.CreateOrNullFromCode(errorCode)
);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralManagerDidStartAdvertisingHandler))]
static void DidStartAdvertising(IntPtr peripheralPtr, int errorCode)
{
GetDelegate(peripheralPtr)?.DidStartAdvertising(CBError.CreateOrNullFromCode(errorCode));
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralManagerDidSubscribeToCharacteristicHandler))]
static void DidSubscribeToCharacteristic(IntPtr peripheralPtr, IntPtr centralPtr, IntPtr serviceUUIDPtr, IntPtr characteristicUUIDPtr)
{
GetDelegate(peripheralPtr)?.DidSubscribeToCharacteristic(
new SafeNativeCentralHandle(centralPtr),
Marshal.PtrToStringUTF8(serviceUUIDPtr),
Marshal.PtrToStringUTF8(characteristicUUIDPtr)
);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralManagerDidUnsubscribeFromCharacteristicHandler))]
static void DidUnsubscribeFromCharacteristic(IntPtr peripheralPtr, IntPtr centralPtr, IntPtr serviceUUIDPtr, IntPtr characteristicUUIDPtr)
{
GetDelegate(peripheralPtr)?.DidUnsubscribeFromCharacteristic(
new SafeNativeCentralHandle(centralPtr),
Marshal.PtrToStringUTF8(serviceUUIDPtr),
Marshal.PtrToStringUTF8(characteristicUUIDPtr)
);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralManagerIsReadyToUpdateSubscribersHandler))]
static void IsReadyToUpdateSubscribers(IntPtr peripheralPtr)
{
GetDelegate(peripheralPtr)?.IsReadyToUpdateSubscribers();
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralManagerDidReceiveReadRequestHandler))]
static void DidReceiveReadRequest(IntPtr peripheralPtr, IntPtr requestPtr)
{
GetDelegate(peripheralPtr)?.DidReceiveReadRequest(
new SafeNativeATTRequestHandle(requestPtr)
);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralManagerDidReceiveWriteRequestsHandler))]
static void DidReceiveWriteRequests(IntPtr peripheralPtr, IntPtr requestsPtr)
{
GetDelegate(peripheralPtr)?.DidReceiveWriteRequests(
new SafeNativeATTRequestsHandle(requestsPtr)
);
}
}
}

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

Loading