Esempio n. 1
0
        internal IList <BluetoothLeServiceData> GetScanResultServiceDataList(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            if (!BluetoothAdapter.IsBluetoothEnabled || !Globals.IsInitialize)
            {
                BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
            }

            IntPtr serviceListArray;

            _serviceListCount = 0;
            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultServiceDataList(ref scanDataStruct, packetType, out serviceListArray, out _serviceListCount);

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

            Log.Info(Globals.LogTag, "Count of ServiceDataList: " + _serviceListCount);

            IList <BluetoothLeServiceData> list = new List <BluetoothLeServiceData>();
            int sizePointerToABC = Marshal.SizeOf(new BluetoothLeServiceDataStruct());

            for (int i = 0; i < _serviceListCount; i++)
            {
                var svc = (BluetoothLeServiceDataStruct)Marshal.PtrToStructure(new IntPtr(serviceListArray.ToInt32() + (i * sizePointerToABC)), typeof(BluetoothLeServiceDataStruct));
                list.Add(BluetoothUtils.ConvertStructToLeServiceData(svc));
            }

            Interop.Bluetooth.FreeServiceDataList(serviceListArray, _serviceListCount);
            Marshal.FreeHGlobal(scanDataStruct.AdvData);
            Marshal.FreeHGlobal(scanDataStruct.ScanData);
            return(list);
        }
Esempio n. 2
0
        internal IList <BluetoothLeServiceData> GetScanResultServiceDataList(BluetoothLeScanData scanData,
                                                                             BluetoothLePacketType packetType, out int serviceCount)
        {
            int    ret = 0;
            IntPtr serviceListArray;

            _serviceListCount = 0;

            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            ret = Interop.Bluetooth.GetScanResultServiceDataList(ref scanDataStruct, packetType, out serviceListArray, out _serviceListCount);
            Log.Info(Globals.LogTag, "ServiceListCount :  " + _serviceListCount + " PacketType : " + packetType + " Error: " + (BluetoothError)ret);
            if (ret != (int)BluetoothError.None)
            {
                Log.Info(Globals.LogTag, "Failed to get Service Data List, Error - " + (BluetoothError)ret);
                serviceCount = 0;
                Marshal.FreeHGlobal(serviceListArray);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                return(null);
            }

            BluetoothLeServiceDataStruct[] svcList = new BluetoothLeServiceDataStruct[_serviceListCount];
            int sizePointerToABC = Marshal.SizeOf(new BluetoothLeServiceDataStruct());

            for (int i = 0; i < _serviceListCount; i++)
            {
                svcList[i] = (BluetoothLeServiceDataStruct)Marshal.PtrToStructure(new IntPtr(serviceListArray.ToInt32() + (i * sizePointerToABC)), typeof(BluetoothLeServiceDataStruct));
                Log.Info(Globals.LogTag, " Uuid : " + svcList[i].ServiceUuid + "length :  " + svcList[i].ServiceDataLength);

                _list.Add(BluetoothUtils.ConvertStructToLeServiceData(svcList[i]));
            }

            serviceCount = _serviceListCount;

            Interop.Libc.Free(serviceListArray);
            Marshal.FreeHGlobal(scanDataStruct.AdvData);
            Marshal.FreeHGlobal(scanDataStruct.ScanData);
            return(_list);
        }