Android Bluetooth sensor application

Source: Internet
Author: User

Generally, Android phones use the client role to actively connect to the SPP protocol device (connect to the digital sensor of the bluetooth module). The connection process is as follows:

1. Use registerReceiver to register BroadcastReceiver to obtain Bluetooth status, search for devices, and other messages;
2. Use BlueAdatper for search;
3. Obtain the bluetooth device information (such as name, MAC, and image service) obtained by searching in onReceive () of BroadcastReceiver );
4. Create a thdevice object through the MAC address of the device;

5. The initthdevice generates the initthsocket and prepares the SOCKET to read and write the device;

6. Use createRfcommSocketToServiceRecord () method of ipvthsocket to select the connection protocol/service. SPP (UUID: 20171101-2017-1000-8000-00805f9b34fb) is used here );
7. After Connect (the system will automatically prompt if no pairing is available), use getInputStream () and getOutputStream () of ipvthsocket to read and write Bluetooth devices.

 

First, let's take a look at the program running in this article. The selected SPP protocol device is a single-homing electric collection table:

 

This program contains two activities (testBlueTooth and wavedi.pdf). testBlueTooth is used for searching to establish a Bluetooth connection. The use of thadapter, ththdevice, and thsocket is very simple. In addition to the functions provided by the first three, you can also control and obtain Bluetooth information by sending messages to the system. For example:

Register BroadcastReceiver:

 

IntentFilter intent = new IntentFilter (); intent. addAction (descrithdevice. ACTION_FOUND); // use BroadcastReceiver to obtain the search result intent. addAction (descrithdevice. ACTION_BOND_STATE_CHANGED); intent. addAction (descrithadapter. ACTION_SCAN_MODE_CHANGED); intent. addAction (descrithadapter. ACTION_STATE_CHANGED); registerReceiver (searchDevices, intent );

 

In BroadcastReceiver's onReceive (), enumerate the content of all messages:

 

String action = intent. getAction (); Bundle B = intent. getExtras (); Object [] lstName = B. keySet (). toArray (); // display all received messages and their details for (int I = 0; I <lstName. length; I ++) {String keyName = lstName [I]. toString (); Log. e (keyName, String. valueOf (B. get (keyName )));}

 

In DDMS, you can see the message returned by delethdevice. ACTION_FOUND:

 

Another Activity of the program ~~~ WaveDiagram is used to read bluetooth data and draw a waveform. Note that the value range of byte in JAVA is different from that in C/C ++, the byte data received by Android must be processed with "& 0xFF" and converted to C/C ++ equivalent data.

Related Article

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.