J2ME - send and receive on serial connection

Posted on 2005-05-01
Last Modified: 2013-12-14
Hi there,

I am trying to send and recieve serial commands from my phone (N6230) to what is essentially an RS-232 device over Bluetooth.
Basically I need to send the device a single character (e.g "y") and then receive the response.

I already have a basic program which sets up the connection and I know this works to open the connection so far - it is just this next bit i am stuck on.


import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class BTMIDlet extends MIDlet {

    public BTMIDlet() { }
    public void startApp() {

   String ServiceURL = "btspp://0800171C3666:1;master=false;encrypt=false;authenticate=false";
    try {

    StreamConnection connection = (StreamConnection);
    InputStream is = connection.openInputStream();
    OutputStream os = connection.openOutputStream();


   catch(IOException e) {
    protected void destroyApp(boolean unconditional) { }
    protected void pauseApp() {}

Any help anyone can give will be greatly received.
Thanks in advance.

Question by:mingbaden
    LVL 1

    Accepted Solution

    to send data:

    int output_data=2;
     os .write(output_data);
    //use some ASCI code to convert your character into integer

    to read data:
    put the codes inside a runnable.

    int input_data;
    while ( ( input_data= is.readByte()) != -1) {
    //put some code here to convert the received char into int

    hope this helps

    Expert Comment


    Can you share the whole source code with me? I am trying to build up a simple bluetooto demo to send charaters from Pocket PC to a laptop through serial port. When I try to run your sample, I got the service address error. How did you know the service URL is "btspp://0800171C3666:1"? Is it the client service URL or server service URL?



    Expert Comment

    Well, you can any another midlet, called to got know MAC adress of BT devices nearby, append them to form and choose any of them for btspp-address.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article is a step by step guide on how to create a basic PTP link using Ubiquiti airOS devices. This guide can be used on the following Ubiquiti AirMAX devices. Nanostation, Bullets, AirBridge, Nanobeam, NanoBridge to name a few. Please review …
    Today sees the launch of a new case study, focusing on BYOD technologies we have been working with for some time now.  But with the advent of 802.11ac wireless technologies and the story behind our landmark developments, we would like to share this …
    This Micro Tutorial will show you how to maximize your wireless card to its maximum capability. This will be demonstrated using Intel(R) Centrino(R) Wireless-N 2230 wireless card on Windows 8 operating system.
    Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now