示例#1
0
        /// <summary> Inquire devices from the surrounding area, uses a native method to achieve good inquiry. </summary>
        /// <returns> A <c>DevicePool</c>, in which all the devices are added, if any. </returns> 
        public DevicePool Inquire()
        {
            this.dev_id = hci_get_route(IntPtr.Zero);
            if( this.dev_id != -1)
            {
            int sock = hci_open_dev(this.dev_id);
            DevicePool pool = new DevicePool();
            inquiry_info info = new inquiry_info();
            IntPtr devs = IntPtr.Zero;
            byte[] lap = new byte[] { 0x33, 0x8b, 0x9e};
            int max_rsp = 255;
            int length = 8;
            int flags = 1;
            int count = hci_inquiry(this.dev_id, length, max_rsp, lap, out devs,flags);
            if(!(devs==IntPtr.Zero))
            {
            IntPtr element = devs;
            if (count>0)
            {
                for(int i = 0; i < count;i++)
                {
                    //try
                    //{

                    info = (inquiry_info)Marshal.PtrToStructure(element,typeof(inquiry_info));
                    element = (IntPtr)((int)element+Marshal.SizeOf(typeof(inquiry_info)));
                    System.Text.StringBuilder bld2 = new System.Text.StringBuilder(248);
                    monotooth.BluetoothAddress ba = new BluetoothAddress();
                    ba.Array = info.bdaddr;
                    if(hci_read_remote_name(sock,ba,bld2.Capacity,bld2,0)==0)
                    {
                        LinuxRemoteDevice dev = new LinuxRemoteDevice(ba,bld2.ToString());
                        pool.Add(dev);
                    } else {
                        LinuxRemoteDevice dev = new LinuxRemoteDevice(ba,"NONAME");
                        pool.Add(dev);
                    }
                }
            }
            }
            close(sock);
            return pool;
            } else
            {
            throw new Exception("You do not have a bluetooth device on your system.");
            }
        }
示例#2
0
        // Implemented functions from IDevice
        public DevicePool Inquire()
        {
            DevicePool pool = new DevicePool();
            IntPtr lpwsaqueryset = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WSAQUERYSET)));
            WSAQUERYSET wsaqueryset = new WSAQUERYSET();
            wsaqueryset.dwSize = Marshal.SizeOf(typeof(WSAQUERYSET));
            wsaqueryset.dwNameSpace = 16;
            WSAData data = new WSAData();
            data.wHighVersion = 2;
            data.wVersion = 2;
            Marshal.StructureToPtr(wsaqueryset,lpwsaqueryset,false);
            int flags = (int)0x0002;
            flags |= (int)(0x1000|0x0010|0x0100);
            Int32 handle = 0;

            int result = 0;

            result = WSAStartup(36,data);
            if(result != 0)
            {
                throw new Exception("WSA Error, make sure you have the newest version (at least 2.2) of Winsock2!");
            }
            result = WSALookupServiceBegin(wsaqueryset,flags,ref handle);

            if (result == -1)
            {
                int error = WSAGetLastError();
                if (error == 10108) // No device attached
                {
                    throw new Exception("You do not have a bluetooth device on your system.");
                }
            }
            while (0 == result)
            {
                Int32 dwBuffer = 0x10000;
                IntPtr pBuffer = Marshal.AllocHGlobal(dwBuffer);

                WSAQUERYSET qsResult = new WSAQUERYSET() ;

                result = WSALookupServiceNext(handle, flags,
                ref dwBuffer, pBuffer);

                if (0==result)
                {
                    Marshal.PtrToStructure(pBuffer, qsResult);
                    CS_ADDR_INFO addr = new CS_ADDR_INFO();
                    Marshal.PtrToStructure(qsResult.lpcsaBuffer, addr);
                    sockaddr sa = new sockaddr();
                    Marshal.PtrToStructure(addr.RemoteAddr.lpSockaddr,sa);
                    monotooth.BluetoothAddress ba = new BluetoothAddress();
                    for (int i = 5; i >= 0; i--)
                    {
                        ba.Array[i] = sa.sa_data[i];
                    }
                    WindowsRemoteDevice dev = new WindowsRemoteDevice(ba, qsResult.szServiceInstanceName);
                    pool.Add(dev);

                } else
                {
                WSALookupServiceEnd(handle);
                }
                Marshal.FreeHGlobal(pBuffer);
            }

            Marshal.FreeHGlobal(lpwsaqueryset);
            WSACleanup();
            return pool;
        }