Upgrade thsocket for Android Bluetooth APIs

Source: Internet
Author: User

Previous two articles Article We mentioned the pairing, discovery, activation, and other operations on the Android platform Bluetooth. This article begins to establish a socket for Bluetooth communication through the javasthsocket class. Android 2.0 and later versions support this feature. Bluetooth and lan use MAC addresses to identify remote devices, establish communication connections to RFCOMM channels, and communicate in input and output streams. I. Connecting Devices
Bluetooth communication can be divided into server and client, and different methods of the descrithsocket class are used between them to obtain 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. In Android, The javasthserversocket class is provided to process user information, the server socket makes a response when an accepted client sends a response to the ipvthsocket. Example Code As follows:
Private class acceptthread extends thread {
Private Final effecthserversocket cwjserversocket;
Public acceptthread (){
Export thserversocket TMP = NULL; // use a temporary object instead, because cwjserversocket is defined as final
Try {
TMP = myadapter. listenusingrfcommwithservicerecord (name, cwj_uuid); // The service only listens
} Catch (ioexception e ){}
Cwjserversocket = TMP;
}
Public void run (){
Descrithsocket 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 a connected RFCOMM channel in a separate thread.
Cwjserversocket. Close ();
Break;
}
}
}
Public void cancel () {// cancel the socket connection, and then the thread returns
Try {
Cwjserversocket. Close ();
} Catch (ioexception e ){}
}
}
Here, the android Development Network reminds everyone to note that the server generally handles multiple tasks without being congested and must use an Asynchronous Method. Here we open a thread, at present, the I/O model is not provided on the upper layer of the android Virtual Machine. Here we will explain the performance optimization solution in the case of high load.
2. Act as the client
In order to initialize a device to connect to a remote device, you must first obtain the local thdevice object. The related methods are described in two articles of the sort thadapter class of Android Bluetooth API. I will not repeat them here, the related sample code is as follows:
Private class connectthread extends thread {
Private Final ipvthsocket cwjsocket;
Private Final effecthdevice cwjdevice;
Public connectthread (effecthdevice device ){
Optional thsocket TMP = NULL;
Cwjdevice = device;
Try {
TMP = device. createrfcommsockettoservicerecord (cwj_uuid); // create a client
} Catch (ioexception e ){}
Cwjsocket = TMP;
}
Public void run (){
Myadapter. canceldiscovery (); // cancels remote device discovery, which reduces system performance.
Try {
Cwjsocket. Connect ();
} Catch (ioexception connectexception ){
Try {
Cwjsocket. Close ();
} Catch (ioexception closeexception ){}
Return;
}
Manageconnectedsocket (cwjsocket); // manage a connected RFCOMM channel in a separate thread.
}
Public void cancel (){
Try {
Cwjsocket. Close ();
} Catch (ioexception e ){}
}
}
After the above introduction, we can see that it is relatively convenient and simple to use Bluetooth communication on the Android platform. For specific data-related communications, we will refer to the bluetoothsocket class of the next Android Bluetooth API (2) describes the specific implementation of manageconnectedsocket.
This article is transferred from Www.35java.com

Related Article

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.