*Creating a Bluetooth client socket*

**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); } }


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




*Sending and receiving strings using Bluetooth Sockets* 

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 17737-17751). John Wiley and 
Sons. Kindle Edition. *

On Tuesday, October 9, 2012 10:07:47 PM UTC-5, akash roy wrote:
>
> i am currently on a project and i need to share files from one device to 
> another with the help of bluetooth. so i thought of changing the codes from 
> the bluetooth chat demo that is available in android developer's site .
> actually i need to simply send a text file . but i am getting some issues 
> in that code due to which i could not rectify my problem. so if any of you 
> can send me a sample for sharing files using bluetooth then i think then it 
> could be a great help to me.
>
> Thankyou
>

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

Reply via email to