private void connectToServerSocket( BluetoothDevice device, UUID uuid) { 

try{

 BluetoothSocket clientSocket = device.createRfcommSocketToServiceRecord( 
uuid); 

// Block until server connection accepted. 

clientSocket.connect(); 

// Start listening for messages. 

listenForMessages( clientSocket); 

// Add a reference to the socket used to send messages. 

transferSocket = clientSocket; } 

catch (IOException e) { Log.e(" BLUETOOTH", "Bluetooth client I/ O 
Exception", e); } }





private void listenForMessages( 

BluetoothSocket socket, StringBuilder incoming) { 

listening = true; int bufferSize = 1024;

 byte[] buffer = new byte[ bufferSize]; 

try { 

InputStream instream = socket.getInputStream();

 int bytesRead = -1; 

while (listening) {

 bytesRead = instream.read( buffer); 

if (bytesRead != -1) {

 String result = ""; while (( bytesRead = = bufferSize) && (buffer[ 
bufferSize-1] != 0))

{ result = result + new String( buffer, 0, bytesRead - 1); bytesRead = 
instream.read( buffer); } 

result = result + new String( buffer, 0, bytesRead - 1); incoming.append( 
result); } 

socket.close(); } } 

catch (IOException e) { Log.e( TAG, "Message received failed.", e);


} finally { } }



*Meier, Reto (2012-04-05). Professional Android 4 Application Development 
(Wrox Professional Guides) (Kindle Locations 17738-17750). John Wiley and 
Sons. Kindle Edition. *

On Wednesday, October 17, 2012 2:40:01 AM UTC-5, vishnu c wrote:
>
> label:Android
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to