×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    42

    Using Bluetooth Enabled Printer using J2ME

    Hello All,

    I want to take the printout from Bluetooth enabled printer using Mobile Phone.[http://palas.in/portable.htm]
    Is it possible using J2ME? If yes then please provide the sample code.

    Thanks,
    Prateek

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Brazil
    Posts
    1,884

    Re: Using Bluetooth Enabled Printer using J2ME

    Yes, it is possible. You must check the way your printer supports bluetooth (protocol stack, profiles, etc) and implement the corresponding code for Java ME app.

    Some links:

    http://today.java.net/pub/a/today/20...bluetooth.html

    http://developers.sun.com/mobility/m...es/bluetooth2/

    http://www.hcilab.org/documents/tuto...her/index.html

    Hope it helps!

    Kind regards,
    Juarez Alvares Barbosa Junior - Brazil

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    2

    Re: Using Bluetooth Enabled Printer using J2ME

    Hi
    I urgently needed code to print data using nokia 603 on bluetooth printer i written BELOW code wich detect printer then i also get paired with mobile but i dose not print any text any body plz verify and tell me what is wrong in my code thanks in advance


    Code


    package hello;

    import java.io.*;
    import java.util.Vector;
    import javax.bluetooth.*;
    import javax.microedition.io.Connector;
    import javax.microedition.io.StreamConnection;
    import javax.microedition.io.StreamConnectionNotifier;
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.MIDlet;


    public class Bluetooth extends MIDlet implements CommandListener,DiscoveryListener {

    LocalDevice bluetooth;
    Form form;
    String blueStr;
    Display disp;
    List activeDevice,activeServices;
    DiscoveryAgent disAgent;
    Form info;
    private Command cmdConnect;
    private Command cmdExit;
    Vector deviceFound;
    private ServiceRecord[] servicesFound = null;
    private Command select;
    private boolean Dflag;
    private String connectionURL;
    private boolean srviceDisFlag;
    private DataInputStream ioDataInputStream;
    StreamConnection sc;
    private Command cmdinfo;

    public void startApp() {
    try {
    disp=Display.getDisplay(this);
    activeDevice = new List("Active Devices", List.IMPLICIT);
    cmdConnect =new Command("Connect",1,0);
    cmdExit =new Command("Exit",2,0);
    activeDevice.addCommand(cmdConnect);
    activeDevice.addCommand(cmdExit);
    activeDevice.setCommandListener(this);
    form=new Form("Bluetooth Device");
    info=new Form(" ");
    info.append("Searching for a device....");
    activeServices=new List("Active Services",List.IMPLICIT);
    activeServices.setCommandListener(this);
    disp.setCurrent(activeDevice);

    bluetooth=LocalDevice.getLocalDevice();
    // bluetooth.setDiscoverable(DiscoveryAgent.LIAC);
    blueStr=bluetooth.getFriendlyName();

    // activeDevice= new List("Devices",Choice.IMPLICIT);
    // System.out.println(blueStr);

    // form.append("Bluetooth Device "+blueStr);
    // form.append(activeDevice);
    disp.setCurrent(activeDevice);
    disAgent=bluetooth.getDiscoveryAgent();
    disp.setCurrent(info);
    deviceFound=new Vector();
    disAgent.startInquiry(DiscoveryAgent.GIAC, this);
    if (Dflag)
    disAgent.cancelInquiry(this);



    } catch (Exception ex) {
    System.out.println("Error "+ex);
    }
    }

    public void dosearchServies(RemoteDevice device) {
    int[] attributes = {0x100, 0x101, 0x102};
    UUID[] uuids;
    UUID RFCOMM_UUID = new UUID(0x1101); // RFCOMM service
    uuids = new UUID[1];
    uuids[0]= RFCOMM_UUID;
    disAgent=bluetooth.getDiscoveryAgent();
    try {

    disAgent.searchServices(null, uuids, device, this);
    // disAgent.selectService(RFCOMM_UUID, ServiceRecord.NOAUTHENTICATE_NOENCRYPT,true);
    // if(srviceDisFlag)
    // disAgent.cancelServiceSearch(1);

    } catch (Exception e) {
    Alert alert1 = new Alert("Error");
    alert1.setString(e.toString());
    disp.setCurrent(alert1);
    }
    }




    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
    {
    try {
    String deviceaddress = null;
    try {
    deviceaddress = btDevice.getBluetoothAddress();//btDevice.getFriendlyName(true);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    String str;
    str=btDevice.getFriendlyName(true);
    //form.append(str);
    activeDevice.append(str, null);
    deviceFound.addElement(btDevice);
    System.out.println("----");
    } catch (IOException ex) {
    ex.printStackTrace();
    }


    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command cmd, Displayable d) {
    String str="";
    if(cmd==cmdConnect)
    {

    int device = activeDevice.getSelectedIndex();


    try{
    str=((RemoteDevice) deviceFound.elementAt(device)).getFriendlyName(true);
    }
    catch(Exception ex){}
    dosearchServies((RemoteDevice) deviceFound.elementAt(device));
    disp.setCurrent(info);
    }
    if (cmd == select ) {

    try {
    int service = activeServices.getSelectedIndex();
    connectionURL = servicesFound[service].getConnectionURL(0, false);

    activeServices.append(connectionURL, null);
    // print(connectionURL);
    // }catch(Exception ex){}
    try {

    StreamConnection connection =(StreamConnection) Connector.open(connectionURL);
    byte buffer[] = new byte[100];
    String msg = "hello there, client";
    InputStream is = connection.openInputStream();
    OutputStream os = connection.openOutputStream();
    OutputStreamWriter ow = new OutputStreamWriter(os, "US-ASCII");
    InputStreamReader ir = new InputStreamReader(is, "US-ASCII");

    ow.write("Hello, serial world!");
    // os.write(msg.getBytes());
    // read data from client
    ow.flush();
    ow.close();
    //Alert alert1 = new Alert("",connectionURL,null,AlertType.INFO);
    //disp.setCurrent(alert1);

    // is.read(buffer);
    connection.close();

    Alert alert = new Alert("","Please Wait Printing is in Progress11",null,AlertType.INFO);
    disp.setCurrent(alert);
    System.out.println("-4-");
    System.out.println("-5-");
    } catch (Exception e) {
    System.out.println("Error "+e);
    Alert alert = new Alert("","Error "+e,null,AlertType.ERROR);
    disp.setCurrent(alert);
    }
    try
    {
    Thread.sleep(200L);
    }
    catch (InterruptedException localInterruptedException)
    {
    }
    int i2 = 0;

    System.out.println("-0-");
    if (this.ioDataInputStream == null)
    this.ioDataInputStream = this.sc.openDataInputStream();
    while (ioDataInputStream.available() > 0)
    i2 = this.ioDataInputStream.read();
    a(i2);
    }
    catch (Exception ex)
    {
    form.append(""+ex);
    }
    // sc.close();


    }
    if (cmd == cmdExit) {
    destroyApp(false);
    }
    }
    private synchronized int a(int p)
    {

    int i=p;
    notifyAll();
    return i;
    }
    // public void deviceDiscovered(RemoteDevice rd, DeviceClass dc) {
    // throw new UnsupportedOperationException("Not supported yet.");
    // }

    public void servicesDiscovered(int i, ServiceRecord[] srs) {

    servicesFound = srs;

    ///activeServices.append(servicesFound.toString(), null);

    for(int j=0;j<srs.length;j++)
    activeServices.append(servicesFound[j].toString(), null);
    select=new Command("Select",1,0);
    cmdinfo=new Command("Info",1,0);
    activeServices.addCommand(select);
    activeServices.addCommand(cmdinfo);
    // activeServices.setSelectCommand(select);
    activeServices.setCommandListener(this);
    }

    public void serviceSearchCompleted(int i, int i1) {
    switch (i1) {
    case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
    srviceDisFlag=true;
    break;
    case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
    break;
    case DiscoveryListener.SERVICE_SEARCH_ERROR:
    break;
    case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
    break;
    case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
    break;
    }
    disp.setCurrent(activeServices);

    }

    public void inquiryCompleted(int discType) {
    Alert dialog = null;
    if (discType != DiscoveryListener.INQUIRY_COMPLETED) {
    dialog = new Alert("Bluetooth Error","The inquiry failed to complete normally",null, AlertType.ERROR);
    }
    else {
    dialog = new Alert("Inquiry Completed","The inquiry completed normally", null,AlertType.INFO);
    }
    dialog.setTimeout(500);
    Display.getDisplay(this).setCurrent(dialog);
    disp.setCurrent(activeDevice);
    Dflag=true;

    }



    }



    Plz try to email me solution on kchari24@gmail.com

  4. #4
    Super Contributor
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    9,570

    Re: Using Bluetooth Enabled Printer using J2ME

    Pairing and discovery is not enough. There must exist (be available) a common, supported profile/protocol suitable for printing or file transfer between the phones. Check both device, as to which Bluetooth profiles they support.

    If the 603 doesn't support the profile that the printer requires, you have to implement the whole thing (if possible, which depends on whether you could code it on top of an existing/supported profile), or find another phone that supports the profile(s) that the printer requires.

    I have no personal experience in writing or even attempting to write code to print stuff from a mobile.

    The Nokia 603 specs are here (under Connectivity, Bluetooth profiles): http://www.developer.nokia.com/Devic...fications/603/

    A2DP, AVRCP 1.0, BIP, DUN, FTP, GAP, GOEP, HFP, HSP, OPP, PBAP 1.0, SAP, SDP, SPP 1.0
    More about Bluetooth profiles and protocols: https://en.wikipedia.org/wiki/Bluetooth_profile


    P.S. When you post code, use the CODE tags so that the code is formatted in a readble way in posts.

  5. #5
    Super Contributor
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    9,570

    Re: Using Bluetooth Enabled Printer using J2ME

    In addition, if both support, e.g., SPP, then you'd actually also need the SPP support to be available via some sort of APIs at the Java level, so check if there are any JSRs that are supposed to allow use of SPP.

    Otherwise, SPP might only be available from the built-in Series 40 apps, and not at all from Java apps.

    The 603 supported JSRs are also listed in the device specs under APIs, Java Technology: http://www.developer.nokia.com/Devic...fications/603/

    The details (specs) of each JSR can be found through:

    http://www.jcp.org/en/home/index

    Nokia paid technical support can be obtained from here, if the forums are not enough to resolve your issue:

    http://www.developer.nokia.com/Resources/Support/

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    4

    Re: Using Bluetooth Enabled Printer using J2ME

    Thanks a lot.. Your code helps me somewhere in my issue for solving Printing functionality in j2me. Thanks a lot..

  7. #7
    Registered User
    Join Date
    Mar 2014
    Location
    Dakar, Sénégal
    Posts
    1

    Re: Using Bluetooth Enabled Printer using J2ME

    Hello Riddhi105,
    Please can you share sample code on how to print using bluetooth in j2me. Thanks

Similar Threads

  1. Problem in sending file to printer via bluetooth
    By madhurahuja in forum Bluetooth Technology
    Replies: 4
    Last Post: 2009-12-02, 11:03
  2. J2SE & J2ME Bluetooth Connection
    By J.J Yan in forum Bluetooth Technology
    Replies: 0
    Last Post: 2008-08-04, 05:34
  3. Nokia 7650 support Bluetooth audo?
    By ovjo12 in forum Bluetooth Technology
    Replies: 5
    Last Post: 2003-07-17, 18:50
  4. Replies: 5
    Last Post: 2003-02-07, 12:25

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×