Here is the code that i wrote to connect with printer socket.
I hope u will reply me ASAP,bcz i have to release my application.

package com.ob;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Set;
import java.util.UUID;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.util.Log;

public class BluetoothPaired extends Activity {

private static final int REQUEST_ENABLE_BT = 2;
private BluetoothAdapter bluetoothAdapter;
private UUID applicationUUID = java.util.UUID.randomUUID();
private static final String logTag = "BluetoothTest";
boolean rePrintFlag;
int offenceCount = 0;
int licOffenceCount = 0;
static int dataSize;
int status = 0x00;
static int[] dataPacket = new int[2000];
String chain="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
String key = "ranjeetkumar1984";
char[] szEncData = new char[16];
public static DataOutputStream dos = null;
  String Devicesfound =null;
  UUID MY_UUID=null;
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null) {
        Log.d(logTag, "Could not get bluetooth adapter");
        return;
    }
    searchForDevices();
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_ENABLE_BT) {
        if (requestCode == RESULT_OK) {
            Log.d("BluetoothTest", "bluetooth enabled");
            searchForDevices();
        } else {
            Log.d("BluetoothTest", "Could not enable bluetooth
device");
        }
    }

}

public void lineReadFromBluetoothDevice(String line) {
    Log.d(logTag, "Mottok: " + line);
}

