Beispiel #1
0
        internal static BluetoothLeServiceData ConvertStructToLeServiceData(BluetoothLeServiceDataStruct structServiceData)
        {
            BluetoothLeServiceData serviceData = new BluetoothLeServiceData();

            Log.Info(Globals.LogTag, "ServiceDataLength" + structServiceData.ServiceDataLength);

            if (structServiceData.ServiceDataLength > 0)
            {
                serviceData.Uuid   = structServiceData.ServiceUuid;
                serviceData.Length = structServiceData.ServiceDataLength;
                serviceData.Data   = new byte[serviceData.Length];
                Marshal.Copy(structServiceData.ServiceData, serviceData.Data, 0, serviceData.Length);
            }
            return(serviceData);
        }
Beispiel #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);
        }