Transferred from: http://blog.csdn.net/pwei007/article/details/6015907
The Android platform supports Bluetooth network protocol stack, which realizes wireless transmission of data between Bluetooth devices.
This document describes how to use the Bluetooth API provided by the Android platform to achieve communication between Bluetooth devices, the communication between Bluetooth devices consists of four steps: Setting up a Bluetooth device, looking for possible or matching devices within the LAN, connecting devices and devices to transfer data. Here are some basic classes that are required to establish a Bluetooth connection:
Bluetoothadapter class: Represents a local Bluetooth adapter. He is the entry point for all Bluetooth interactions. With it you can find other Bluetooth devices, query the bundled device, instantiate a Bluetooth device with a known MAC address, and establish a bluetoothserversocket (as the server side) to listen for connections from other devices.
Bluetoothdevice class: Represents a remote Bluetooth device that uses it to request a remote Bluetooth device connection or to obtain the name, address, kind, and binding status of a remote Bluetooth device. (Its information is encapsulated in Bluetoothsocket).
Bluetoothsocket class: An interface that represents a Bluetooth socket (similar to a socket in TCP), which is the connection point that the application communicates with other Bluetooth devices through the input and output streams.
Blueboothserversocket class: Represents the open service connection to listen for possible connection requests (belonging to the server side), in order to connect two Bluetooth devices must have a device to open a service socket as a server. The Blueboothserversocket class will return a bluetoothsocket when the remote device initiates a connection request and is already connected.
Bluetoothclass class: Describes the general characteristics and capabilities of a Bluetooth device. His read-only attribute set defines the primary, secondary, and related services of the device. However, he did not accurately describe all the Bluetooth files and services supported by the device, but as a small hint of the type of device.
Let's talk about the specific programming implementation:
You must make sure that your device supports Bluetooth and that he can use it. If your device supports Bluetooth, make it available. Of course, there are two ways, one is to turn on Bluetooth in your system settings, and the other is to start the Bluetooth function in your application, the first method will not speak, specifically a second method:
First get the Bluetooth adapter Bluetoothadapter by calling the static method Getdefaultadapter (), and you can use the object later. If the return is empty, the story was over.
Eg:bluetoothadapter mbluetoothadapter = Bluetoothadapter.getdefaultadapter ();
if (Mbluetoothadapter = = null) {
Device does not support Bluetooth
}
Second, call IsEnabled () to query the status of the current Bluetooth device, if returned to false, it means that the Bluetooth device is not turned on, next you need to encapsulate a action_request_enable request to intent inside, Call the Startactivityforresult () method to enable the Bluetooth device, for example:
if (!mbluetoothadapter.isenabled ()) {
Intent enablebtintent = newintent (bluetoothadapter.action_request_enable);
Startactivityforresult (Enablebtintent, REQUEST_ENABLE_BT);
}
At this point, if not unexpectedly, congratulations on your Bluetooth device has been turned on, the next need to look for the surrounding Bluetooth devices may exist.
Find a device:
Using the methods in the Bluetoothadapter class, you can find the remote device (but the range of Bluetooth looks like it is within 10 meters) or the other phone that has been matched (or bound) on your phone. Of course it is necessary to make sure that the other Bluetooth device is turned on or the "enabled" feature is turned on (the other device can be found to be the prerequisite for you to initiate the connection). If the device can be found, it will feed back some of the other device information, such as name, MAC address, etc., using this information, your device can choose to go to the other side to initialize a connection.
If you are connected to the device for the first time, a paired request is automatically displayed to the user. When the device is paired, some of his basic information (primarily name and Mac) is saved and can be read using the Bluetooth API. Using a known MAC address, you can initiate a connection request to a remote Bluetooth device.
Match the difference between the device and the device on the connection: matching just means that the other device found your presence, and has a common identification code, and can be connected. On connection: Indicates that the current device shares a RFCOMM channel and that data can be exchanged between the two. This means that the Bluetooth device must have been paired before establishing the Rfcomm channel.
How to query matching devices:
Before you make a connection, you must query the paired Bluetooth device set (there may be more than one Bluetooth device around you) so that you can choose which device to communicate with, for example you could query all paired Bluetooth devices and use an array adapter to print them out:
set<bluetoothdevice> paireddevices = Mbluetoothadapter.getbondeddevices ();
If There is paired devices
if (paireddevices.size () > 0) {
Loop through paired devices
for (Bluetoothdevice device:paireddevices) {
ADD the name and address to an array adapter to show in a ListView
Marrayadapter.add (Device.getname () + "/n" + device.getaddress ());
}
Setting up a Bluetooth connection requires only a MAC address, which is enough.
Scan device:
To scan the device, simply call the StartDiscovery () method, the scanning process is approximately 12 seconds, the application for Action_found action needs to register a broadcastreceiver to accept the information that the device scanned. For each device, the system broadcasts a action_found action. For example:
Create a broadcastreceiver for Action_found
Private final Broadcastreceiver mreceiver = new Broadcastreceiver () {
public void OnReceive (context context, Intent Intent) {
String action = Intent.getaction ();
When Discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals (ACTION)) {
Get the Bluetoothdevice object from the Intent
Bluetoothdevice device = Intent.getparcelableextra (Bluetoothdevice.extra_device);
ADD the name and address to an array adapter to show in a ListView
Marrayadapter.add (Device.getname () + "/n" + device.getaddress ());
}
}
};
Register the Broadcastreceiver
Intentfilter filter = new Intentfilter (bluetoothdevice.action_found);
Registerreceiver (mreceiver, filter); Don ' t forget to unregister during OnDestroy
Note: The scanning process is a resource-intensive process, and once you find the device you need, make sure your program calls the Canceldiscovery () method to stop the scan before initiating the connection request. Obviously, if you've connected to a previous device, initiating a scan will reduce your communication bandwidth.
Enable to be found:enabling discoverability
If you want to make your device discoverable by other devices, wrap the action_request_discoverable action in Intent and call Startactivityforresult (Intent, int) method is available. He will enable your device to be discovered without leaving your application out of the picture. By default, the enable time is 120 seconds, and of course you can change the enable time by adding the Extra_discoverable_duration field (up to 300 seconds, which is due to the security of information on your device). For example:
Intent discoverableintent = new
Intent (bluetoothadapter.action_request_discoverable);
Discoverableintent.putextra (bluetoothadapter.extra_discoverable_duration, 300);
StartActivity (discoverableintent);
After running the code, a dialog will pop up to prompt you to start the device so that it can be detected (the system will turn it on if your Bluetooth function is not turned on during the process), and if you are ready to discover a connection to the remote device, you do not need to turn on the Enable Device Discovery feature Think that this feature is only needed when your application is on the server side.
Connecting devices:
in your application, to establish a connection between two Bluetooth devices, you must implement both the client and server-side code (since any one device must be available as either a service side or a client). An open service to listen, an initiating connection request (using the server-side device's MAC address). When they all have a Bluetooth socket on the same Rfecomm Channel, it can be thought that they are connected to each other. The server side and the client are in different ways or their Bluetooth sockets. When a connection is heard, the server gets the Bluetooth socket. When the customer can open a frcomm channel to the server side, the client obtains the Bluetooth socket.
Note: During this process, if two Bluetooth devices are not paired well,theandroid system notifies the user via a notification or dialog box. RFCOMM connection requests are blocked before the user chooses. such as:
Connection to the server:
When you want to connect two devices, one must act as a server (by holding an open bluetoothserversocket) to listen for incoming connection requests, When the supervisor hears to provide a connection on the bluetoothsocket to the client, when the client gets from Bluetoothserversocket Bluetoothsocket can destroy bluetoothserversocketlater, unless you still want to listen for more connection requests.
Basic steps for establishing a service socket and listening to a connection:
First, you get the Bluetoothserversocket object by calling the Listenusingrfcommwithservicerecord (String, UUID) method. The parameter string represents the name of the service, and the UUID represents an identity that is connected to the client (a 128-bit format string ID, which is equivalent to a PIN code), and the UUID must match before the connection can be established. Next call the Accept () method to listen for possible connection requests, and when heard, return a Bluetooth socket on the connection bluetoothsocket. Finally, after you hear a connection, you need to call the close () method to close the listener. (General Bluetooth device is a point-to-point transmission)
Note: the Accept () method should not be placed inside the main acitvity because he is a blocking call (the program stops there without having to hear the connection request). The workaround is to create a new thread to manage. For example:
Private class Acceptthread extends Thread {
Private final Bluetoothserversocket Mmserversocket;
Public Acceptthread () {
Use a temporary object, which is later assigned to Mmserversocket,
Because Mmserversocket is final
Bluetoothserversocket tmp = NULL;
try {
My_uuid is the app's UUID string, also used by the client code
TMP = Madapter.listenusingrfcommwithservicerecord (NAME, My_uuid);
} catch (IOException e) {}
Mmserversocket = tmp;
}
public void Run () {
Bluetoothsocket socket = NULL;
Keep listening until exception occurs or a socket is returned
while (true) {
try {
Socket = Mmserversocket.accept ();
} catch (IOException e) {
Break
}
If a connection was accepted
if (socket! = NULL) {
Do work to manage the connection (in a separate thread)
Manageconnectedsocket (socket);
Mmserversocket.close ();
Break
}
}
}
/** 'll cancel the listening socket, and cause the thread to finish */
public void Cancel () {
try {
Mmserversocket.close ();
} catch (IOException e) {}
}
}
Connection to the client:
In order to initialize a connection to a remote device, you first need to obtain a Bluetoothdevice object that represents the device. Get Bluetoothsocket and initialize the connection by bluetoothdevice the object:
Specific steps:
Use the method Createrfcommsockettoservicerecord (UUID) in the Bluetoothdevice object to get Bluetoothsocket. The UUID is the matching code. Then, call the Connect () method to. If the remote device receives the connection, they will share the Rffcomm channel during the communication process, and the Connect () method returns. For example:
Private class Connectthread extends Thread {
Private final Bluetoothsocket Mmsocket;
Private final Bluetoothdevice Mmdevice;
Public Connectthread (Bluetoothdevice device) {
Use a temporary object, which is later assigned to Mmsocket,
Because Mmsocket is final
Bluetoothsocket tmp = NULL;
Mmdevice = device;
Get a bluetoothsocket to connect with the given Bluetoothdevice
try {
My_uuid is the app's UUID string, also used by the server code
TMP = Device.createrfcommsockettoservicerecord (MY_UUID);
} catch (IOException e) {}
Mmsocket = tmp;
}
public void Run () {
Cancel discovery because it'll slow down the connection
Madapter.canceldiscovery ();
try {
Connect the device through the socket. This would block
Until it succeeds or throws an exception
Mmsocket.connect ();
} catch (IOException connectexception) {
Unable to connect; Close the socket and get out
try {
Mmsocket.close ();
} catch (IOException closeexception) {}
Return
}
Do work to manage the connection (in a separate thread)
Manageconnectedsocket (Mmsocket);
}
/** would cancel an in-progress connection, and close the socket */
public void Cancel () {
try {
Mmsocket.close ();
} catch (IOException e) {}
}
Note: the Conncet () method is also a blocking call, which is typically established in a separate thread to invoke the method. Connecting to connect () should not be initiated during device discover, which slows down significantly so that the connection fails. And the data transfer is done only by calling the close () method to close the connection, which saves the system's internal resources.
Manage connections (primarily related to data transfer):
When the device is connected, each device has its own bluetoothsocket. Now you can share the data between devices.
1. Get the input and output stream first by calling the getInputStream () and Getoutputstream () methods. The data is then read or written by calling read (byte[]) and write (byte[]).
2. Implementation details: For read and write operations are blocking calls, need to establish a dedicated ready to manage.
3. Private class Connectedthread extends Thread {
Private final Bluetoothsocket Mmsocket;
Private final InputStream Mminstream;
Private final OutputStream Mmoutstream;
Public Connectedthread (Bluetoothsocket socket) {
Mmsocket = socket;
InputStream tmpin = null;
OutputStream tmpout = null;
Get the input and output streams, using temp objects because
Member streams is final
try {
Tmpin = Socket.getinputstream ();
Tmpout = Socket.getoutputstream ();
} catch (IOException e) {}
Mminstream = Tmpin;
Mmoutstream = Tmpout;
}
public void Run () {
byte[] buffer = new byte[1024]; Buffer store for the stream
int bytes; Bytes returned from read ()
Keep listening to the InputStream until an exception occurs
while (true) {
try {
Read from the InputStream
bytes = mminstream.read (buffer);
Send the obtained bytes to the UI Activity
Mhandler.obtainmessage (Message_read, Bytes,-1, buffer)
. Sendtotarget ();
} catch (IOException e) {
Break
}
}
}
/* Call this from the main Activity to send data to the remote device */
public void write (byte[] bytes) {
try {
Mmoutstream.write (bytes);
} catch (IOException e) {}
}
/* Call this from the main Activity to shutdown the connection */
public void Cancel () {
try {
Mmsocket.close ();
} catch (IOException e) {}
}
}
(turn) Android Bluetooth communication programming