2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 namespace Tizen.Network.Bluetooth
21 internal class BluetoothOppServerImpl
23 private static readonly BluetoothOppServerImpl _instance = new BluetoothOppServerImpl();
25 internal event EventHandler<ConnectionRequestedEventArgs> ConnectionRequested;
26 private Interop.Bluetooth.ConnectionRequestedCallback _ConnectionRequestedCallback;
28 internal event EventHandler<TransferProgressEventArgs> TransferProgress;
29 private Interop.Bluetooth.TransferProgressCallback _TransferProgressCallback;
31 internal event EventHandler<TransferFinishedEventArgs> TransferFinished;
32 private Interop.Bluetooth.TransferFinishedCallback _TransferFinishedCallback;
34 internal int StartServer(string filePath)
36 _ConnectionRequestedCallback = (string devAddress, IntPtr userData) =>
38 ConnectionRequested?.Invoke(this, new ConnectionRequestedEventArgs(devAddress));
41 int ret = Interop.Bluetooth.InitializeOppServerCustom(filePath, _ConnectionRequestedCallback, IntPtr.Zero);
42 if (ret != (int)BluetoothError.None)
44 Log.Error(Globals.LogTag, "Failed to start bluetooth opp server, Error - " + (BluetoothError)ret);
45 BluetoothErrorFactory.ThrowBluetoothException(ret);
49 Globals.IsOppServerInitialized = true;
54 internal int StopServer()
56 if (Globals.IsOppServerInitialized)
58 int ret = Interop.Bluetooth.DinitializeOppServer();
59 if (ret != (int)BluetoothError.None) {
60 Log.Error (Globals.LogTag, "Failed to stop bluetooth opp server, Error - " + (BluetoothError)ret);
64 return (int)BluetoothError.NotInitialized;
67 internal int AcceptPush(string name, out int _transferId)
70 if (Globals.IsOppServerInitialized)
72 _TransferProgressCallback = (string file, long size, int percent, IntPtr userData) =>
74 TransferProgress?.Invoke(this, new TransferProgressEventArgs(file, size, percent));
77 _TransferFinishedCallback = (int result, string file, long size, IntPtr userData) =>
79 TransferFinished?.Invoke(this, new TransferFinishedEventArgs(result, file, size));
82 int ret = Interop.Bluetooth.OppServerAcceptPush(_TransferProgressCallback, _TransferFinishedCallback, name, IntPtr.Zero, out _transferId);
83 if (ret != (int)BluetoothError.None)
85 Log.Error(Globals.LogTag, "Failed to accept the push request, Error - " + (BluetoothError)ret);
89 return (int)BluetoothError.NotInitialized;
92 internal int RejectPush()
94 if (Globals.IsOppServerInitialized)
96 int ret = Interop.Bluetooth.OppServerRejectPush();
97 if (ret != (int)BluetoothError.None)
99 Log.Error(Globals.LogTag, "Failed to reject the push request, Error - " + (BluetoothError)ret);
103 return (int)BluetoothError.NotInitialized;
106 internal int CancelTransferId(int TransferId)
108 if (Globals.IsOppServerInitialized)
110 int ret = Interop.Bluetooth.OppServerCancelTransfer(TransferId);
111 if (ret != (int)BluetoothError.None)
113 Log.Error(Globals.LogTag, "Failed to cancel the transferid " + TransferId + " Error - " + (BluetoothError)ret);
117 return (int)BluetoothError.NotInitialized;
120 internal int SetDestinationPath(string path)
122 if (Globals.IsOppServerInitialized)
124 int ret = Interop.Bluetooth.OppServerSetDestinationPath(path);
125 if (ret != (int)BluetoothError.None)
127 Log.Error(Globals.LogTag, "Failed to Set the desitination path " + path + " Error - " + (BluetoothError)ret);
131 return (int)BluetoothError.NotInitialized;
134 internal static BluetoothOppServerImpl Instance
143 internal class BluetoothOppClientImpl
145 private static readonly BluetoothOppClientImpl _instance = new BluetoothOppClientImpl();
147 internal event EventHandler<PushRespondedEventArgs> PushResponded;
148 private Interop.Bluetooth.PushRespondedCallback _PushRespondedCallback;
150 internal event EventHandler<PushProgressEventArgs> PushProgress;
151 private Interop.Bluetooth.PushProgressCallback _PushProgressCallback;
153 internal event EventHandler<PushFinishedEventArgs> PushFinished;
154 private Interop.Bluetooth.PushFinishedCallback _PushFinishedCallback;
156 private BluetoothOppClientImpl()
158 Log.Info(Globals.LogTag, "Initializing OppClient");
162 ~BluetoothOppClientImpl()
167 internal int AddFile(string filePath)
170 if (Globals.IsOppClientInitialized)
172 int ret = Interop.Bluetooth.OppClientAddFile(filePath);
173 if (ret != (int)BluetoothError.None)
175 Log.Error(Globals.LogTag, "Failed to Add File, Error - " + (BluetoothError)ret);
179 return (int)BluetoothError.NotInitialized;
182 internal int ClearFile()
185 if (Globals.IsOppClientInitialized)
187 int ret = Interop.Bluetooth.OppClientClearFiles();
188 if (ret != (int)BluetoothError.None)
190 Log.Error(Globals.LogTag, "Failed to Clear Files, Error - " + (BluetoothError)ret);
194 return (int)BluetoothError.NotInitialized;
197 internal int CancelPush()
200 if (Globals.IsOppClientInitialized)
202 int ret = Interop.Bluetooth.OppClientCancelPush();
203 if (ret != (int)BluetoothError.None)
205 Log.Error(Globals.LogTag, "Failed to Clear Files, Error - " + (BluetoothError)ret);
209 return (int)BluetoothError.NotInitialized;
212 internal int PushFile(string Destination)
215 if (Globals.IsOppClientInitialized)
217 _PushRespondedCallback = (int result, string address, IntPtr userData) =>
219 PushResponded?.Invoke(this, new PushRespondedEventArgs(result, address));
222 _PushProgressCallback = (string file, long size, int percent, IntPtr userData) =>
224 PushProgress?.Invoke(this, new PushProgressEventArgs(file, size, percent));
227 _PushFinishedCallback = (int result, string address, IntPtr userData) =>
229 PushFinished?.Invoke(this, new PushFinishedEventArgs(result, address));
232 int ret = Interop.Bluetooth.OppClientPushFile(Destination, _PushRespondedCallback, _PushProgressCallback, _PushFinishedCallback, IntPtr.Zero);
233 if (ret != (int)BluetoothError.None)
235 Log.Error(Globals.LogTag, "Failed to push File, Error - " + (BluetoothError)ret);
239 return (int)BluetoothError.NotInitialized;
242 private void Initialize()
244 if (Globals.IsInitialize)
247 int ret = Interop.Bluetooth.InitializeOppClient();
248 if (ret != (int)BluetoothError.None)
250 Log.Error(Globals.LogTag, "Failed to initialize bluetooth Opp Client, Error - " + (BluetoothError)ret);
251 BluetoothErrorFactory.ThrowBluetoothException(ret);
255 Globals.IsOppClientInitialized = true;
260 Log.Error(Globals.LogTag, "Failed to initialize Opp Client, BT not initialized");
261 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
265 private void Deinitialize()
267 if (Globals.IsOppClientInitialized)
269 int ret = Interop.Bluetooth.DeinitializeOppClient();
270 if (ret != (int)BluetoothError.None)
272 Log.Error(Globals.LogTag, "Failed to deinitialize Opp Client, Error - " + (BluetoothError)ret);
273 BluetoothErrorFactory.ThrowBluetoothException(ret);
277 Globals.IsOppClientInitialized = false;
282 internal static BluetoothOppClientImpl Instance