[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
internal delegate void BtGattClientRequestCompletedCallback(int result, IntPtr requestHandle, IntPtr userData);
+ [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
+ internal delegate void BtGattClientAttMtuChangedCallback(IntPtr clientHandle, ref AttMtuInfoStruct mtuInfo, IntPtr userData);
+
// Gatt Attribute
[DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_destroy")]
[DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_client_write_value")]
internal static extern int BtGattClientWriteValue(BluetoothGattAttributeHandle gattHandle, BtGattClientRequestCompletedCallback callback, IntPtr userData);
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_client_get_att_mtu")]
+ internal static extern int BtGattClientGetAttMtu(BluetoothGattClientHandle clientHandle, out int mtu);
+
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_client_request_att_mtu_change")]
+ internal static extern int BtGattClientSetAttMtu(BluetoothGattClientHandle clientHandle, int mtu);
+
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_client_set_att_mtu_changed_cb")]
+ internal static extern int BtGattClientSetMtuChangedCallback(BluetoothGattClientHandle clientHandle, BtGattClientAttMtuChangedCallback callback, IntPtr userData);
+
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_client_unset_att_mtu_changed_cb")]
+ internal static extern int BtGattClientUnsetMtuChangedCallback(BluetoothGattClientHandle clientHandle);
+
// GATT Server
[DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_server_destroy")]
}
}
+ /// <summary>
+ /// An extended EventArgs class contains the changed MTU value.
+ /// </summary>
+ /// <since_tizen> 8 </since_tizen>
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public class AttMtuChangedEventArgs : EventArgs
+ {
+ internal AttMtuChangedEventArgs(string remoteAddress, int mtu)
+ {
+ RemoteAddress = remoteAddress;
+ Mtu = mtu;
+ }
+
+ /// <summary>
+ /// The remote address.
+ /// </summary>
+ /// <since_tizen> 8 </since_tizen>
+ public string RemoteAddress { get; }
+
+ /// <summary>
+ /// The MTU value
+ /// </summary>
+ /// <since_tizen> 8 </since_tizen>
+ public int Mtu { get; }
+ }
+
/// <summary>
/// An extended EventArgs class contains the changed attribute value.
/// </summary>
using System;
using System.Collections.Generic;
+using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
_impl = new BluetoothGattClientImpl(remoteAddress);
_remoteAddress = remoteAddress;
StaticConnectionStateChanged += OnConnectionStateChanged;
+ _impl.AttMtuChanged += OnAttMtuChanged;
+ }
+
+ private void OnAttMtuChanged(object s, AttMtuChangedEventArgs e)
+ {
+ AttMtuChanged?.Invoke(this, e);
}
/// <summary>
return await _impl.WriteValueAsyncTask(descriptor.GetHandle());
}
+ /// <summary>
+ /// Gets the value of the ATT MTU(Maximum Transmission Unit) for the connection.
+ /// </summary>
+ /// <returns>The MTU value</returns>
+ /// <exception cref="NotSupportedException">Thrown when the BT/BLE is not supported.</exception>
+ /// <exception cref="InvalidOperationException">Thrown when the BT/BLE is not enabled
+ /// or when the remote device is disconnected, or when other specific error occurs.</exception>
+ /// <since_tizen> 8 </since_tizen>
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public int GetAttMtu()
+ {
+ return _impl.GetAttMtu();
+ }
+
+ /// <summary>
+ /// Sets the value of the ATT MTU(Maximum Transmission Unit) for the connection.
+ /// </summary>
+ /// <param name="mtu">The MTU value</param>
+ /// <exception cref="NotSupportedException">Thrown when the BT/BLE is not supported.</exception>
+ /// <exception cref="InvalidOperationException">Thrown when the BT/BLE is not enabled
+ /// or when the remote device is disconnected, or when other specific error occurs.</exception>
+ /// <since_tizen> 8 </since_tizen>
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public void SetAttMtu(int mtu)
+ {
+ _impl.SetAttMtu(mtu);
+ }
+
+ /// <summary>
+ /// The AttMtuChanged event is raised when the MTU value changed.
+ /// </summary>
+ /// <since_tizen> 8 </since_tizen>
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public event EventHandler<AttMtuChangedEventArgs> AttMtuChanged;
+
internal bool Isvalid()
{
return _impl.GetHandle().IsInvalid == false;
Dictionary<int, TaskCompletionSource<bool>> _writeValueTaskSource = new Dictionary<int, TaskCompletionSource<bool>>();
private Interop.Bluetooth.BtGattClientRequestCompletedCallback _writeValueCallback;
private Interop.Bluetooth.BtGattForeachCallback _serviceForeachCallback;
+ private Interop.Bluetooth.BtGattClientAttMtuChangedCallback _attMtuChangedCallback;
+ private event EventHandler<AttMtuChangedEventArgs> _attMtuChanged;
internal BluetoothGattClientImpl(string remoteAddress)
{
return task.Task;
}
+ internal int GetAttMtu()
+ {
+ int err = Interop.Bluetooth.BtGattClientGetAttMtu(_handle, out int mtu);
+ if (err.IsFailed())
+ {
+ GattUtil.Error(err, "Failed to get MTU value");
+ BluetoothErrorFactory.ThrowBluetoothException(err);
+ }
+ return mtu;
+ }
+
+ internal void SetAttMtu(int mtu)
+ {
+ int err = Interop.Bluetooth.BtGattClientSetAttMtu(_handle, mtu);
+ if (err.IsFailed())
+ {
+ GattUtil.Error(err, "Failed to set MTU value");
+ BluetoothErrorFactory.ThrowBluetoothException(err);
+ }
+ }
+
+ internal event EventHandler<AttMtuChangedEventArgs> AttMtuChanged
+ {
+ add
+ {
+ if (_attMtuChanged == null)
+ {
+ RegisterMtuChangedEvent();
+ }
+ _attMtuChanged += value;
+ }
+ remove
+ {
+ _attMtuChanged -= value;
+ if (_attMtuChanged == null)
+ {
+ UnregisterMtuChangedEvent();
+ }
+ }
+ }
+
+ private void RegisterMtuChangedEvent()
+ {
+ _attMtuChangedCallback = (IntPtr clientHandle, ref AttMtuInfoStruct mtuInfoStruct, IntPtr userData) =>
+ {
+ _attMtuChanged?.Invoke(null, new AttMtuChangedEventArgs(mtuInfoStruct.RemoteAddress, mtuInfoStruct.Mtu));
+ };
+ int ret = Interop.Bluetooth.BtGattClientSetMtuChangedCallback(_handle, _attMtuChangedCallback, IntPtr.Zero);
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to set MTU changed callback, Error - " + (BluetoothError)ret);
+ }
+ }
+
+ private void UnregisterMtuChangedEvent()
+ {
+ int ret = Interop.Bluetooth.BtGattClientUnsetMtuChangedCallback(_handle);
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to unset MTU changed callback, Error - " + (BluetoothError)ret);
+ }
+ }
+
internal BluetoothGattClientHandle GetHandle()
{
return _handle;
internal uint number;
internal uint duration;
}
+
+ [NativeStruct("bt_gatt_client_att_mtu_info_s", Include = "bluetooth_type.h", PkgConfig = "capi-network-bluetooth")]
+ [StructLayout(LayoutKind.Sequential)]
+ internal struct AttMtuInfoStruct
+ {
+ [MarshalAsAttribute(UnmanagedType.LPStr)]
+ internal string RemoteAddress;
+ internal int Mtu;
+ internal int Status;
+ }
+
internal static class BluetoothUtils
{
internal static BluetoothDevice ConvertStructToDeviceClass(BluetoothDeviceStruct device)