We mentioned in the previous two articles about the Android platform Bluetooth pairing, discovery, enabling and other operations, this article began to create a socket with Bluetooth communication through the Bluetoothsocket class. Starting from Android 2.0 to support this feature, Bluetooth and LAN like the MAC address to identify the remote device, the establishment of communication connection Rfcomm channel in the input, output flow mode communication.
First, the connection equipment
Bluetooth communication is divided into server and client clients, and they use different methods of Bluetoothsocket classes to get data.
1. As a server
If a device needs to be connected to two or more devices, it needs to be transmitted as a server, and the Bluetoothserversocket class is provided in Android to handle the user's message, and the server-side socket is accepted (accepted) Respond to a bluetoothsocket when a customer sends it. The sample code is as follows:
Private class Acceptthread extends Thread {
Private final Bluetoothserversocket Cwjserversocket;
Public Acceptthread () {
Bluetoothserversocket tmp = NULL; Use a temporary object instead, because Cwjserversocket is defined as final
try {
TMP = Myadapter.listenusingrfcommwithservicerecord (NAME, Cwj_uuid); Service Monitoring only
catch (IOException e) {}
Cwjserversocket = tmp;
}
public void Run () {
Bluetoothsocket socket = NULL;
while (true) {//Keep the connection until an exception occurs or the socket returns
try {
Socket = Cwjserversocket.accept (); If a connection agrees
catch (IOException e) {
Break
}
if (socket!= null) {
Manageconnectedsocket (socket); Manage an already connected Rfcomm channel on a separate thread.
Cwjserversocket.close ();
Break
}
}
}
public void Cancel () {//Cancel socket connection, and then the thread returns
try {
Cwjserversocket.close ();
catch (IOException e) {}
}
}
Here the Android Development Network reminds you of the need to note that the server generally handle multiple tasks not tender blocking, must use asynchronous method here we have a thread, the current Android virtual machine does not provide the upper I/O model, here we will explain the high load situation performance optimization solution.
2. As a client
To initialize a connection to a remote device, you must first obtain the local Bluetoothdevice object, and the related methods are mentioned in two articles of the Bluetoothadapter class of our Android Bluetooth API, and the relevant sample code is as follows:
Private class Connectthread extends Thread {
Private final Bluetoothsocket Cwjsocket;
Private final Bluetoothdevice Cwjdevice;
Public Connectthread (Bluetoothdevice device) {
Bluetoothsocket tmp= null;
Cwjdevice= device;
try {
tmp= Device.createrfcommsockettoservicerecord (CWJ_UUID); Client-side Creation
catch (IOException e) {}
cwjsocket= tmp;
}
public void Run () {
Myadapter.canceldiscovery (); Cancel discovery of remote devices, which can degrade system performance
try {
Cwjsocket.connect ();
catch (IOException connectexception) {
try {
Cwjsocket.close ();
catch (IOException closeexception) {}
Return
}
Manageconnectedsocket (Cwjsocket); Manage an already connected Rfcomm channel on a separate thread.
}
public void Cancel () {
try {
Cwjsocket.close ();
catch (IOException e) {}
}
}
After the introduction we can see that the use of Bluetooth communication on the Android platform is relatively convenient and simple, about the specific data communication we will be in the next Android Bluetooth API Bluetoothsocket Class (2) Talk about the concrete realization of manageconnectedsocket.