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,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace CoreBluetooth
{
Expand Down Expand Up @@ -40,6 +41,11 @@ public ICBCentralManagerDelegate Delegate
}
}

/// <summary>
/// ICBCentralManagerDelegate callbacks will be called in this context.
/// </summary>
public SynchronizationContext CallbackContext { get; set; }

NativeCentralManagerProxy _nativeCentralManagerProxy;

public CBCentralManager(ICBCentralManagerDelegate centralDelegate = null, CBCentralInitOptions options = null)
Expand All @@ -55,6 +61,7 @@ public CBCentralManager(ICBCentralManagerDelegate centralDelegate = null, CBCent
}
Delegate = centralDelegate;
_nativeCentralManagerProxy = new NativeCentralManagerProxy(_handle, this);
CallbackContext = SynchronizationContext.Current;
}

public void Connect(CBPeripheral peripheral)
Expand All @@ -76,7 +83,7 @@ public CBPeripheral[] RetrievePeripherals(params string[] peripheralIds)
var result = new CBPeripheral[peripheralHandles.Length];
for (var i = 0; i < peripheralHandles.Length; i++)
{
var peripheral = new CBPeripheral(peripheralHandles[i]);
var peripheral = new CBPeripheral(peripheralHandles[i], CallbackContext);
SetPeripheral(peripheral);
result[i] = peripheral;
}
Expand Down Expand Up @@ -126,42 +133,56 @@ void SetPeripheral(CBPeripheral peripheral)

void INativeCentralManagerDelegate.DidConnect(string peripheralId)
{
if (_disposed) return;
var peripheral = GetPeripheral(peripheralId);
if (peripheral == null) return;
Delegate?.DidConnectPeripheral(this, peripheral);
CallbackContext.Post(_ =>
{
if (_disposed) return;
var peripheral = GetPeripheral(peripheralId);
if (peripheral == null) return;
Delegate?.DidConnectPeripheral(this, peripheral);
}, null);
}

void INativeCentralManagerDelegate.DidDisconnectPeripheral(string peripheralId, CBError error)
{
if (_disposed) return;
var peripheral = GetPeripheral(peripheralId);
if (peripheral == null) return;
Delegate?.DidDisconnectPeripheral(this, peripheral, error);
CallbackContext.Post(_ =>
{
if (_disposed) return;
var peripheral = GetPeripheral(peripheralId);
if (peripheral == null) return;
Delegate?.DidDisconnectPeripheral(this, peripheral, error);
}, null);
}

void INativeCentralManagerDelegate.DidFailToConnect(string peripheralId, CBError error)
{
if (_disposed) return;
var peripheral = GetPeripheral(peripheralId);
if (peripheral == null) return;
Delegate?.DidFailToConnectPeripheral(this, peripheral, error);
CallbackContext.Post(_ =>
{
if (_disposed) return;
var peripheral = GetPeripheral(peripheralId);
if (peripheral == null) return;
Delegate?.DidFailToConnectPeripheral(this, peripheral, error);
}, null);
}

void INativeCentralManagerDelegate.DidDiscoverPeripheral(SafeNativePeripheralHandle peripheralHandle, int rssi)
{
if (_disposed) return;

var peripheral = new CBPeripheral(peripheralHandle);
SetPeripheral(peripheral);
Delegate?.DidDiscoverPeripheral(this, peripheral, rssi);
CallbackContext.Post(_ =>
{
if (_disposed) return;
var peripheral = new CBPeripheral(peripheralHandle, CallbackContext);
SetPeripheral(peripheral);
Delegate?.DidDiscoverPeripheral(this, peripheral, rssi);
}, null);
}

void INativeCentralManagerDelegate.DidUpdateState(CBManagerState state)
{
if (_disposed) return;
this.State = state;
Delegate?.DidUpdateState(this);
CallbackContext.Post(_ =>
{
if (_disposed) return;
this.State = state;
Delegate?.DidUpdateState(this);
}, null);
}

public void Dispose()
Expand Down
124 changes: 79 additions & 45 deletions Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBPeripheral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;

namespace CoreBluetooth
{
Expand Down Expand Up @@ -61,14 +62,21 @@ public string Name
}

public ICBPeripheralDelegate Delegate { get; set; }

/// <summary>
/// ICBPeripheralDelegate callbacks will be called in this context.
/// </summary>
public SynchronizationContext CallbackContext { get; set; }

List<CBService> _services = new List<CBService>();
public ReadOnlyCollection<CBService> Services { get; }

internal CBPeripheral(SafeNativePeripheralHandle nativePeripheral)
internal CBPeripheral(SafeNativePeripheralHandle nativePeripheral, SynchronizationContext callbackContext)
{
Handle = nativePeripheral;
_nativePeripheral = new NativePeripheralProxy(Handle, this);
this.Services = _services.AsReadOnly();
Services = _services.AsReadOnly();
CallbackContext = callbackContext;
}

/// <summary>
Expand Down Expand Up @@ -189,87 +197,113 @@ CBCharacteristic FindOrInitializeCharacteristic(CBService service, string charac

void INativePeripheralDelegate.DidDiscoverServices(string[] serviceUUIDs, CBError error)
{
if (_disposed) return;
var services = serviceUUIDs.Select(uuid =>
CallbackContext.Post(_ =>
{
return _services.FirstOrDefault(s => s.UUID == uuid) ?? new CBService(uuid, this);
}).ToArray();
_services.Clear();
_services.AddRange(services);
if (_disposed) return;
var services = serviceUUIDs.Select(uuid =>
{
return _services.FirstOrDefault(s => s.UUID == uuid) ?? new CBService(uuid, this);
}).ToArray();
_services.Clear();
_services.AddRange(services);

Delegate?.DidDiscoverServices(this, error);
Delegate?.DidDiscoverServices(this, error);
}, null);
}

