internal delegate void HidConnectionStateChangedCallback(int result, bool connected, string deviceAddress, IntPtr userData);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+ internal delegate void ConnectionRequestedCallback(string deviceAddress, IntPtr userData);
+ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+ internal delegate void PushRequestedCallback(string file, long size, IntPtr userData);
+
+ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+ internal delegate void TransferProgressCallback(string file, long size, int percent, IntPtr userData);
+ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+ internal delegate void TransferFinishedCallback(int result, string file, long size, IntPtr userData);
+
+ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+ internal delegate void PushRespondedCallback(int result, string deviceAddress, IntPtr userData);
+ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+ internal delegate void PushProgressCallback(string file, long size, int percent, IntPtr userData);
+ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+ internal delegate void PushFinishedCallback(int result, string deviceAddress, IntPtr userData);
+
+ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void TargetConnectionStateChangedCallback(bool connected, string deviceAddress, IntPtr userData);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void EqualizerStateChangedCallback(int equalizer, IntPtr userData);
[DllImport(Libraries.Bluetooth, EntryPoint = "bt_hid_host_disconnect")]
internal static extern int Disconnect(string deviceAddress);
+ // Bluetooth OPP
+ // Opp Server
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_server_initialize")]
+ internal static extern int InitializeOppServer(string deviceAddress, PushRequestedCallback pushRequestedCb, IntPtr userData);
+
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_server_deinitialize")]
+ internal static extern int DinitializeOppServer();
+
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_server_initialize_by_connection_request")]
+ internal static extern int InitializeOppServerCustom(string deviceAddress, ConnectionRequestedCallback connectionRequestedCb, IntPtr userData);
+
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_server_accept")]
+ internal static extern int OppServerAcceptPush(TransferProgressCallback transferProgressCb, TransferFinishedCallback transferFinishedCb, string name, IntPtr userData, out int transferId);
+
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_server_reject")]
+ internal static extern int OppServerRejectPush();
+
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_server_cancel_transfer")]
+ internal static extern int OppServerCancelTransfer(int transferId);
+
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_server_set_destination")]
+ internal static extern int OppServerSetDestinationPath(string path);
+
+ // Opp Client
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_client_initialize")]
+ internal static extern int InitializeOppClient();
+
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_client_dinitialize")]
+ internal static extern int DeinitializeOppClient();
+
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_client_add_file")]
+ internal static extern int OppClientAddFile(string filePath);
+
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_client_clear_files")]
+ internal static extern int OppClientClearFiles();
+
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_client_cancel_push")]
+ internal static extern int OppClientCancelPush();
+
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_client_push_files")]
+ internal static extern int OppClientPushFile(string filePath, PushRespondedCallback pushRespondedCb, PushProgressCallback pushProgressCb, PushFinishedCallback pushFinishedCb, IntPtr userData);
+
//Bluetooth Avrcp
[DllImport(Libraries.Bluetooth, EntryPoint = "bt_avrcp_target_initialize")]
internal static extern int InitializeAvrcp(TargetConnectionStateChangedCallback targetStateChangedCb, IntPtr userData);
internal static bool IsInitialize = false;
internal static bool IsAudioInitialize = false;
internal static bool IsHidInitialize = false;
+ internal static bool IsOppServerInitialized = false;
+ internal static bool IsOppClientInitialized = false;
}
internal partial class BluetoothAdapterImpl : IDisposable
profile = (hid as T);
}
+ else if (type.Equals("Tizen.Network.Bluetooth.BluetoothOppClient"))
+ {
+ BluetoothOppClient oppClient = new BluetoothOppClient();
+ profile = (oppClient as T);
+ }
+
if (profile != null)
{
profile.RemoteAddress = RemoteDeviceAddress;
namespace Tizen.Network.Bluetooth
{
+ /// <summary>
+ /// A class which is used to Throw the Bluetooth Error exceptions.
+ /// </summary>
static internal class BluetoothErrorFactory
{
+ /// <summary>
+ /// Exceptions for Bluetooth Errors.
+ /// </summary>
+ /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth Error happens.</exception>
static internal void ThrowBluetoothException(int exception)
{
BluetoothError error = (BluetoothError)exception;
/// </summary>
public bool Completed { get; }
}
+
+ /// <summary>
+ /// An extended EventArgs class which contains the connection state and address of the remote Bluetooth device.
+ /// </summary>
+ public class ConnectionRequestedEventArgs : EventArgs
+ {
+ private string _address;
+
+ internal ConnectionRequestedEventArgs(string address)
+ {
+ _address = address;
+ }
+
+ /// <summary>
+ /// The address.
+ /// </summary>
+ public string Address
+ {
+ get
+ {
+ return _address;
+ }
+ }
+ }
+
+ /// <summary>
+ /// An extended EventArgs class which contains the file transfer progress state, file transfer progress by percent.
+ /// </summary>
+ public class TransferProgressEventArgs : EventArgs
+ {
+ private string _file;
+ private long _size;
+ private int _percent;
+
+ internal TransferProgressEventArgs(string file, long size, int percent)
+ {
+ _file = file;
+ _size = size;
+ _percent = percent;
+ }
+
+ /// <summary>
+ /// The File name.
+ /// </summary>
+ public string File
+ {
+ get
+ {
+ return _file;
+ }
+ }
+
+ /// <summary>
+ /// The File size.
+ /// </summary>
+ public long Size
+ {
+ get
+ {
+ return _size;
+ }
+ }
+
+ /// <summary>
+ /// The File transfer percent.
+ /// </summary>
+ public int Percent
+ {
+ get
+ {
+ return _percent;
+ }
+ }
+ }
+
+ /// <summary>
+ /// An extended EventArgs class which contains the file transfer finished state and file state.
+ /// </summary>
+ public class TransferFinishedEventArgs : EventArgs
+ {
+ private string _file;
+ private long _size;
+ private int _result;
+
+ internal TransferFinishedEventArgs(int result, string file, long size)
+ {
+ _file = file;
+ _size = size;
+ _result = result;
+ }
+
+ /// <summary>
+ /// The File name.
+ /// </summary>
+ public string File
+ {
+ get
+ {
+ return _file;
+ }
+ }
+
+ /// <summary>
+ /// The File size.
+ /// </summary>
+ public long Size
+ {
+ get
+ {
+ return _size;
+ }
+ }
+
+ /// <summary>
+ /// The return value.
+ /// </summary>
+ public int Result
+ {
+ get
+ {
+ return _result;
+ }
+ }
+ }
+
+ /// <Summary>
+ /// An extended EventArgs class which contains the Push Request respond state
+ /// </Summary>
+
+ public class PushRespondedEventArgs : EventArgs
+ {
+ int _result;
+ string _address;
+
+ internal PushRespondedEventArgs(int result, string address)
+ {
+ _address = address;
+ _result = result;
+ }
+
+ /// <summary>
+ /// The return value.
+ /// </summary>
+ public int Result
+ {
+ get
+ {
+ return _result;
+ }
+ }
+
+ /// <summary>
+ /// The address.
+ /// </summary>
+ public string Address
+ {
+ get
+ {
+ return _address;
+ }
+ }
+ }
+
+ /// <summary>
+ /// An extended EventArgs class which contains the file push progress state, push progress by percent.
+ /// </summary>
+ public class PushProgressEventArgs : EventArgs
+ {
+ private string _file;
+ private long _size;
+ private int _percent;
+
+ internal PushProgressEventArgs(string file, long size, int percent)
+ {
+ _file = file;
+ _size = size;
+ _percent = percent;
+ }
+
+ /// <summary>
+ /// The File name.
+ /// </summary>
+ public string File
+ {
+ get
+ {
+ return _file;
+ }
+ }
+
+ /// <summary>
+ /// The File size.
+ /// </summary>
+ public long Size
+ {
+ get
+ {
+ return _size;
+ }
+ }
+
+ /// <summary>
+ /// The File transfer percent.
+ /// </summary>
+ public int Percent
+ {
+ get
+ {
+ return _percent;
+ }
+ }
+ }
+
+ /// <Summary>
+ /// An extended EventArgs class which contains the Push Request respond state
+ /// </Summary>
+
+ public class PushFinishedEventArgs : EventArgs
+ {
+ int _result;
+ string _address;
+
+ internal PushFinishedEventArgs(int result, string address)
+ {
+ _address = address;
+ _result = result;
+ }
+
+ /// <summary>
+ /// The return value.
+ /// </summary>
+ public int Result
+ {
+ get
+ {
+ return _result;
+ }
+ }
+
+ /// <summary>
+ /// The address.
+ /// </summary>
+ public string Address
+ {
+ get
+ {
+ return _address;
+ }
+ }
+ }
}
--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+
+
+namespace Tizen.Network.Bluetooth
+{
+ /// <summary>
+ /// A class which is used to handle the connection and send and receive the object over Opp profile.
+ /// </summary>
+ /// <privilege> http://tizen.org/privilege/bluetooth </privilege>
+ public class BluetoothOppServer
+ {
+ private static BluetoothOppServerImpl _impl;
+ private static BluetoothOppServer _instance;
+
+ public BluetoothOppServer()
+ {
+ _impl = BluetoothOppServerImpl.Instance;
+ }
+
+ /// <summary>
+ /// (event) ConnectionRequested is called when OPP client requests for connection.
+ /// </summary>
+ public event EventHandler<ConnectionRequestedEventArgs> ConnectionRequested
+ {
+ add
+ {
+ _impl.ConnectionRequested += value;
+ }
+ remove
+ {
+ _impl.ConnectionRequested -= value;
+ }
+ }
+
+ /// <summary>
+ /// (event) TransferProgress is called when the file transfer state is changed.
+ /// </summary>
+ public event EventHandler<TransferProgressEventArgs> TransferProgress
+ {
+ add
+ {
+ _impl.TransferProgress += value;
+ }
+ remove
+ {
+ _impl.TransferProgress -= value;
+ }
+ }
+
+ /// <summary>
+ /// (event) TransferFinished is called when the file tranfser is completed.
+ /// </summary>
+ public event EventHandler<TransferFinishedEventArgs> TransferFinished
+ {
+ add
+ {
+ _impl.TransferFinished += value;
+ }
+ remove
+ {
+ _impl.TransferFinished -= value;
+ }
+ }
+ /// <summary>
+ /// Register the Opp Server with the Opp service.
+ /// </summary>
+ /// <remarks>
+ /// The device must be bonded with remote device by CreateBond().
+ /// If connection request is received from OPP Client, ConnectionRequested event will be invoked.
+ /// </remarks>
+ /// <param name="FilePath"> Path to store the files.</param>
+ /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
+ /// <exception cref="System.NotSupportedException">Thrown when the required feature is not Supported.</exception>
+ /// <exception cref="System.NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
+ /// <exception cref="System.InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
+ public static BluetoothOppServer StartServer(string FilePath)
+ {
+ if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
+ {
+ if (_instance == null)
+ {
+ BluetoothOppServer server = new BluetoothOppServer();
+ if (server != null)
+ _instance = server;
+ }
+ int ret = _impl.StartServer(FilePath);
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to Opp Start Server - " + (BluetoothError)ret);
+ BluetoothErrorFactory.ThrowBluetoothException(ret);
+ }
+ return _instance;
+ }
+ else
+ {
+ BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
+ }
+ return null;
+ }
+
+ /// <summary>
+ /// Stops the Opp Server.
+ /// </summary>
+ /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
+ /// <exception cref="System.NotSupportedException">Thrown when the required feature is not Supported.</exception>
+ /// <exception cref="System.NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
+ /// <exception cref="System.InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
+ public void StopServer()
+ {
+ if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
+ {
+ int ret = _impl.StopServer();
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to Stop the Opp Server - " + (BluetoothError)ret);
+ BluetoothErrorFactory.ThrowBluetoothException(ret);
+ }
+ else
+ {
+ if (_instance != null)
+ _instance = null;
+ }
+ }
+ else
+ {
+ BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
+ }
+ }
+
+ /// <summary>
+ /// Accept File Push request.
+ /// </summary>
+ /// <param name="FileName"> File name to accept.</param>
+ /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
+ /// <exception cref="System.NotSupportedException">Thrown when the required feature is not Supported.</exception>
+ /// <exception cref="System.NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
+ /// <exception cref="System.InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
+ public int AcceptPush(string FileName)
+ {
+ int _transitionId = -1;
+ if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
+ {
+ int ret = _impl.AcceptPush(FileName, out _transitionId);
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to Accept Push - " + (BluetoothError)ret);
+ BluetoothErrorFactory.ThrowBluetoothException(ret);
+ }
+ }
+ else
+ {
+ BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
+ }
+ return _transitionId;
+ }
+
+ /// <summary>
+ /// Reject File Push request.
+ /// </summary>
+ /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
+ /// <exception cref="System.NotSupportedException">Thrown when the required feature is not Supported.</exception>
+ /// <exception cref="System.NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
+ /// <exception cref="System.InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
+ public void RejectPush()
+ {
+ if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
+ {
+ int ret = _impl.RejectPush();
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to Reject Push - " + (BluetoothError)ret);
+ BluetoothErrorFactory.ThrowBluetoothException(ret);
+ }
+ }
+ else
+ {
+ BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
+ }
+ }
+
+ /// <summary>
+ /// Cancel the ongoing transfer session.
+ /// </summary>
+ /// <param name="TransferId"> tranfer ID.</param>
+ /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
+ /// <exception cref="System.NotSupportedException">Thrown when the required feature is not Supported.</exception>
+ /// <exception cref="System.NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
+ /// <exception cref="System.InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
+ public void CancelTransfer(int TransferId)
+ {
+ if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
+ {
+ int ret = _impl.CancelTransferId(TransferId);
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to Cancel Transfer - " + (BluetoothError)ret);
+ BluetoothErrorFactory.ThrowBluetoothException(ret);
+ }
+ }
+ else
+ {
+ BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
+ }
+ }
+
+ /// <summary>
+ /// Cancel the ongoing transfer session.
+ /// </summary>
+ /// <param name="FilePath"> Path to store the files.</param>
+ /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
+ /// <exception cref="System.NotSupportedException">Thrown when the required feature is not Supported.</exception>
+ /// <exception cref="System.NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
+ /// <exception cref="System.InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
+ public void SetDestinationPath(string FilePath)
+ {
+ if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
+ {
+ int ret = _impl.SetDestinationPath(FilePath);
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to Set Destination Path - " + (BluetoothError)ret);
+ BluetoothErrorFactory.ThrowBluetoothException(ret);
+ }
+ }
+ else
+ {
+ BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
+ }
+ }
+ }
+
+ /// <summary>
+ /// A class which is used to handle the connection and send and receive the object over Opp profile.
+ /// </summary>
+ /// <privilege> http://tizen.org/privilege/bluetooth </privilege>
+ public class BluetoothOppClient : BluetoothProfile
+ {
+ internal BluetoothOppClient()
+ {
+ }
+
+ /// <summary>
+ /// (event) PushResponded is called when remote OPP Server responds to a File push request.
+ /// </summary>
+ public event EventHandler<PushRespondedEventArgs> PushResponded
+ {
+ add
+ {
+ BluetoothOppClientImpl.Instance.PushResponded += value;
+ }
+ remove
+ {
+ BluetoothOppClientImpl.Instance.PushResponded -= value;
+ }
+ }
+
+ /// <summary>
+ /// (event) PushProgress is called when the file transfer state is changed.
+ /// </summary>
+ public event EventHandler<PushProgressEventArgs> PushProgress
+ {
+ add
+ {
+ BluetoothOppClientImpl.Instance.PushProgress += value;
+ }
+ remove
+ {
+ BluetoothOppClientImpl.Instance.PushProgress -= value;
+ }
+ }
+
+ /// <summary>
+ /// (event) PushFinished is called when the file tranfser is completed.
+ /// </summary>
+ public event EventHandler<PushFinishedEventArgs> PushFinished
+ {
+ add
+ {
+ BluetoothOppClientImpl.Instance.PushFinished += value;
+ }
+ remove
+ {
+ BluetoothOppClientImpl.Instance.PushFinished -= value;
+ }
+ }
+
+ /// <summary>
+ /// Add File path to be pushed.
+ /// </summary>
+ /// <param name="FilePath"> file for sending.</param>
+ /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
+ /// <exception cref="System.NotSupportedException">Thrown when the required feature is not Supported.</exception>
+ /// <exception cref="System.NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
+ /// <exception cref="System.InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
+ public void AddFile(string FilePath)
+ {
+ if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
+ {
+ int ret = BluetoothOppClientImpl.Instance.AddFile(FilePath);
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to Set File Path - " + (BluetoothError)ret);
+ BluetoothErrorFactory.ThrowBluetoothException(ret);
+ }
+ }
+ else
+ {
+ BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
+ }
+ }
+
+ /// <summary>
+ /// Clears all the File paths.
+ /// </summary>
+ /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
+ /// <exception cref="System.NotSupportedException">Thrown when the required feature is not Supported.</exception>
+ /// <exception cref="System.NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
+ /// <exception cref="System.InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
+ public void ClearFiles()
+ {
+ if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
+ {
+ int ret = BluetoothOppClientImpl.Instance.ClearFile();
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to Clear the Files - " + (BluetoothError)ret);
+ BluetoothErrorFactory.ThrowBluetoothException(ret);
+ }
+ }
+ else
+ {
+ BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
+ }
+ }
+
+ /// <summary>
+ /// Cancels the ongoing push session.
+ /// </summary>
+ /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
+ /// <exception cref="System.NotSupportedException">Thrown when the required feature is not Supported.</exception>
+ /// <exception cref="System.NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
+ /// <exception cref="System.InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
+ public void CancelPush()
+ {
+ if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
+ {
+ int ret = BluetoothOppClientImpl.Instance.CancelPush();
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to Cancel Push Operation - " + (BluetoothError)ret);
+ BluetoothErrorFactory.ThrowBluetoothException(ret);
+ }
+ }
+ else
+ {
+ BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
+ }
+ }
+
+ /// <summary>
+ /// Pushes the file set through AddFile.
+ /// </summary>
+ /// <param name="Destination"> destination device address.</param>
+ /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
+ /// <exception cref="System.NotSupportedException">Thrown when the required feature is not Supported.</exception>
+ /// <exception cref="System.NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
+ /// <exception cref="System.InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
+ public void PushFile(string Destination)
+ {
+ if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
+ {
+ int ret = BluetoothOppClientImpl.Instance.PushFile(Destination);
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to Cancel Push Operation - " + (BluetoothError)ret);
+ BluetoothErrorFactory.ThrowBluetoothException(ret);
+ }
+ }
+ else
+ {
+ BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
+ }
+ }
+ }
+}
+
--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+
+namespace Tizen.Network.Bluetooth
+{
+ internal class BluetoothOppServerImpl
+ {
+ private event EventHandler<ConnectionRequestedEventArgs> _ConnectionRequested;
+ private Interop.Bluetooth.ConnectionRequestedCallback _ConnectionRequestedCallback;
+
+ private event EventHandler<TransferProgressEventArgs> _TransferProgress;
+ private Interop.Bluetooth.TransferProgressCallback _TransferProgressCallback;
+
+ private event EventHandler<TransferFinishedEventArgs> _TransferFinished;
+ private Interop.Bluetooth.TransferFinishedCallback _TransferFinishedCallback;
+
+ private static readonly BluetoothOppServerImpl _instance = new BluetoothOppServerImpl();
+
+ internal event EventHandler<ConnectionRequestedEventArgs> ConnectionRequested
+ {
+ add
+ {
+ _ConnectionRequested += value;
+ }
+ remove
+ {
+ //nothing to be done
+ }
+ }
+
+ internal event EventHandler<TransferProgressEventArgs> TransferProgress
+ {
+ add
+ {
+ _TransferProgress += value;
+ }
+ remove
+ {
+ //nothing to be done
+ }
+ }
+
+ internal event EventHandler<TransferFinishedEventArgs> TransferFinished
+ {
+ add
+ {
+ _TransferFinished += value;
+ }
+ remove
+ {
+ //nothing to be done
+ }
+ }
+
+ internal int StartServer(string filePath)
+ {
+ _ConnectionRequestedCallback = (string devAddress, IntPtr userData) =>
+ {
+ _ConnectionRequested?.Invoke(null, new ConnectionRequestedEventArgs(devAddress));
+ };
+
+ int ret = Interop.Bluetooth.InitializeOppServerCustom(filePath, _ConnectionRequestedCallback, IntPtr.Zero);
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to start bluetooth opp server, Error - " + (BluetoothError)ret);
+ BluetoothErrorFactory.ThrowBluetoothException(ret);
+ }
+ else
+ {
+ Globals.IsOppServerInitialized = true;
+ }
+ return ret;
+ }
+
+ internal int StopServer()
+ {
+ if (Globals.IsOppServerInitialized)
+ {
+ int ret = Interop.Bluetooth.DinitializeOppServer();
+ if (ret != (int)BluetoothError.None) {
+ Log.Error (Globals.LogTag, "Failed to stop bluetooth opp server, Error - " + (BluetoothError)ret);
+ }
+ return ret;
+ }
+ return (int)BluetoothError.NotInitialized;
+ }
+
+ internal int AcceptPush(string name, out int _transferId)
+ {
+ _transferId = -1;
+ if (Globals.IsOppServerInitialized)
+ {
+ _TransferProgressCallback = (string file, long size, int percent, IntPtr userData) =>
+ {
+ _TransferProgress?.Invoke(null, new TransferProgressEventArgs(file, size, percent));
+ };
+
+ _TransferFinishedCallback = (int result, string file, long size, IntPtr userData) =>
+ {
+ _TransferFinished?.Invoke(null, new TransferFinishedEventArgs(result, file, size));
+ };
+
+ int ret = Interop.Bluetooth.OppServerAcceptPush(_TransferProgressCallback, _TransferFinishedCallback, name, IntPtr.Zero, out _transferId);
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to accept the push request, Error - " + (BluetoothError)ret);
+ }
+ return ret;
+ }
+ return (int)BluetoothError.NotInitialized;
+ }
+
+ internal int RejectPush()
+ {
+ if (Globals.IsOppServerInitialized)
+ {
+ int ret = Interop.Bluetooth.OppServerRejectPush();
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to reject the push request, Error - " + (BluetoothError)ret);
+ }
+ return ret;
+ }
+ return (int)BluetoothError.NotInitialized;
+ }
+
+ internal int CancelTransferId(int TransferId)
+ {
+ if (Globals.IsOppServerInitialized)
+ {
+ int ret = Interop.Bluetooth.OppServerCancelTransfer(TransferId);
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to cancel the transferid " + TransferId + " Error - " + (BluetoothError)ret);
+ }
+ return ret;
+ }
+ return (int)BluetoothError.NotInitialized;
+ }
+
+ internal int SetDestinationPath(string path)
+ {
+ if (Globals.IsOppServerInitialized)
+ {
+ int ret = Interop.Bluetooth.OppServerSetDestinationPath(path);
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to Set the desitination path " + path + " Error - " + (BluetoothError)ret);
+ }
+ return ret;
+ }
+ return (int)BluetoothError.NotInitialized;
+ }
+
+ internal static BluetoothOppServerImpl Instance
+ {
+ get
+ {
+ return _instance;
+ }
+ }
+ }
+
+ internal class BluetoothOppClientImpl
+ {
+ private event EventHandler<PushRespondedEventArgs> _PushResponded;
+ private Interop.Bluetooth.PushRespondedCallback _PushRespondedCallback;
+
+ private event EventHandler<PushProgressEventArgs> _PushProgress;
+ private Interop.Bluetooth.PushProgressCallback _PushProgressCallback;
+
+ private event EventHandler<PushFinishedEventArgs> _PushFinished;
+ private Interop.Bluetooth.PushFinishedCallback _PushFinishedCallback;
+
+ private static readonly BluetoothOppClientImpl _instance = new BluetoothOppClientImpl();
+
+ internal event EventHandler<PushRespondedEventArgs> PushResponded
+ {
+ add
+ {
+ _PushResponded += value;
+ }
+ remove
+ {
+ //nothing to be done
+ }
+ }
+
+ internal event EventHandler<PushProgressEventArgs> PushProgress
+ {
+ add
+ {
+ _PushProgress += value;
+ }
+ remove
+ {
+ //nothing to be done
+ }
+ }
+
+ internal event EventHandler<PushFinishedEventArgs> PushFinished
+ {
+ add
+ {
+ _PushFinished += value;
+ }
+ remove
+ {
+ //nothing to be done
+ }
+ }
+
+ private BluetoothOppClientImpl()
+ {
+ Log.Info(Globals.LogTag, "Initializing OppClient");
+ initialize();
+ }
+
+ ~BluetoothOppClientImpl()
+ {
+ deinitialize();
+ }
+
+ internal int AddFile(string filePath)
+ {
+
+ if (Globals.IsOppClientInitialized)
+ {
+ int ret = Interop.Bluetooth.OppClientAddFile(filePath);
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to Add File, Error - " + (BluetoothError)ret);
+ }
+ return ret;
+ }
+ return (int)BluetoothError.NotInitialized;
+ }
+
+ internal int ClearFile()
+ {
+
+ if (Globals.IsOppClientInitialized)
+ {
+ int ret = Interop.Bluetooth.OppClientClearFiles();
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to Clear Files, Error - " + (BluetoothError)ret);
+ }
+ return ret;
+ }
+ return (int)BluetoothError.NotInitialized;
+ }
+
+ internal int CancelPush()
+ {
+
+ if (Globals.IsOppClientInitialized)
+ {
+ int ret = Interop.Bluetooth.OppClientCancelPush();
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to Clear Files, Error - " + (BluetoothError)ret);
+ }
+ return ret;
+ }
+ return (int)BluetoothError.NotInitialized;
+ }
+
+ internal int PushFile(string Destination)
+ {
+
+ if (Globals.IsOppClientInitialized)
+ {
+ _PushRespondedCallback = (int result, string address, IntPtr userData) =>
+ {
+ _PushResponded?.Invoke(null, new PushRespondedEventArgs(result, address));
+ };
+
+ _PushProgressCallback = (string file, long size, int percent, IntPtr userData) =>
+ {
+ _PushProgress?.Invoke(null, new PushProgressEventArgs(file, size, percent));
+ };
+
+ _PushFinishedCallback = (int result, string address, IntPtr userData) =>
+ {
+ _PushFinished?.Invoke(null, new PushFinishedEventArgs(result, address));
+ };
+
+ int ret = Interop.Bluetooth.OppClientPushFile(Destination, _PushRespondedCallback, _PushProgressCallback, _PushFinishedCallback, IntPtr.Zero);
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to push File, Error - " + (BluetoothError)ret);
+ }
+ return ret;
+ }
+ return (int)BluetoothError.NotInitialized;
+ }
+
+ private void initialize()
+ {
+ if (Globals.IsInitialize)
+ {
+
+ int ret = Interop.Bluetooth.InitializeOppClient();
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to initialize bluetooth Opp Client, Error - " + (BluetoothError)ret);
+ BluetoothErrorFactory.ThrowBluetoothException(ret);
+ }
+ else
+ {
+ Globals.IsOppClientInitialized = true;
+ }
+ }
+ else
+ {
+ Log.Error(Globals.LogTag, "Failed to initialize Opp Client, BT not initialized");
+ BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
+ }
+ }
+
+ private void deinitialize()
+ {
+ if (Globals.IsOppClientInitialized)
+ {
+ int ret = Interop.Bluetooth.DeinitializeOppClient();
+ if (ret != (int)BluetoothError.None)
+ {
+ Log.Error(Globals.LogTag, "Failed to deinitialize Opp Client, Error - " + (BluetoothError)ret);
+ BluetoothErrorFactory.ThrowBluetoothException(ret);
+ }
+ else
+ {
+ Globals.IsOppClientInitialized = false;
+ }
+ }
+ }
+
+ internal static BluetoothOppClientImpl Instance
+ {
+ get
+ {
+ return _instance;
+ }
+ }
+ }
+}
+