Here we useRFCOMM is a simple serial linear simulation protocol that provides RS232 control and status signals.
That is, a serial port (COM) transmission protocol with a baud rate of BITs and 8 bits. Therefore, mobile phone Bluetooth can communicate with other Bluetooth devices.
Two devices need to establish a server and a server to communicate with each other. After the client connects to the server, it can send and receive data.
Source code: Http://download.csdn.net/detail/menghnhhuan/3901081
[HTML] View plaincopyprint?
-
- /* Some constants, representing the name of the server */
-
- Public static final stringProtocol_scheme_l2cap="Btl2cap";
- Public static final stringProtocol_scheme_rfcomm="Btspp";
-
- Public static final stringProtocol_scheme_bt_obex="Btgoep";
-
- Public static final stringProtocol_scheme_tcp_obex="Tcpobex";
-
- Private ipvthserversocketMserversocket=Null;
-
- Private serverthreadStartserverthread=Null; // Enable the service thread
-
- Private clientthreadClientconnectthread=Null; // Client connection thread
-
- Private ipvthsocketSocket=Null;
- Private delethdeviceDevice=Null;
-
- Private readthreadMreadthread=Null; // Read data thread
-
- Private descrithadapterMblustmthadapter=Descrithadapter. Getdefaadapter adapter (); // Bluetooth adapter
/* Some constants, representing the server name */public static final string protocol_scheme_l2cap = "btl2cap"; public static final string protocol_scheme_rfcomm = "btspp "; public static final string protocol_scheme_bt_obex = "btgoep"; public static final string protocol_scheme_tcp_obex = "tcpobex"; private javasthserversocket mserversocket = NULL; private serverthread startserverthread = NULL; // enable the service thread private clientthread clientconnectthread = NULL; // The client connection thread private javasthsocket socket = NULL; private javasthdevice device = NULL; private readthread mreadthread = NULL; // read the data thread private javasthadapter mblustmthadapter = descrithadapter. getdefaadapter adapter (); // Bluetooth adapter
Enable ServerCode
[HTML] View plaincopyprint?
- Startserverthread=NewServerthread ();
- Startserverthread. Start ();
Startserverthread = new serverthread (); startserverthread. Start ();
Enable Client
[HTML] View plaincopyprint?
- Device=Mblustmthadapter. Getremotedevice (Address );
- Clientconnectthread=NewClientthread ();
- Clientconnectthread. Start ();
Device = mblustmthadapter. getremotedevice (Address); clientconnectthread = new clientthread (); clientconnectthread. Start ();
Server code
[HTML] View plaincopyprint?
-
- // Enable the server
-
- Private class serverthread extends thread {
- Public void run (){
-
- Try {
-
- /* Create a Bluetooth server
-
- * Parameters: Server Name and UUID */
-
- Mserversocket=Mblustmthadapter. Listenusingrfcommwithservicerecord (protocol_scheme_rfcomm,
- UUID. fromstring ("20171101-0000-1000-8000-00805f9b34fb "));
-
- Log. D ("server", "Wait cilent connect ...");
-
-
- /* Accept client connection requests */
-
- Socket=Mserversocket. Accept ();
- Log. D ("server", "accept success! ");
-
-
- // Start receiving data
-
- Mreadthread=NewReadthread ();
-
- Mreadthread. Start ();
-
- } Catch (ioexception e ){
- // Todo auto-generated Catch Block
-
- E. printstacktrace ();
-
- }
-
- }
-
- };
// Enable server private class serverthread extends thread {public void run () {try {/* to create a Bluetooth server * parameters: Server Name, UUID */mserversocket = mbluw.thadapter. listenusingrfcommwithservicerecord (protocol_scheme_rfcomm, UUID. fromstring ("20171101-0000-1000-8000-00805f9b34fb"); log. D ("server", "Wait cilent connect... ");/* accept client connection requests */socket = mserversocket. accept (); log. D ("server", "accept success! "); // Start receiving data mreadthread = new readthread (); mreadthread. start ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}};
Client code:
[HTML] View plaincopyprint?
-
- // Enable the client
-
- Private class clientthread extends thread {
- Public void run (){
-
- Try {
-
- // Create a socket connection: only the uuid of the server at registration is required.
-
- //Socket=Device. Createrfcommsockettoservicerecord (export thprotocols. obex_object_push_protocol_uuid );
- Socket=Device. Createrfcommsockettoservicerecord (UUID. fromstring ("20171101-0000-1000-8000-00805f9b34fb "));
-
- // Connection
-
- Socket. Connect ();
-
- // Start receiving data
- Mreadthread=NewReadthread ();
-
- Mreadthread. Start ();
-
- }
-
- Catch (ioexception E)
-
- {
-
- Log. E ("Connect", "", e );
- MessageMSG=NewMessage ();
-
- MSG. OBJ="An error occurred while connecting to the server! Disconnect and try again. ";
-
- MSG. What=0;
-
- Linkdetectedhandler. sendmessage (MSG );
- }
-
- }
-
- };
// Enable Client Private class clientthread extends thread {public void run () {try {// create a socket connection: only the uuid ID of the server at registration/socket = device. createrfcommsockettoservicerecord (fig. obex_object_push_protocol_uuid); socket = device. createrfcommsockettoservicerecord (UUID. fromstring ("20171101-0000-1000-8000-00805f9b34fb"); // connect to the socket. connect (); // start receiving data mreadthread = new readthread (); mreadthread. start ();} Ca Tch (ioexception e) {log. E ("Connect", "", e); message MSG = new message (); msg. OBJ = "An error occurred while connecting to the server! Disconnect and try again. "; MSG. What = 0; linkdetectedhandler. sendmessage (MSG );}}};
Code for sending and receiving data
[HTML] View plaincopyprint?
- // Send data
-
- Private void sendmessagehandle (string MSG)
-
- {
-
- If (Socket= NULL)
-
- {
-
- Toast. maketext (mcontext, "No connection", Toast. length_short). Show ();
-
- Return;
-
- }
- Try {
-
- OutputstreamOS=Socket. Getoutputstream ();
-
- OS. Write (msg. getbytes ());
-
- } Catch (ioexception e ){
-
- // Todo auto-generated Catch Block
-
- E. printstacktrace ();
- }
-
- }
-
- // Read data
-
- Private class readthread extends thread {
-
- Public void run (){
-
-
- Byte []Buffer=NewByte [2, 1024];
- Int bytes;
-
- InputstreamMminstream=Null;
-
-
- Try {
-
- Mminstream=Socket. Getinputstream ();
- } Catch (ioexception E1 ){
-
- // Todo auto-generated Catch Block
-
- E1.printstacktrace ();
-
- }
-
- While (true ){
-
- Try {
- // Read from the inputstream
-
- If ((Bytes=Mminstream. Read (buffer ))>0)
-
- {
- Byte []Buf_data=NewByte [bytes];
-
- For (intI=0; I<Bytes; I ++)
-
- {
- Buf_data [I] = buffer [I];
-
- }
-
- StringS=NewString (buf_data );
- MessageMSG=NewMessage ();
-
- MSG. OBJ=S;
-
- MSG. What=1;
- Linkdetectedhandler. sendmessage (MSG );
-
- }
-
- } Catch (ioexception e ){
-
- Try {
-
- Mminstream. Close ();
- } Catch (ioexception E1 ){
-
- // Todo auto-generated Catch Block
-
- E1.printstacktrace ();
-
- }
-
- Break;
- }
-
- }
-
- }
-
- }
Source: http://blog.csdn.net/menghnhhuan/article/details/7057484