hi folks,

i want to develop a desktop application which will continuously receive text commands from a connected mobile device, via bluetooth rfcomm protocol.

i went through hundreds of code examples, including jsr-82 bluetooth & obex api demos; but cannot get what i wanted.

one of the popular examples that i am trying to deal with is here;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; 
import javax.bluetooth.*;
import javax.microedition.io.*;
 
/**
* Class that implements an SPP Server which accepts single line of
* message from an SPP client and sends a single line of response to the client.
*/
public class SampleSPPServer {
    //private static LocalDevice localDevice;
    static LocalDevice localDevice;
    DiscoveryAgent agent;
   
    //start server
    private void startServer() throws IOException{
 
        //Create a UUID for SPP
        UUID uuid = new UUID(0x1101);
        //Create the servicve url
        String connectionString = "btspp://localhost:" + uuid +";name=SampleSPPServer";        
        
        //open server url
        StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );
       
        //Wait for client connection
        System.out.println("\nServer Started. Waiting for clients to connect...");
        StreamConnection connection=streamConnNotifier.acceptAndOpen();
         
        RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
        System.out.println("Remote device address: "+dev.getBluetoothAddress());
        System.out.println("Remote device name: "+dev.getFriendlyName(true));
       
        //read string from spp client
        InputStream inStream=connection.openInputStream();
        BufferedReader bReader=new BufferedReader(new InputStreamReader(inStream));
        String lineRead=bReader.readLine();
        System.out.println(lineRead);   
        
 
    }
 
 
    public static void main(String[] args) {
       
        //display local device address and name
        try{
            localDevice = LocalDevice.getLocalDevice();
            System.out.println("Address: "+localDevice.getBluetoothAddress());
            System.out.println("Name: "+localDevice.getFriendlyName());
        }catch(Exception e){            
            System.err.println(e.toString());
            System.err.println(e.getStackTrace());
            e.printStackTrace();}        
        
        try{
            SampleSPPServer sampleSPPServer=new SampleSPPServer();
            sampleSPPServer.startServer();
        }catch(Exception e){
            System.err.println(e.toString());
            System.err.println(e.getStackTrace());
            e.printStackTrace();}
       
    }
}

this simple example throws nullpointerexception at localDevice related line 51. if i comment out line 51 & 52, another nullpointerexception is thrown at streamConnecion related line 30.

i have CSR Usb Bt device by WIDCOMM which works regularly, and i am using netbeans 6.1 with j2me wtk 2.5.2 libraries.

what i needed is, a small j2se application that will receive string via bt. can you help me, please?

---
den

Recommended Answers

All 5 Replies

Is your bluetooth device working ? I mean can you use some default application and discover all bluetooth enabled device in the surounding ?

thanks for the reply, yes, it is working properly. i am able to search bt enabled devices, discover services and establish a serial connection over bt com ports, via default bt configuration tool.

for reference, my nokia 6670 can connect to nokia pc suit and its available for file browsing/transfer. moreover, i also use my mobile phone as a bt webcam via an application called wwigo.

so i am sure that there is no failure on the bt adapter, and it's configuration, and working without port conflict.

what may the other causes of the exception?
at least, could somebody post a working piece of code which meets my requirements(simple bluetooth text receiver)?

update:
i've rolled back windows bluetooth drivers, and replaced standard bluetooth api into bluecove jsr-82 for j2se implementation.
so far, no exceptions thrown during the initialization of localdevice, and it works fine as it prints my bt address and alias.

the only problem left is that;

[B]StreamConnection connection=streamConnNotifier.acceptAndOpen();[/B]

this line (#30) always throws nullpointerexception.


here what netbeans outputs after running the code;

init:
deps-jar:
compile:
run:
BlueCove version 2.1.0 on winsock
Address: 00046184C2A3
Name: REVOLTER

Server Started. Waiting for clients to connect...
Exception in thread "main" java.lang.NullPointerException
        at SampleSPPServer.startServer(SampleSPPServer.java:30)
        at SampleSPPServer.main(SampleSPPServer.java:79)
BlueCove stack shutdown completed
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

i have developed BT application.In this PC is acting as a server and phone as client its working perfectly.But i am not able to handle multiple connections for example if i want to connect more than one client then it ll not connect.. please help me in this . Its better if u post the code

Hi revolter00,

Were you able to resolve this issue?
I am using a similar piece of code. But for me acceptAndOpen() does not return .
the output i get is : BlueCove version 2.1.0 on winsock

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.