Add SendData(byte[] data) method and deprecated previous method (#1392)
authorWootak <wootak.jung@samsung.com>
Wed, 12 Feb 2020 00:31:34 +0000 (09:31 +0900)
committerGitHub <noreply@github.com>
Wed, 12 Feb 2020 00:31:34 +0000 (09:31 +0900)
src/Tizen.Network.Bluetooth/Interop/Interop.Bluetooth.cs [changed mode: 0755->0644]
src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothData.cs
src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothSocket.cs
src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothStructs.cs

old mode 100755 (executable)
new mode 100644 (file)
index 66bac98..d3bcaea
@@ -350,6 +350,8 @@ internal static partial class Interop
         internal static extern int DisconnectSocket(int socketFd);
         [DllImport(Libraries.Bluetooth, EntryPoint = "bt_socket_send_data")]
         internal static extern int SendData(int socketFd, string data, int dataLength);
+        [DllImport(Libraries.Bluetooth, EntryPoint = "bt_socket_send_data")]
+        internal static extern int SendData(int socketFd, byte[] data, int dataLength);
         [DllImport(Libraries.Bluetooth, EntryPoint = "bt_socket_set_data_received_cb")]
         internal static extern int SetDataReceivedCallback(DataReceivedCallback callback, IntPtr userData);
         [DllImport(Libraries.Bluetooth, EntryPoint = "bt_socket_unset_data_received_cb")]
index 2c132b5..c87c6bf 100644 (file)
@@ -452,9 +452,10 @@ namespace Tizen.Network.Bluetooth
     /// <since_tizen> 3 </since_tizen>
     public class SocketData
     {
-        internal string RecvData;
-        internal int Size;
-        internal int Fd;
+        internal string _recvData;
+        internal byte[] _data;
+        internal int _dataSize;
+        internal int _fd;
 
         internal SocketData()
         {
@@ -468,7 +469,7 @@ namespace Tizen.Network.Bluetooth
         {
             get
             {
-                return Fd;
+                return _fd;
             }
         }
         /// <summary>
@@ -479,18 +480,30 @@ namespace Tizen.Network.Bluetooth
         {
             get
             {
-                return Size;
+                return _dataSize;
             }
         }
         /// <summary>
         /// The received data.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
+        [Obsolete("Deprecated since API level 7. Please use ByteData property.")]
         public string Data
         {
             get
             {
-                return RecvData;
+                return _recvData;
+            }
+        }
+        /// <summary>
+        /// The received data.
+        /// </summary>
+        /// <since_tizen> 7 </since_tizen>
+        public byte[] ByteData
+        {
+            get
+            {
+                return _data;
             }
         }
     }
index fc3ceb8..44d7ba3 100644 (file)
@@ -49,7 +49,19 @@ namespace Tizen.Network.Bluetooth
         /// </remarks>
         /// <param name="data">The data to be sent.</param>
         /// <since_tizen> 3 </since_tizen>
+        [Obsolete("Deprecated since API level 7. Please use SendData(byte[] data) method.")]
         int SendData(string data);
+
+        /// <summary>
+        /// Method to send data over bluetooth socket
+        /// </summary>
+        /// <returns>The number of bytes written (zero indicates nothing was written).</returns>
+        /// <remarks>
+        /// The connection must be established.
+        /// </remarks>
+        /// <param name="data">The data to be sent.</param>
+        /// <since_tizen> 7 </since_tizen>
+        int SendData(byte[] data);
     }
 
     /// <summary>
@@ -241,6 +253,7 @@ namespace Tizen.Network.Bluetooth
         /// <param name="data">The data to be sent.</param>
         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled
         /// or when the remote device is not connected, or the send data procedure fails.</exception>
+        [Obsolete("Deprecated since API level 7. Please use SendData(byte[] data) method.")]
         public int SendData(string data)
         {
             int ret = Interop.Bluetooth.SendData(connectedSocket, data, data.Length);
@@ -252,6 +265,27 @@ namespace Tizen.Network.Bluetooth
             return ret;
         }
 
+        /// <summary>
+        /// Sends data to the connected device.
+        /// </summary>
+        /// <returns>The number of bytes written (zero indicates nothing was written).</returns>
+        /// <remarks>
+        /// The connection must be established.
+        /// </remarks>
+        /// <param name="data">The data to be sent.</param>
+        /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled
+        /// or when the remote device is not connected, or the send data procedure fails.</exception>
+        public int SendData(byte[] data)
+        {
+            int ret = Interop.Bluetooth.SendData(connectedSocket, data, data.Length);
+            if (ret < 0)
+            {
+                Log.Error(Globals.LogTag, "Failed to send data, Error - " + (BluetoothError)ret);
+                BluetoothErrorFactory.ThrowBluetoothException(ret);
+            }
+            return ret;
+        }
+
         ~BluetoothSocket()
         {
             Dispose(false);
index 454d675..7042262 100644 (file)
@@ -385,9 +385,16 @@ namespace Tizen.Network.Bluetooth
         internal static SocketData ConvertStructToSocketData(SocketDataStruct structInfo)
         {
             SocketData data = new SocketData();
-            data.Fd = structInfo.SocketFd;
-            data.Size = structInfo.DataSize;
-            data.RecvData = Marshal.PtrToStringAnsi(structInfo.Data);
+            Log.Info(Globals.LogTag, "SocketDataLength" + structInfo.DataSize);
+
+            data._fd = structInfo.SocketFd;
+            if (structInfo.DataSize > 0)
+            {
+                data._dataSize = structInfo.DataSize;
+                data._data = new byte[data._dataSize];
+                Marshal.Copy(structInfo.Data, data._data, 0, data._dataSize);
+                data._recvData = Marshal.PtrToStringAnsi(structInfo.Data, structInfo.DataSize);
+            }
             return data;
         }