java left logo
java middle logo
java right logo
 

Home arrow Java ME Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4084
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to get friendly name of remote bluetooth devices E-mail
User Rating: / 15
PoorBest 

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 = (Stringe.nextElement();
                deviceList.appendname, 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();
        
    }
    
    // ************************************************
    // ************************************************
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.