void INativePeripheralDelegate.DidDiscoverCharacteristics(string serviceUUID, string[] characteristicUUIDs, CBError error)
{
if (_disposed) return;
var service = _services.FirstOrDefault(s => s.UUID == serviceUUID);
if (service == null) return;
var characteristics = characteristicUUIDs.Select(uuid => FindOrInitializeCharacteristic(service, uuid)).ToArray();
service.UpdateCharacteristics(characteristics);
Delegate?.DidDiscoverCharacteristics(this, service, error);
CallbackContext.Post(_ =>
{
if (_disposed) return;
var service = _services.FirstOrDefault(s => s.UUID == serviceUUID);
if (service == null) return;
var characteristics = characteristicUUIDs.Select(uuid => FindOrInitializeCharacteristic(service, uuid)).ToArray();
service.UpdateCharacteristics(characteristics);
Delegate?.DidDiscoverCharacteristics(this, service, error);
}, null);
}

void INativePeripheralDelegate.DidUpdateValueForCharacteristic(string serviceUUID, string characteristicUUID, byte[] data, CBError error)
{
if (_disposed) return;
var characteristic = FindCharacteristic(serviceUUID, characteristicUUID);
if (characteristic == null) return;
characteristic.UpdateValue(data);
Delegate?.DidUpdateValueForCharacteristic(this, characteristic, error);
CallbackContext.Post(_ =>
{
if (_disposed) return;
var characteristic = FindCharacteristic(serviceUUID, characteristicUUID);
if (characteristic == null) return;
characteristic.UpdateValue(data);
Delegate?.DidUpdateValueForCharacteristic(this, characteristic, error);
}, null);
}

void INativePeripheralDelegate.DidWriteValueForCharacteristic(string serviceUUID, string characteristicUUID, CBError error)
{
if (_disposed) return;
var characteristic = FindCharacteristic(serviceUUID, characteristicUUID);
if (characteristic == null) return;

Delegate?.DidWriteValueForCharacteristic(this, characteristic, error);
CallbackContext.Post(_ =>
{
if (_disposed) return;
var characteristic = FindCharacteristic(serviceUUID, characteristicUUID);
if (characteristic == null) return;
Delegate?.DidWriteValueForCharacteristic(this, characteristic, error);
}, null);
}

void INativePeripheralDelegate.IsReadyToSendWriteWithoutResponse()
{
if (_disposed) return;
Delegate?.IsReadyToSendWriteWithoutResponse(this);
CallbackContext.Post(_ =>
{
if (_disposed) return;
Delegate?.IsReadyToSendWriteWithoutResponse(this);
}, null);
}

void INativePeripheralDelegate.DidUpdateNotificationStateForCharacteristic(string serviceUUID, string characteristicUUID, bool isNotifying, CBError error)
{
if (_disposed) return;
var characteristic = FindCharacteristic(serviceUUID, characteristicUUID);
if (characteristic == null) return;
characteristic.UpdateIsNotifying(isNotifying);
Delegate?.DidUpdateNotificationStateForCharacteristic(this, characteristic, error);
CallbackContext.Post(_ =>
{
if (_disposed) return;
var characteristic = FindCharacteristic(serviceUUID, characteristicUUID);
if (characteristic == null) return;
characteristic.UpdateIsNotifying(isNotifying);
Delegate?.DidUpdateNotificationStateForCharacteristic(this, characteristic, error);
}, null);
}

void INativePeripheralDelegate.DidReadRSSI(int rssi, CBError error)
{
if (_disposed) return;
Delegate?.DidReadRSSI(this, rssi, error);
CallbackContext.Post(_ =>
{
if (_disposed) return;
Delegate?.DidReadRSSI(this, rssi, error);
}, null);
}

void INativePeripheralDelegate.DidUpdateName()
{
if (_disposed) return;
Delegate?.DidUpdateName(this);
CallbackContext.Post(_ =>
{
if (_disposed) return;
Delegate?.DidUpdateName(this);
}, null);
}

void INativePeripheralDelegate.DidModifyServices(string[] invalidatedServiceUUIDs)
{
if (_disposed) return;
List<CBService> invalidatedServices = new List<CBService>();
foreach (var uuid in invalidatedServiceUUIDs)
CallbackContext.Post(_ =>
{
var service = _services.FirstOrDefault(s => s.UUID == uuid);
if (service != null)
if (_disposed) return;
List<CBService> invalidatedServices = new List<CBService>();
foreach (var uuid in invalidatedServiceUUIDs)
{
invalidatedServices.Add(service);
_services.Remove(service);
var service = _services.FirstOrDefault(s => s.UUID == uuid);
if (service != null)
{
invalidatedServices.Add(service);
_services.Remove(service);
}
}
}

Delegate?.DidModifyServices(this, invalidatedServices.ToArray());
Delegate?.DidModifyServices(this, invalidatedServices.ToArray());
}, null);
}

public override string ToString()
Expand Down
Loading