Leitor de dispositivos Bluetooth

1 resposta
D

Olá pessoal,

Implementei esse código para ler dispositivos bluetooth e ele copilou sem nenhum erro, porém ao coloca-lo no celular ele não reconheceu nenhum dispositivo. Será que alguém poderia me dizer o que está errado?

Obrigado!

Código:

import javax.microedition.midlet.<em>;

import javax.microedition.lcdui.</em>;

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("Encontrar",Command.SCREEN, 0);
    exit = new Command("Sair",Command.EXIT, 0);
    
    // Lista de dispositivosDevice List
    deviceList = new List("Selecione o dispositivo", 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();
    
}

// Implementa 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 &lt; devices.length; i++) {
                bluetoothDevices.put(devices[i].getFriendlyName(false), 
                        devices[i]);
                
                printString("Device (cached): " + devices[i].getFriendlyName(false) +
                        " " + devices[i].getBluetoothAddress() );
            }
        }
        
        devices = discoveryAgent.retrieveDevices(DiscoveryAgent.PREKNOWN);
        
        if (devices != null) {
        
            for (int i=0; i &lt; 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("retorna de 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 &gt; 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);
}


public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
    
    try {
        
        printString("deviceDisc: " + btDevice.getFriendlyName(false));
        
        // Adiciona o dispositivo
        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();
    
}

// ************************************************
// ************************************************

}

1 Resposta

M

teu celular tem a jsr-82 ?? Ve se o bluetooth tá ligado, alguns celulares não ligam automaticamente.

Criado 3 de abril de 2007
Ultima resposta 3 de abr. de 2007
Respostas 1
Participantes 2