private void searchForDevices() {

        Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
300);
        startActivity(discoverableIntent);
        bluetoothAdapter.startDiscovery();

    if (bluetoothAdapter.isEnabled()) {
        Set<BluetoothDevice> devicesAvailable =
bluetoothAdapter.getBondedDevices();
        System.out.println("set of devices available
=========="+devicesAvailable);
        if(devicesAvailable.size() >0)
                {
                for ( BluetoothDevice device : devicesAvailable) {
                         Devicesfound +=device.getAddress()+"~";
                         System.out.println("=============="+device.getName()+"
"+device.getAddress()+"\n");
                   }
                }
        if (devicesAvailable.isEmpty()) {
            informUserNoDevicesPaired();
        } else {
            askUserToPickDeviceToUse(devicesAvailable);
        }
    } else {
        Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
}

private void askUserToPickDeviceToUse(Set<BluetoothDevice>
devicesAvailable) {

          for (Iterator iterator = devicesAvailable.iterator();
iterator.hasNext();) {
                BluetoothDevice bluetoothDevice = (BluetoothDevice) 
iterator.next();
                if(bluetoothDevice.getAddress().equalsIgnoreCase("00:0A:3A:
74:61:4A"))
                {
                         pairToDevice((BluetoothDevice)bluetoothDevice );
                }
        }

}

private void informUserNoDevicesPaired() {
    Builder dialogBuilder = new AlertDialog.Builder(this);
    dialogBuilder.setMessage("No user device paired");
    dialogBuilder.setPositiveButton("Ok", new
DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int arg1) {
            dialog.dismiss();

        }
    });
    dialogBuilder.setOnCancelListener(new OnCancelListener() {

        public void onCancel(DialogInterface dialog) {
            dialog.dismiss();
            finish();
        }
    });
    dialogBuilder.show();
}

private void showError(String message) {
    Builder dialogBuilder = new AlertDialog.Builder(this);
    dialogBuilder.setMessage("Error" + message);
    dialogBuilder.setOnCancelListener(new OnCancelListener() {

        public void onCancel(DialogInterface dialog) {
            dialog.dismiss();
            finish();
        }
    });
    dialogBuilder.show();
}

private void pairToDevice(BluetoothDevice bluetoothDevice) {
        System.out.println("Socket opening for sending data");
    openSocket(bluetoothDevice);
}

private void openSocket(BluetoothDevice bluetoothDevice) {
    try {

        final ProgressDialog dialog = new ProgressDialog(this);
        final ConnectRunnable connector = new
ConnectRunnable(bluetoothDevice, dialog);
/*        dialog.show(this, "Connecting to device ::::\n" +
bluetoothDevice.getName() + " : " + bluetoothDevice.getAddress(),"",
                true, true,
                new OnCancelListener() {

                    public void onCancel(DialogInterface dialog) {
                        connector.cancel();
                    }
                });
*/
        new Thread(connector).start();

    } catch (IOException ex) {
        Log.d(logTag, "Could not open bluetooth socket", ex);
        showError("Kunne ikke åpne socket grunnet feil: " +
ex.getMessage());
    }
}

private void closeSocket(BluetoothSocket openSocket) {
    try {
        openSocket.close();
    } catch (IOException ex) {
        Log.d(logTag, "Could not close exisiting socket", ex);
    }
}

private void startListeningForInput(BluetoothSocket socket) {
        System.out.println("In startListeningForInput method=============");
    new Thread(new InputReader(socket)).start();

}

private void dismissDialog(final Dialog dialog) {
    runOnUiThread(new Runnable() {

        public void run() {
            dialog.dismiss();
        }
    });
}

private class ConnectRunnable implements Runnable {


    private final ProgressDialog dialog;
    private final BluetoothSocket socket;

        public ConnectRunnable(BluetoothDevice device, ProgressDialog
dialog) throws IOException {
        System.out.println("Creating new socket=============");
        MY_UUID =
UUID.fromString("00001101-0000-1000-8000-00805F73648E");
        socket = device.createRfcommSocketToServiceRecord(MY_UUID);
        socket.getInputStream();
        System.out.println("device ================="+device+"
"+socket.getInputStream());
        this.dialog = dialog;
    }

    public void run() {
                        System.out.println("Connecting to new 
socket=============");
                        BluetoothDevice btdevice=socket.getRemoteDevice();
                        System.out.println("Remote device
==========="+socket.getRemoteDevice());

        try {

            socket.connect();

            } catch (IOException connectException) {
            Log.d(logTag, "Could not connect to socket",
connectException);
            closeSocket(socket);
            return;
        }
        bluetoothAdapter.cancelDiscovery();
        startListeningForInput(socket);
        dismissDialog(dialog);
    }

    public void cancel() {
        try {
            socket.close();
        } catch (IOException e) {
            Log

Thanks in advance.

On Jun 18, 10:28 pm, Rocky <[email protected]> wrote:
> You need CS architecture, post the code, then we can understand where is the
> exact problem.
>
>
>
>
>
>
>
>
>
> On Sat, Jun 18, 2011 at 7:37 PM, ABS <[email protected]> wrote:
> > Hello everyone,
> >             I am new on android platform, working on one traffic
> > control app. In this app i have to connect with bluetooth printer
> > through android device, i am trying to do this connection with
> > Bluetooth socket.
> >            But problem is that i am getting IOexception while
> > connecting with printer socket.
>
> > Just i have to send data to print,Do i need to use Server and client
> > architecture?
>
> >  exception is as follows
>
> > 06-18 19:15:56.943: DEBUG/BluetoothTest(17386): java.io.IOException:
> > Service discovery failed
> > 06-18 19:15:56.943: DEBUG/BluetoothTest(17386):     at
> > android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:
> > 383)
> > 06-18 19:15:56.943: DEBUG/BluetoothTest(17386):     at
> > android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:193)
> > 06-18 19:15:56.943: DEBUG/BluetoothTest(17386):     at
> > com.ob.BluetoothPaired$ConnectRunnable.run(BluetoothPaired.java:236)
> > 06-18 19:15:56.943: DEBUG/BluetoothTest(17386):     at
> > java.lang.Thread.run(Thread.java:1096)
>
> >  I stuck there if anyone know solution reply me ASAP
>
> > thanks in advance
>
> > --
> > 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
>
> --
> Thanks & Regards
>
> Rakesh Kumar Jha
> Software Developer
> Symphony Services Corp (India) Pvt Ltd
> Bangalore
> (O) +918030274295
> (R) +919886336619

-- 
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