The example below lists the devices that are remotely available from local device. Also it list the devices that has bluetooth turned off.

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

public class BluetoothMidlet extends MIDlet implements 
        CommandListener, Runnable, DiscoveryListener {
    
    private Display display;
    private Command discover,exit;
    LocalDevice local;
    private DiscoveryAgent discoveryAgent;
    private Hashtable bluetoothDevices = new Hashtable();
    List deviceList;
    
    public BluetoothMidlet() {
        
        display = Display.getDisplay(this);
        discover = new Command("Discover",Command.SCREEN, 0);
        exit = new Command("Exit",Command.EXIT, 0);
        
        // Device List
        deviceList = new List("Select Device", List.IMPLICIT);
        deviceList.addCommand(exit);
        deviceList.setCommandListener(this);
        
    }
    
    public void startApp() {
        
        Form form = new Form("Devices");
        form.addCommand(discover);
        form.addCommand(exit);
        form.setCommandListener(this);
        display.setCurrent(form);
        
    }
    
    public void pauseApp() {}
    
    public void destroyApp(boolean unconditional) {}
    
    private void doBluetoothDiscovery() {
        
        Thread t = new Thread(this);
        t.start();
        
    }
    
    // Implement Runnable
    public void run() {
        
        bluetoothDiscovery();
        
    }
    
    public void printString(String s) {
        
        System.out.println(s);
        
    }
    
    private void bluetoothDiscovery() {
        
        LocalDevice localDevice;
        
        try {
            
            localDevice = LocalDevice.getLocalDevice();
            
        } catch (BluetoothStateException e) {
            
            printString("BluetoothStateException: " + e);
            return;
            
        }
        
        discoveryAgent = localDevice.getDiscoveryAgent();
        
        RemoteDevice devices[];
        
        try {
            
            devices = discoveryAgent.retrieveDevices(DiscoveryAgent.CACHED);
            
            if (devices != null) {
                
                for (int i=0; i < devices.length; i++) {
                    bluetoothDevices.put(devices[i].getFriendlyName(false), 
                            devices[i]);
                    
                    printString("Device (cached): " + d
                            evices[i].getFriendlyName(false) +
                            " " + devices[i].getBluetoothAddress() );
                }
            }
            
            devices = discoveryAgent.retrieveDevices(DiscoveryAgent.PREKNOWN);
            
            if (devices != null) {
            
                for (int i=0; i < devices.length; i++) {
                    
                    bluetoothDevices.put(devices[i].getFriendlyName(false), 
                            devices[i]);
                    
                    printString("Device (cached): " + 
                            devices[i].getFriendlyName(false) +
                            " " + devices[i].getBluetoothAddress() );
                    
                }
            }
            
        } catch (IOException e) {
            printString("Exception (b2): " + e);
        }
        
        try {
            
            discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
            
        } catch (BluetoothStateException e) {
            printString("Exception (b3): " + e);
        }
        
        printString("return from bluetoothDiscovery()");
        
    }
    
    public void commandAction(Command c, Displayable d) {
        
        if(c == exit) {
            notifyDestroyed();
            destroyApp(true);
        }
        
        if(c == discover) {
            doBluetoothDiscovery();
        }
    }
    
    private void bluetoothCopyDeviceTableToList() {
        
        // Remove old entries
        for (int i=deviceList.size(); i > 0; i--) {
            deviceList.delete(i-1);
        }
        
        // Append keys from table
        for (Enumeration e = bluetoothDevices.keys(); e.hasMoreElements(); ) {
        
            try {
                
                String name = (String) e.nextElement();
                deviceList.append( name, null );
                
            } catch (Exception exception) {
            }
        }
        
        display.setCurrent(deviceList);
    }
    
    // ************************************************
    // Implement BT DiscoveryListener
    // ************************************************
    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
        
        try {
            
            printString("deviceDisc: " + btDevice.getFriendlyName(false));
            
            // Add Device to table
            bluetoothDevices.put(btDevice.getFriendlyName(false), btDevice);
            
        } catch (Exception e) {
            printString("Exception (b4): " + e);
        }
        
    }
    
    public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
    }
    
    public void serviceSearchCompleted(int transID, int respCode) {
    }
    
    public void inquiryCompleted(int discType) {
        
        printString("inquiryCompleted! " + discType);
        bluetoothCopyDeviceTableToList();
        
    }
    
    // ************************************************
    // ************************************************
}