The previous article introduced the Android uses the microphone to collect and display the analog signal realization method, this kind of collection method is suitable for without IO control, reads the signal simply. If the sensor itself needs to contain a control circuit (for example, the acquisition of blood oxygen signals requires infrared and infra-red emission), then the sensor itself will need to take a piece of the main control IC, in-chip acquisition and output digital signal. How does an Android phone interact with such a digital sensor without changing its hardware circuitry? The alternative means of communication is USB and Bluetooth, two ways to have the benefit: the USB mode can give the sensor power supply, Bluetooth mode to own power supply; USB interface standard, Bluetooth universal Support SPP protocol. This article chooses the Bluetooth way to do introduction, introduces the Android Bluetooth API as well as the Bluetooth client's usage.
Since Android 2.0, the official has finally released the Bluetooth API (2.0 of the following system's unofficial Bluetooth API can refer to here: http://code.google.com/p/android-bluetooth/). Android phones are typically connected to the SPP protocol device with the client's role (connected to the digital sensor of the Bluetooth module), the connection process is:
1. Use Registerreceiver registration Broadcastreceiver to obtain Bluetooth status, search equipment and other information;
2. Use of Blueadatper search;
3. Obtain the Bluetooth device information (such as name, Mac,rssi) obtained from the search in Broadcastreceiver's onreceive ();
4. To establish a Bluetoothdevice object through the MAC address of the equipment;
5. Bluetoothdevice derived from the Bluetoothsocket, prepare socket to read and write equipment;
6. Through the Bluetoothsocket Createrfcommsockettoservicerecord () method to select the connected Protocol/service, here is the SPP (uuid:0 0001101-0000-1000-8000-00805F9B34FB);
After 7.Connect (System automatically prompts if no match is available), use Bluetoothsocket's getInputStream () and Getoutputstream () to read and write Bluetooth devices.
Let's take a look at the effect of this program, the selected SPP protocol device is a single Guide ECG collection table:
Because of the more code in this article, readers can download the complete code.
This program contains two activity, that is, Testbluetooth and Wavediagram,testbluetooth are search established Bluetooth connections. The use of Bluetoothadapter, Bluetoothdevice, and Bluetoothsocket is simple, in addition to the functions provided by the first three, you can control and obtain Bluetooth information by sending messages to the system, for example:
Register Broadcastreceiver:
Intentfilter Intent = new Intentfilter ();
Intent.addaction (Bluetoothdevice.action_found)//using 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);
In Broadcastreceiver OnReceive () enumerates the contents of all messages:
String action = Intent.getaction ();
Bundle B = Intent.getextras ();
object[] Lstname = B.keyset (). ToArray ();
Displays all messages received 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 Bluetoothdevice.action_found:
Program another activity, that is, Wavediagram used to read Bluetooth data and draw a waveform, here to note that the Java byte range is the same as C + +, Android received the byte data to do "& 0xFF" processing, To the data equivalent of C + +.
Finally, I hope that the example described in this article can help us with the development of Android.