Android 12th-based Bluetooth sensor applications

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

Last time, we introduced how Android uses a microphone to collect and display analog signals. This collection method is applicable to scenarios where no I/O control is required and signals are read. If the sensor itself needs to contain a control circuit (for example, the acquisition of blood oxygen signals requires infrared and infrared emissions), then the sensor itself needs to carry a Main Control IC to collect and output digital signals in the chip. How can Android phones interact with such digital sensors without changing the hardware circuit? The available communication methods include USB and Bluetooth. The two methods have their own advantages: the USB mode can provide power to the sensor, and the Bluetooth mode must provide its own power. The USB interface standards are different, and the Bluetooth mode generally supports the SPP protocol. This article introduces the android Bluetooth API and the usage of the Bluetooth client.
In Android 2.0, the official release of the Bluetooth API (below 2.0 systems of unofficial Bluetooth API can refer to here: http://code.google.com/p/android-bluetooth ). 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:

The code of this article is much, can download here: http://www.pudn.com/downloads305/sourcecode/comm/android/detail1359043.html

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 (); <br/> intent. addaction (descrithdevice. action_found); // use broadcastreceiver to obtain the search result. <br/> intent. addaction (descrithdevice. action_bond_state_changed); <br/> intent. addaction (descrithadapter. action_scan_mode_changed); <br/> intent. addaction (descrithadapter. action_state_changed); <br/> registerreceiver (searchdevices, intent );

In broadcastreceiver's onreceive (), enumerate the content of all messages:

String action = intent. getaction (); <br/> bundle B = intent. getextras (); <br/> object [] lstname = B. keyset (). toarray (); </P> <p> // display all received messages and their details <br/> for (INT I = 0; I <lstname. length; I ++) {<br/> string keyname = lstname [I]. tostring (); <br/> log. E (keyname, String. valueof (B. get (keyname); <br/>}

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.