About Bluetooth development

Source: Internet
Author: User

One: what is Bluetooth

1:bluetooth is currently the most widely used wireless communication protocol, the standard for near-range wireless communication. Legend has it that Sweden has a king who loves blueberries so much that his teeth are blue every day, and that during his reign the king was very sociable, able to say that he had a very good relationship with his neighbours, the Bluetooth inventor who felt that Bluetooth was the function of a near-distance communication device, similar to the king, Hence the name Bluetooth.

2: Mainly for short-range equipment communication (10 meters)

3: Wireless headset, wireless mouse, wireless keyboard

    

Bluetooth logo

Two: Bluetooth working flow chart

First two devices must have Bluetooth device or professional point called Bluetooth adapter, mobile phone and computer For example I drew the following flowchart. Second, scan on the phone, scan the surrounding blue Bluetooth device, first find the computer near the phone, and then send it a signal to be paired with Bluetooth, again return a signal that the phone and the computer has been paired successfully, the final pairing can be transferred after the file. This is one of the most basic processes.

Three: The most important two APIs related to Bluetooth

1:buletoothadapter

The object of this class represents the local Bluetooth adapter, which is equivalent to the Bluetooth adapter in the Bluetooth workflow flowchart, that is, if the application is running on the phone, then the Bluetooth adapter on the phone is the local Bluetooth adapter.

2:buletoothdevice

This class of objects represents a remote Bluetooth device, equivalent to a Bluetooth workflow flowchart in the computer's Bluetooth adapter, that is to say that the application is running on the phone, then buletoothdevice represents the remote device you want to connect the Bluetooth adapter above.

Four: Hardware Preparation

Today This example must be running on a phone with Android 2.0SDK above, not running on the emulator, because now the simulator is not able to simulate Bluetooth, so there must be an Android phone, in addition to a computer with a Bluetooth adapter. Mobile phones and computers for pairing, can only be done manually, not through the code is to achieve pairing, because security problems can not be automatically paired with the application, once the pairing is successful, the file can be transferred. How to pair here is not explained.

V: how to pair Bluetooth

I was going to take the phone and the computer as a debugging, but my computer does not have a Bluetooth adapter, so it is replaced with a Bluetooth pen.

1: Insert Phone

If you find that no drive system prompts you to install the driver

2: Download Pea pods

Pea pod will automatically install the corresponding model of USB driver, USB debugging is turned on by default (be sure to turn on the phone's USB debugging), waiting for the installation to complete.

3: Open the devices in Eclipse's DDMS view this area has a digital name for your mobile device.

4: Turn on the "settings" on the phone

5: Select "Wireless and Network"

The Bluetooth small icon on the head of the phone is turned on, indicating that Bluetooth is turned on

6: Scan Pairing

Pick up the Bluetooth pen, open its switch, click on the phone "scan find Device"

7: Request Pairing

Enter the key to request pairing, and then wait for pairing to succeed

VI: Achieve effect

Scan a paired remote Bluetooth device

Code steps

1: Need to declare Bluetooth rights in Androidmainfest.xml

<uses-permission android:name= "Android.permission.BLUETOOTH"/>

2: Get Bluetoothadapter Object

3: Determine if the current device has a Bluetooth device

4: Determine if the Bluetooth device in the current device is turned on

5: Get all Bluetooth device objects that have been paired

Seven: Code

1: Layout file Main.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"
/>
<button
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Scan around Bluetooth devices"
Android:id= "@+id/btn2"
/>
</LinearLayout>

2: Code file Mainactivity.java

Package Com.szy.bluetooth;

Import Java.util.Iterator;
Import Java.util.Set;
Import android.app.Activity;
Import Android.bluetooth.BluetoothAdapter;
Import Android.bluetooth.BluetoothDevice;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

public class Mainactivity extends Activity {
Private Button mybutton = null;
public void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Get button
MyButton = (Button) Findviewbyid (R.ID.BTN2);
Binding listeners
Mybutton.setonclicklistener (New Buttonlistener ());
}

Listener Anonymous Class
Private class Buttonlistener implements Onclicklistener
{
public void OnClick (View v)
{
Get Bluetoothadapter Object
Bluetoothadapter adapter = Bluetoothadapter.getdefaultadapter ();
Determines whether the Bluetoothadapter object is empty, if empty, indicates that the machine does not have a Bluetooth device
if (adapter! = NULL)
{
SYSTEM.OUT.PRINTLN ("This machine has Bluetooth device");
Call the IsEnabled () method to determine if the current Bluetooth device is available
if (!adapter.isenabled ())
{
If the Bluetooth device is not available, create a intent object that is used to initiate an activity that prompts the user to start the Bluetooth adapter
Intent Intent = new Intent (bluetoothadapter.action_request_enable);
StartActivity (Intent);
}
Get all Bluetooth adapter objects that have been paired
set<bluetoothdevice> devices = Adapter.getbondeddevices ();
if (Devices.size () >0)
{
Using iterations
for (Iterator Iterator = Devices.iterator (); Iterator.hasnext ();)
{
Get the Bluetoothdevice object, that is to say, get the paired Bluetooth adapter
Bluetoothdevice device = (bluetoothdevice) iterator.next ();
Get the address of a remote Bluetooth device
LOG.D ("MyTag", device.getaddress ());

}
}
}
Else
{
SYSTEM.OUT.PRINTLN ("No Bluetooth Device");
}
}
}
}

Eight: Do not connect Android phone

Because the Bluetooth device cannot be found, the "No Bluetooth device" is output in the system information in the DDMS view.

 

Nine: Commissioning

We get the address of the Bluetooth adapter of the Bluetooth pen, and we then use the MAC address to establish the communication channel for the file transmission.

Figure 10

About Bluetooth development

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.