"Mbak, minta bantuan referensi untuk aplikasi bluetooth?soalnya saya...."
Berawal dari pertanyaan itu, dan beberapa pertanyaan yang serupa dengan alasan yang berbeda, akhirnya saya membuat aplikasi sederhana sekali. Yang paling dasar dari sebuah aplikasi yang bisa berkomunikasi melalui bluetooth, yaitu menampilkan daftar bluetooth device yang sedang aktif di area sekitar bluetooth device kita. Saya dapat ini dari berbagai sumber dan forum (khususnya forum nokia). Sintaksnya, seperti dibawah ini:
import java.io.IOException; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; import javax.bluetooth.BluetoothStateException; import javax.bluetooth.DeviceClass; import javax.bluetooth.DiscoveryAgent; import javax.bluetooth.DiscoveryListener; import javax.bluetooth.LocalDevice; import javax.bluetooth.RemoteDevice; import javax.bluetooth.ServiceRecord; import javax.microedition.io.Connector; import javax.microedition.io.StreamConnection; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.List; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; /* * Author @Dyah Fajar Nur Rohmah * dindadyah@gmail.com * dyahslib.blogspot.com * written with many references I got */ public class BluetoothDeviceDiscovery extends MIDlet implements CommandListener, DiscoveryListener, Runnable { private Display display; /** * For displaying found devices. */ private List listDev; private Command cmdExit; private Command cmdDiscover; private Command cmdStopDiscover; private LocalDevice devLocal; private DiscoveryAgent discoverAgent; private Hashtable deviceBT = new Hashtable(); Vector services; public BluetoothDeviceDiscovery() { InitializeComponents(); } protected void InitializeComponents() { display = Display.getDisplay( this ); //initializing device list listDev = new List( "Device list", List.IMPLICIT ); cmdExit = new Command( "Exit", Command.EXIT, 1 ); cmdDiscover = new Command( "Start", Command.SCREEN, 1); cmdStopDiscover = new Command( "Stop", Command.SCREEN, 1); listDev.addCommand( cmdExit ); listDev.addCommand( cmdDiscover ); listDev.setCommandListener( this ); } public void startApp() throws MIDletStateChangeException { display.setCurrent( listDev ); } public void pauseApp() { //No implementation required } public void destroyApp(boolean unconditional) throws MIDletStateChangeException { exitMIDlet(); } public void commandAction( Command command, Displayable displayable ) { if( command == cmdExit ) { exitMIDlet(); } if( command == cmdDiscover ) { doBluetoothDiscovery(); listDev.removeCommand(cmdDiscover); listDev.addCommand(cmdStopDiscover); } if( command == cmdStopDiscover ) { stopDiscover(); listDev.removeCommand( cmdStopDiscover ); listDev.addCommand( cmdDiscover ); } } /** * Method calls stopDiscover method and notifyDestroyed after that. */ protected void exitMIDlet() { stopDiscover(); notifyDestroyed(); } /** * Stops device discovery mode. */ protected void stopDiscover() { //we turning of the device discovery mode discoverAgent.cancelInquiry( this ); } //method-method untuk searching device private void doBluetoothDiscovery() { Thread t = new Thread(this); t.start(); } public void run() { bluetoothDiscovery(); display.setCurrent(listDev); } public void printString(String s) { System.out.println(s); } private void bluetoothDiscovery() { //switchDisplayable(null, getGaugeForm()); //LocalDevice local; try { devLocal = LocalDevice.getLocalDevice(); } catch (BluetoothStateException e) { printString("Message Error: " + e); return; } discoverAgent = devLocal.getDiscoveryAgent(); RemoteDevice device[]; services = new Vector(); //String devFound [] = null; try { device = discoverAgent.retrieveDevices(DiscoveryAgent.CACHED); String status = (device != null ? "ya" : "tdk"); printString(status); if (device != null) { for (int i = 0; i < device.length; i++) { deviceBT.put(device[i].getFriendlyName(false), device[i]); printString("Device (cache): " + device[i].getFriendlyName(false) + " " + device[i].getBluetoothAddress()); listDev.append(device[i].getBluetoothAddress(), null); } } device = discoverAgent.retrieveDevices(DiscoveryAgent.PREKNOWN); if (device != null) { for (int i = 0; i < device.length; i++) { //deviceBT.put(device[i].getFriendlyName(false), device[i]); listDev.insert(i, device[i].getFriendlyName(false) + " - " + device[i].getBluetoothAddress(), null); printString("Device (preknown): " + device[i].getFriendlyName(false) + " " + device[i].getBluetoothAddress()); //devFound[i] = device[i].getBluetoothAddress(); } } } catch (IOException e) { printString("Message (b2): " + e); } try { discoverAgent.startInquiry(DiscoveryAgent.GIAC, this); //bedanya GIAC dan LIAC } catch (BluetoothStateException e) { printString("message (b3): " + e); } printString("return from bluetoothDiscovery()"); // for (int i; i < device.length; i ++){ // return devFound[i]; // } } public void bluetoothCopyDeviceToList() { //remove old entries for (int i = listDev.size(); i > 0; i--) { //devList.delete(i-1); } //append key from table for (Enumeration e = deviceBT.keys(); e.hasMoreElements();) { try { String name = (String) e.nextElement(); listDev.append(name, null); } catch (Exception exception) { } } display.setCurrent(listDev); } //implemestasi dari DiscoveryListener public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) { try { printString("deviceDisc: " + btDevice.getFriendlyName(false)); //add device to table deviceBT.put(btDevice.getFriendlyName(false), btDevice); } catch (Exception e) { printString("Exception (b4): " + e); } } public void servicesDiscovered(int transID, ServiceRecord[] servRecord) { //penjelasan ada di buku Bluetooth Aplication Programing with API 4.3.1 printString("kode 2 : " + String.valueOf(servRecord.length)); if (servRecord.length > 0) { String url = servRecord[servRecord.length].getConnectionURL (ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); printString("kode 3 : " + url); try { StreamConnection con = (StreamConnection) Connector.open(url); printString(url); //DataInputStream in = con.openDataInputStream(); } catch (IOException ex) { ex.printStackTrace(); } } } public void inquiryCompleted(int i) { throw new UnsupportedOperationException("Not supported yet."); } public void serviceSearchCompleted(int i, int i1) { throw new UnsupportedOperationException("Not supported yet."); } } |
Komentar
Posting Komentar
Terima kasih sudah membaca....^^