Windows core programming code analysis based on Visual C ++ (56) Use WinSock to search for Bluetooth devices

Source: Internet
Author: User

Bluetooth is a radio technology that supports short-distance communication (generally 10 m) between devices. Supports Wireless information exchange between mobile phones, PDAs, wireless headsets, laptops, related peripherals, and many other devices. The "Bluetooth" technology can effectively simplify the communication between mobile communication terminal devices, and can also successfully simplify the communication between devices and the Internet, so that data transmission becomes faster and more efficient, broaden the road for wireless communication. Bluetooth adopts a distributed network structure and fast frequency hopping and short packet technology. It supports point-to-point and point-to-point communication and works in the 2.4 GHz ISM (industrial, scientific, and medical) frequencies worldwide. The data rate is 1 Mbps. The Time Division Duplex Transmission scheme is used to achieve full duplex transmission.
Bluetooth is not just a simple technology, but a concept for mobile phones and even the entire IT industry. When the Bluetooth Alliance vowed to embrace the bright future prospects, the industry was shocked. Without the limitations of traditional connections, we can completely enjoy the free fun. The promise given by Bluetooth is exciting.

Bluetooth is an open global specification for wireless data and voice communication. It establishes a special connection for a fixed and mobile device communication environment based on low-cost, short-range wireless connections. The program is written in a 9x9mm microchip. For example, if bluetooth technology is introduced into mobile phones and laptops, you can remove the annoying connection cable between mobile phones and laptops and enable them to communicate over wireless.

Printers, PDAs, desktop computers, fax machines, keyboards, game joystick, and all other digital devices can be part of a Bluetooth system. In addition, Bluetooth wireless technology also provides common interfaces for existing digital networks and peripherals to form a group of personal special connected devices away from the fixed network.

Bluetooth works in the 2.4 GHz ISM (industrial, scientific, and medical) band that is universal worldwide. The data rate of Bluetooth is 1 Mb/s. The Time Division Duplex Transmission scheme is used for full duplex transmission. Use the 802.15 protocol. The ISM frequency band is an open frequency band for all radio systems. Therefore, an unpredictable interference source may occur when a frequency band is used. For example, some household appliances, cordless phones, car room door opener, microwave ovens, etc., may be interference. To this end, Bluetooth has specially designed fast validation and frequency hopping solutions to ensure link stability. The frequency hopping technique divides the frequency band into several hop channels. In a single connection, the radio transceiver follows a certain code sequence (that is, a certain rule, technically, it is called "pseudo-random code", that is, "fake" Random Code) constantly jump from one channel to another. Only the receiving and receiving sides communicate according to this rule, however, other interference may not be able to interfere according to the same rule. The instantaneous bandwidth of the frequency hopping is very narrow, but the narrow band width is expanded to a wide band by hundreds of times through the spectrum extension technology, this reduces the potential impact of interference.

Compared with other systems that work in the same frequency band, Bluetooth provides faster frequency hopping and shorter data packets, making Bluetooth more stable than other systems. The use of FEC (forward error correction, forward correction) suppresses random noise on long-distance links. The frequency-hopping transceiver using the binary frequency modulation (FM) technology is used to suppress interference and prevent fading.

The Bluetooth baseband protocol is a combination of circuit switching and group switching. Synchronous data packets can be transmitted in the reserved time slot, and each data packet is sent at different frequencies. A data packet occupies one time slot in name, but can be extended to five time slots. Bluetooth supports asynchronous data channels and up to three synchronous voice channels at the same time. It also supports simultaneous transmission of asynchronous data and synchronous voice over one channel. Each voice channel supports 64 KB/s synchronous voice link. An asynchronous channel can support asymmetric connections with a maximum rate of 721kb/s at one end and a rate of 57.6kb/s at the other end, or symmetric connections of 43.2kb/s.

 

How can we search for Bluetooth devices on a computer? We use Winsock to search for Bluetooth devices. For more information, see code analysis.

 

# Include "stdafx. H "# include <winsock2.h> # include <ws2bth. h> # pragma comment (Lib, "ws2_32.lib") int _ tmain (INT argc, _ tchar * argv []) {// initialize Winsock wsadatawsadata; if (wsastartup (makeword (2, 2), & wsadata )! = 0) {_ tprintf (_ T ("wsastartup failed with error code: % d \ n"), wsagetlasterror (); return 1 ;} // initialize the Bluetooth enumeration structure DWORD dwwsaqslen = sizeof (wsaqueryset); lpwsaqueryset lpwsaqs = (lpwsaqueryset) heapalloc (getprocessheap (), callback, dwwsaqslen ); lpwsaqs-> dwsize = sizeof (wsaqueryset); lpwsaqs-> dwnamespace = ns_bth; // start to enumerate bluetooth device DWORD keys = lup_containers | lup_flushcache | lup_return_name | lup_r Eturn_addr; handle hservice; If (wsalookupservicebegin (lpwsaqs, dwwsaqsflags, & hservice )! = Socket_error) {bool bfinished = false; while (! Bfinished) {If (condition (hservice, condition, & signature, lpwsaqs) = no_error) {sockaddr_bth * sabth = (sockaddr_bth *) lpwsaqs-> lpcsabuffer-> remoteaddr. lpsockaddr; bth_addr bthaddr = sabth-> btaddr; _ tprintf (_ T ("\ n detected bluetooth device: \ n ")); _ tprintf (_ T ("---------------------- \ n"); _ tprintf (_ T ("NAP: 0x % 04x SAP: 0x % 08x device name: % s \ n "), get_nap (bthaddr), get_sap (bthaddr), lpwsaqs-> lpszserviceinstancename);} else {// switch (wsagetlasterror ()) {// If the given buffer is too small, resubmit case wsaefault: heapfree (getprocessheap (), 0, lpwsaqs); lpwsaqs = (lpwsaqueryset) heapalloc (getprocessheap (), callback, dwwsaqslen ); break; // no more Bluetooth devices; Case wsa_e_no_more: bfinished = true; break; default: // bfinished = true; break ;}} wsalookupserviceend (hservice );} // release the resource heapfree (getprocessheap (), 0, lpwsaqs); wsacleanup (); Return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.