Android Bluetooth sensor application

Source: Internet
Author: User

Android phones generally use the client's role to actively connect the SPP protocol device (digital sensor connected to the Bluetooth module), the connection process is:

1. Use Registerreceiver to register broadcastreceiver to get Bluetooth status, search device and other messages;
2. Search using Blueadatper;
3. Obtain the Bluetooth device information (such as name, Mac,rssi) in Broadcastreceiver's OnReceive ();
4. Establish a Bluetoothdevice object through the MAC address of the device;

5. Derived from the Bluetoothdevice Bluetoothsocket, prepare sockets to read and write equipment;

6. Select the Connected Protocol/service through the Bluetoothsocket Createrfcommsockettoservicerecord () method, which uses SPP (uuid:0 0001101-0000-1000-8000-00805F9B34FB);
After 7.Connect (the system automatically prompts if not paired), use Bluetoothsocket's getInputStream () and Getoutputstream () to read and write Bluetooth devices.

First take a look at this program run, the selected SPP protocol device is a single-lead ECG acquisition table:

This program contains two activity (Testbluetooth and Wavediagram), Testbluetooth is a search to establish a Bluetooth connection. The use of Bluetoothadapter, Bluetoothdevice, and Bluetoothsocket is simple, except for the functions provided by the first three, which can be controlled by sending messages to the system, such as:

Registration Broadcastreceiver:

Intentfilter Intent = new Intentfilter (); intent.addaction (bluetoothdevice.action_found);// Use Broadcastreceiver to obtain search results intent.addaction (bluetoothdevice.action_bond_state_changed); Intent.addaction ( bluetoothadapter.action_scan_mode_changed); intent.addaction (bluetoothadapter.action_state_changed); Registerreceiver (searchdevices, intent);

Enumerates the contents of all messages in Broadcastreceiver's OnReceive ():

String action = Intent.getaction (); Bundle B = Intent.getextras (); object[] Lstname = B.keyset (). ToArray ();//Displays all received messages and their details for (int i = 0; i < Lstname.lengt H i++) {String keyName = lstname[i].tostring (); LOG.E (KeyName, string.valueof (B.get (KeyName)));}

In DDMS you can see the message returned by Bluetoothdevice.action_found:

Program another activity~~~wavediagram used to read the Bluetooth data and draw a waveform, it is important to note that the Java byte value range is not the same as C + +, Android received the byte data to do "& 0xFF" processing, Data that is equivalent to C + +.

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.