Home > Back-end >  Socket/BluetoothSocket getInputStream/getOutputstream problem: android development keeps flashing ba
Socket/BluetoothSocket getInputStream/getOutputstream problem: android development keeps flashing ba

Time:10-03

Because it is a bluetooth chat app, and machine are not on the computer can't debugging, gradually collapse,,
Write the code more small white please forgive me,,


Describe the general problem: (client and server to send and receive the code is the same basic)
Each time you select a bluetooth device, the client to connect to the server, the server accept, open to receive flow thread, the client is open to receive immediately flow thread
Hope is connected in the back-end database information exchange, immediately after the user can't see, after exchanging the database data open message received sent
After two receiving flow is started, I'll call the output function of the database but immediately flashed back every time can't find the reason because I not impossible to debug==should be adjustable, despair,,

Running idea is:
Opens the listener thread AcceptThread
BluetoothServerSocket. The accept () wait for connection, connect the start receiving flow obstruction after receiving data
//jam here, waiting for the other equipment connection 
The socket=mServerSocket. The accept ();
//setListenState (SERVING);//I am server
Sockets. Add (socket);//add list
//out (socket);
Connected (socket, false);
Re=new receiveThread (socket);
Re. The start ();//receiving flow
IsSuc=true;
If (isSuc)
{
CustomD. The show ();//some tip box if confirmed would open the write method all data to the client. Write to the local database
IsSuc=false;
}



Prompt dialog box to confirm key Settings
 customD. SetConfirm (" confirm ", new CustomDialog IOnConfirmListener () {

@ Override
Public void onConfirm (CustomDialog dialog) {
Toast. MakeText (mContext, "confirmed success!" Toast), LENGTH_SHORT), show ();

Try {
MAcceptThread. Re. DBout ();
} the catch (IOException e) {
e.printStackTrace();
}

}

});



(android development)
Startup method of all the contents of the output database
 public void DBout () throws IOException {

The String end="messageEND";
List Other_Row p.f=indAllLocal ();//all query table information
If (other_Row. The size () & gt; 0 {

For (TUsers row: other_Row) {
String t=TtoString (row);//write a method table each line into type string
MOutStream. Write (t.g etBytes ());

}
MOutStream. Write (end. GetBytes ());//end mark
//mOutStream. Flush ();
}


}



Receive the read thread so long re=new receiveThread (socket); Re. The start ();//receiving flow
 public void the run () {
Try {
SetListenState (SERVING);


Byte [] buffer=new byte [MAX_BUFFER_SIZE];
Int bytes;


InputStream tmpIn=null;
OutputStream tmpOut=null;

//from the Socket connection access to read and write data flow interface
Try {
//br=new BufferedReader (new InputStreamReader (socket. GetInputStream (), "utf-8"));
TmpIn=rSocket. GetInputStream ();
TmpOut=rSocket. GetOutputStream ();
} the catch (IOException e) {
SetConnectState (CONNECT_STATE_IDLE);
RSocket=null;
MConnectedThread=null;

return;
}

MInStream=tmpIn;
MOutStream=tmpOut;


//read the data
while (! MUserCancel) {

//jam here, is waiting to read data with flow interface
Bytes=mInStream. Read (buffer);


If (isFirst) {//receiving information database
Byte [] data=https://bbs.csdn.net/topics/new byte (bytes);
System. Arraycopy (data buffer, 0, 0, bytes);
String t1=new String (data);
If (t1) equals (" messageEND ")) {
IsFirst=false;
} else {
TUsers te=StoTuser (t1);
P.c onnected_Insert (te, rSocket getRemoteDevice (). The getAddress ());
continue;
}

}


//ConnectionManager connection status change into CONNECT_STATE_CONNECTED
SetConnectState (CONNECT_STATE_CONNECTED);

If (dd==1) {
MSG="sys: # #" + "welcome" + rSocket. GetRemoteDevice (). The getName () + "enter the chat room, the current number of chat rooms for" + (sockets. The size () + 1);
SendData publishes the event (MSG. GetBytes (StandardCharsets UTF_8), true);//-- -- -- -- -- -- -- -- to enter chat room message
//setListenState (LISTEN_STATE_IDLE);
Dd++;
}


Try {//receive chat messages here

//will read the data passed to focus on its components
If (mConnectionListener!=null & amp; & Bytes & gt; 0 {
Byte [] data=https://bbs.csdn.net/topics/new byte (bytes);
System. Arraycopy (data buffer, 0, 0, bytes);
Byte [] temp;
Byte [] temp1;
MSG=rSocket. GetRemoteDevice (). The getName () + ":";
Temp1=MSG. GetBytes ();
Temp=byteMerger (temp1, data);
//send in addition to the readers of equipment
For (int I=0; I & lt; Sockets. The size (); I++) {
if (! (rSocket. GetRemoteDevice (.) getAddress () equals (sockets) get (I)) getRemoteDevice (). The getAddress ())))
Sockets. Get (I). GetOutputStream (), write (temp);
}

MConnectionListener. OnReadData (temp);

}
} the catch (IOException e) {
//block in the process, if the other local call mSocket. Close (),
//or the other side of the connection is closed
//will enter into this exception
break;
}
}


} the catch (Exception e) {
e.printStackTrace();
}


SetListenState (LISTEN_STATE_LISTENING);
SetConnectState (CONNECT_STATE_IDLE);
}//run



The above is the service side
The client receives is
 try {
Bytes=mInStream. Read (buffer);

//here is want to write the information by the server to the client, the client to write your own database information
If (dd==1) {
DBout ();
Dd++;
}


If (isFirst) {

Byte [] data=https://bbs.csdn.net/topics/new byte (bytes);
System. Arraycopy (data buffer, 0, 0, bytes);
String t1=new String (data);
If (t1) equals (" messageEND ")) {
IsFirst=false;
Dd++;
} else {
TUsers te=StoTuser (t1);
P.c onnected_Insert (te, mSocket getRemoteDevice (). The getAddress ());
continue;
}

}


//ConnectionManager connection status change into CONNECT_STATE_CONNECTED
SetConnectState (CONNECT_STATE_CONNECTED); nullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related