Hi
i want to use mobile device bluetooth , i use this code but there is probleam with this method getDiscoveryAgent() . this error shown :

Null pointer java/lang/nullpointer exeption

this is my code :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Vector;
import java.io.*;
import javax.bluetooth.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
 * @author mahdi
 */
public class Midlet extends MIDlet implements CommandListener {

    LocalDevice local = null;
    DiscoveryAgent agent = null;
    List activeDevices = new List("Active Devices", List.IMPLICIT);
    Display display;

    public void startApp() {
        display = Display.getDisplay(this);
        doDeviceDiscovery();
        display.setCurrent(activeDevices);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
    }

    public void doDeviceDiscovery() {
        try {
            local = LocalDevice.getLocalDevice();
        } catch (BluetoothStateException ex) {
            ex.printStackTrace();
        }
        agent = local.getDiscoveryAgent();
    }
}

and this is out put on netbeans :

Smart card communication error 0x80100017
The specified reader is not currently available for use
Using Untrusted simulated domain
"Series 40 5th Edition SDK, Feature Pack 1" Instance #6500500 Ready for Future Connections
"Series 40 5th Edition SDK, Feature Pack 1" Connection Terminated

can any body help me ?
thanks

Dani AI

Generated

The NullPointerException shown by is almost always caused by calling local.getDiscoveryAgent() after LocalDevice.getLocalDevice() failed. Per the JSR-82 API, LocalDevice.getLocalDevice() throws a BluetoothStateException when the Bluetooth controller is absent or cannot be initialized — it does not return null — so catching the exception and continuing lets local remain null and yields the NPE. (docs.oracle.com)

Fix the flow so you only call getDiscoveryAgent() when the local device was obtained. Move the agent assignment into the try-block (or return/abort if getLocalDevice() fails) and report the error to the user instead of swallowing the exception. For example:

LocalDevice local;
DiscoveryAgent agent;
try {
    local = LocalDevice.getLocalDevice();
    agent = local.getDiscoveryAgent();
} catch (BluetoothStateException e) {
    // log/alert and stop discovery (do not call getDiscoveryAgent on a null local)
    return;
}

Emulator configuration is the next thing to check. The Oracle Java ME SDK emulator can simulate JSR-82, but Bluetooth is a configurable feature in the emulator (if disabled, getLocalDevice() will throw a BluetoothStateException). Vendor/device SDKs differ — some device emulators do not expose Bluetooth or require extra setup — so if the Series 40 emulator you picked does not provide Bluetooth, try enabling Bluetooth in the emulator prefs or test on a real handset. (docs.oracle.com)

Finally, once you protect against the NPE and have a working local/agent, implement DiscoveryListener and start an inquiry (as suggested) to discover devices. Checklist: 1) handle BluetoothStateException and abort cleanly, 2) verify emulator Bluetooth settings or run on a real phone, 3) implement DiscoveryListener and startInquiry only after agent is valid.

hi mahdi68
public class Midlet extends MIDlet implements CommandListener
u should write
public class Midlet extends MIDlet implements CommandListener,DiscoveryListener {
and use this one for device discovery

try {
            if (!agent.startInquiry(DiscoveryAgent.GIAC, this)) {
            // Inquiry not started, error handling code here
            }
            } catch (BluetoothStateException bse) {
            // Error handling code here
            }
and there are two method u should use 
deviceDiscovered
inquiryCompleted
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.