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
Expand Up @@ -42,6 +42,7 @@ public void DidUpdateState(CBCentralManager central)
public void DidConnectPeripheral(CBCentralManager central, CBPeripheral peripheral)
{
Debug.Log($"[DidConnectPeripheral] peripheral: {peripheral}");
Debug.Log($"[DidConnectPeripheral] mtu: {peripheral.GetMaximumWriteValueLength(CBCharacteristicWriteType.WithResponse)}");
peripheral.DiscoverServices(new string[] { _serviceUUID });
}

Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ public void WriteValue(byte[] data, CBCharacteristic characteristic, CBCharacter
_nativePeripheral.WriteValue(data, characteristic, type);
}

/// <summary>
/// The maximum amount of data, in bytes, you can send to a characteristic in a single write type.
/// </summary>
public int GetMaximumWriteValueLength(CBCharacteristicWriteType type)
{
return _nativePeripheral.GetMaximumWriteValueLength(type);
}

/// <summary>
/// Sets notifications or indications for the value of a specified characteristic.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ internal static extern int cb4u_peripheral_write_characteristic_value(
int writeType
);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern int cb4u_peripheral_maximum_write_value_length(SafeNativePeripheralHandle handle, int writeType);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern int cb4u_peripheral_set_notify_value(
SafeNativePeripheralHandle handle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ internal void WriteValue(byte[] data, CBCharacteristic characteristic, CBCharact
ExceptionUtils.ThrowIfCharacteristicNotFound(result, characteristic.UUID);
}

internal int GetMaximumWriteValueLength(CBCharacteristicWriteType writeType)
{
return NativeMethods.cb4u_peripheral_maximum_write_value_length(_handle, (int)writeType);
}

internal void SetNotifyValue(bool enabled, CBCharacteristic characteristic)
{
int result = NativeMethods.cb4u_peripheral_set_notify_value(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public class CB4UPeripheral : NSObject {
}
}

public func maximumWriteValueLength(_ writeType: CBCharacteristicWriteType) -> Int32 {
return Int32(peripheral.maximumWriteValueLength(for: writeType))
}

public func setNotifyValue(_ serviceUUID: CBUUID, _ characteristicUUID: CBUUID, _ enabled: Bool) -> Int32 {
return actionForCharacteristic(serviceUUID, characteristicUUID) { (service, characteristic) -> Void in
peripheral.setNotifyValue(enabled, for: characteristic)
Expand All @@ -97,7 +101,7 @@ extension CB4UPeripheral : CBPeripheralDelegate {

public func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
let commaSeparatedServiceIds = peripheral.services?.map { $0.uuid.uuidString }.joined(separator: ",") ?? ""

commaSeparatedServiceIds.withCString { (commaSeparatedServiceIdsCString) in
didDiscoverServicesHandler?(selfPointer(), commaSeparatedServiceIdsCString, errorToCode(error))
}
Expand All @@ -106,7 +110,7 @@ extension CB4UPeripheral : CBPeripheralDelegate {
public func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
let serviceId = service.uuid.uuidString
let commaSeparatedCharacteristicIds = service.characteristics?.map { $0.uuid.uuidString }.joined(separator: ",") ?? ""

serviceId.withCString { (serviceIdCString) in
commaSeparatedCharacteristicIds.withCString { (commaSeparatedCharacteristicIdsCString) in
didDiscoverCharacteristicsHandler?(selfPointer(), serviceIdCString, commaSeparatedCharacteristicIdsCString, errorToCode(error))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ public func cb4u_peripheral_write_characteristic_value(
return instance.writeCharacteristicValue(CBUUID(string: String(cString: serviceUUID)), CBUUID(string: String(cString: characteristicUUID)), data, CBCharacteristicWriteType(rawValue: Int(writeType))!)
}

@_cdecl("cb4u_peripheral_maximum_write_value_length")
public func cb4u_peripheral_maximum_write_value_length(
_ peripheralPtr: UnsafeRawPointer,
_ writeType: Int32
) -> Int32 {
let instance = Unmanaged<CB4UPeripheral>.fromOpaque(peripheralPtr).takeUnretainedValue()

return Int32(instance.maximumWriteValueLength(CBCharacteristicWriteType(rawValue: Int(writeType))!))
}

@_cdecl("cb4u_peripheral_set_notify_value")
public func cb4u_peripheral_set_notify_value(
_ peripheralPtr: UnsafeRawPointer,
Expand Down