如何在J2ME程序里获取周围支持蓝牙通讯的设备列表

本文介绍了一种使用Java ME实现的蓝牙设备发现方法,并展示了如何列出附近的蓝牙设备。通过本地设备API启动查询,捕获并显示缓存及已知设备,最后实现了设备列表的更新。

下边的例子说明了如何获取附近可以和本移动设备进行兰牙通讯的设备列表,

同时也列出了所有将兰牙功能关闭的设备

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);
       
        // 设备列表
        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();
       
    }
   
    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() {
       
        for (int i=deviceList.size(); i > 0; i--) {
            deviceList.delete(i-1);
        }
       
        for (Enumeration e = bluetoothDevices.keys(); e.hasMoreElements(); ) {
       
            try {
               
                String name = (String) e.nextElement();
                deviceList.append( name, null );
               
            } catch (Exception exception) {
            }
        }
       
        display.setCurrent(deviceList);
    }
   
    // ************************************************
    // 实现 兰牙 DiscoveryListener
    // ************************************************
    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
       
        try {
           
            printString("deviceDisc: " + btDevice.getFriendlyName(false));
           
            // 将设备添加到列表
            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("查询完成! " + discType);
        bluetoothCopyDeviceTableToList();
       
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值