Beispiel #1
0
        internal ManufacturerData GetScanResultManufacturerData(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            int    dataId     = 0;
            int    dataLength = 0;
            IntPtr manufData;

            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);
            ManufacturerData          data           = new ManufacturerData();

            int ret = Interop.Bluetooth.GetScanResultManufacturerData(ref scanDataStruct, packetType, out dataId,
                                                                      out manufData, out dataLength);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get Manufacturer data - " + (BluetoothError)ret);
                return(null);
            }

            data.Id         = dataId;
            data.DataLength = dataLength;
            if (data.DataLength > 0)
            {
                data.Data = new byte[data.DataLength];
                Marshal.Copy(manufData, data.Data, 0, data.DataLength);
            }

            return(data);
        }
Beispiel #2
0
        internal ManufacturerData GetScanResultManufacturerData(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            if (!BluetoothAdapter.IsBluetoothEnabled || !Globals.IsInitialize)
            {
                BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
            }

            int    dataId     = 0;
            int    dataLength = 0;
            IntPtr manufData;
            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultManufacturerData(ref scanDataStruct, packetType, out dataId, out manufData, out dataLength);

            if (ret == (int)BluetoothError.NoData)
            {
                Log.Info(Globals.LogTag, "No ManufacturerData in " + packetType);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                return(null);
            }
            else if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get Manufacturer data - " + (BluetoothError)ret);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                BluetoothErrorFactory.ThrowBluetoothException(ret);
            }

            Log.Info(Globals.LogTag, "Count of ManufacturerData: " + dataLength);

            ManufacturerData data = new ManufacturerData();

            data.Id         = dataId;
            data.DataLength = dataLength;
            if (data.DataLength > 0)
            {
                data.Data = new byte[data.DataLength];
                Marshal.Copy(manufData, data.Data, 0, data.DataLength);
            }

            Interop.Libc.Free(manufData);
            Marshal.FreeHGlobal(scanDataStruct.AdvData);
            Marshal.FreeHGlobal(scanDataStruct.ScanData);
            return(data);
        }
Beispiel #3
0
        /// <summary>
        /// Adds the manufacturer specific data to the advertise or the scan response data.
        /// Please refer to the adopted Bluetooth specification for the the appearance.
        /// </summary>
        /// <remarks>
        /// The Bluetooth must be enabled.
        /// </remarks>
        /// <param name="packetType">The packet type.</param>
        /// <param name="manufacturerData"> The manufacturer specific data.</param>
        /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
        /// or when the add advertising manufacturer data procedure fails.</exception>
        /// <since_tizen> 3 </since_tizen>
        public void AddAdvertisingManufacturerData(BluetoothLePacketType packetType,
                                                   ManufacturerData manufacturerData)
        {
            if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
            {
                IntPtr manufDataPtr;
                manufDataPtr = Marshal.AllocHGlobal(manufacturerData.DataLength);
                Marshal.Copy(manufacturerData.Data, 0, manufDataPtr, manufacturerData.DataLength);

                int ret = Interop.Bluetooth.AddAdvertisingManufData(GetHandle(), packetType,
                                                                    manufacturerData.Id, manufDataPtr, manufacturerData.DataLength);
                if (ret != (int)BluetoothError.None)
                {
                    Log.Error(Globals.LogTag, "Failed to add service solicitation uuid to advertising data- " + (BluetoothError)ret);
                    BluetoothErrorFactory.ThrowBluetoothException(ret);
                }
            }
            else
            {
                BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
            }
        }