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

            int appearance = -1;
            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultAppearance(ref scanDataStruct, packetType, out appearance);

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

            Marshal.FreeHGlobal(scanDataStruct.AdvData);
            Marshal.FreeHGlobal(scanDataStruct.ScanData);
            return(appearance);
        }
Esempio n. 2
0
        internal string GetLeScanResultDeviceName(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            if (!BluetoothAdapter.IsBluetoothEnabled || !Globals.IsInitialize)
            {
                BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
            }

            string deviceName;
            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetLeScanResultDeviceName(ref scanDataStruct, packetType, out deviceName);

            if (ret == (int)BluetoothError.NoData)
            {
                Log.Info(Globals.LogTag, "No DeviceName 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 Device name- " + (BluetoothError)ret);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                BluetoothErrorFactory.ThrowBluetoothException(ret);
            }

            Log.Info(Globals.LogTag, "DeviceName: " + deviceName);
            Marshal.FreeHGlobal(scanDataStruct.AdvData);
            Marshal.FreeHGlobal(scanDataStruct.ScanData);
            return(deviceName);
        }
Esempio n. 3
0
        internal int GetScanResultTxPowerLevel(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            if (!BluetoothAdapter.IsBluetoothEnabled || !Globals.IsInitialize)
            {
                BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
            }

            int powerLevel = -1;
            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultTxPowerLevel(ref scanDataStruct, packetType, out powerLevel);

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

            Log.Info(Globals.LogTag, "TxPowerLevel: " + powerLevel);
            Marshal.FreeHGlobal(scanDataStruct.AdvData);
            Marshal.FreeHGlobal(scanDataStruct.ScanData);
            return(powerLevel);
        }
Esempio n. 4
0
        internal IList <string> GetScanResultSvcSolicitationUuids(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            IntPtr uuidListArray;
            int    count;

            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScaResultSvcSolicitationUuids(ref scanDataStruct, packetType, out uuidListArray, out count);

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

            IntPtr[] uuidList = new IntPtr[count];
            Marshal.Copy(uuidListArray, uuidList, 0, count);
            IList <string> list = new List <string>();

            foreach (IntPtr uuids in uuidList)
            {
                list.Add(Marshal.PtrToStringAnsi(uuids));
                Interop.Libc.Free(uuids);
            }

            Interop.Libc.Free(uuidListArray);
            return(list);
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        internal IList <string> GetLeScanResultServiceUuids(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            IntPtr uuidListArray = IntPtr.Zero;
            int    count         = -1;

            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultServiceUuid(ref scanDataStruct, packetType,
                                                                 ref uuidListArray, ref count);

            if (ret != (int)BluetoothError.None)
            {
                Log.Info(Globals.LogTag, "Failed to service uuids list- " + (BluetoothError)ret);
                return(null);
            }

            Log.Info(Globals.LogTag, "count of uuids :  " + count);

            IntPtr[] uuidList = new IntPtr[count];
            Marshal.Copy(uuidListArray, uuidList, 0, count);
            IList <string> list = new List <string>();

            foreach (IntPtr uuids in uuidList)
            {
                list.Add(Marshal.PtrToStringAnsi(uuids));
                Interop.Libc.Free(uuids);
            }

            Interop.Libc.Free(uuidListArray);
            return(list);
        }
Esempio n. 7
0
        internal int GetScanResultAppearance(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            int appearance;

            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultAppearance(ref scanDataStruct, packetType, out appearance);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get Appearance value- " + (BluetoothError)ret);
            }
            return(appearance);
        }
Esempio n. 8
0
        internal int GetScanResultTxPowerLevel(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            int powerLevel = -1;
            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultTxPowerLevel(ref scanDataStruct, packetType, out powerLevel);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get tx power level- " + (BluetoothError)ret);
            }
            Log.Info(Globals.LogTag, "TxPowerLevel is --- " + powerLevel);
            return(powerLevel);
        }
Esempio n. 9
0
        internal string GetLeScanResultDeviceName(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            string deviceName;

            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetLeScanResultDeviceName(ref scanDataStruct, packetType, out deviceName);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get Device name- " + (BluetoothError)ret);
                return(null);
            }
            Log.Info(Globals.LogTag, "Device name " + deviceName);
            return(deviceName);
        }
Esempio n. 10
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);
        }
Esempio n. 11
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. 12
0
        internal IList <string> GetScanResultSvcSolicitationUuids(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            if (!BluetoothAdapter.IsBluetoothEnabled || !Globals.IsInitialize)
            {
                BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
            }

            IntPtr uuidListArray;
            int    count;
            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultSvcSolicitationUuids(ref scanDataStruct, packetType, out uuidListArray, out count);

            if (ret == (int)BluetoothError.NoData)
            {
                Log.Info(Globals.LogTag, "No ServiceSolicitationUuids 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 service solicitation uuids " + (BluetoothError)ret);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                BluetoothErrorFactory.ThrowBluetoothException(ret);
            }

            Log.Info(Globals.LogTag, "Count of ServiceSolicitationUuids: " + count);

            IList <string> list = new List <string>();

            IntPtr[] uuidList = new IntPtr[count];
            Marshal.Copy(uuidListArray, uuidList, 0, count);
            foreach (IntPtr uuids in uuidList)
            {
                list.Add(Marshal.PtrToStringAnsi(uuids));
                Interop.Libc.Free(uuids);
            }

            Interop.Libc.Free(uuidListArray);
            Marshal.FreeHGlobal(scanDataStruct.AdvData);
            Marshal.FreeHGlobal(scanDataStruct.ScanData);
            return(list);
        }
Esempio n. 13
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);
        }