Encontrando os dispositivos móveis disponíveis – J2ME

Veja nesta dica como listar os dispositivos que estão remotamente disponíveis para o dispositivo local.

Encontrando os dispositivos móveis disponíveis – J2ME

 

O exemplo abaixo lista os dispositivos que estão remotamente disponíveis para o dispositivo local. Ele lista também os dispositivos que tem o bluetooth desligado.

 

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("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 < 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("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 > 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);
}

// ************************************************
// Implementa BT DiscoveryListener
// ************************************************
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();

}

// ************************************************
// ************************************************
}

Ebook exclusivo
Dê um upgrade no início da sua jornada. Crie sua conta grátis e baixe o e-book

Artigos relacionados