BTW any way.... gak usah panjang lebar langsung saja saya kasih code untuk melakukan koneksi bluetooth dengan J2ME. Walaupun saya rasa code ini akan compatible ke semua device yang telah mempunyai feature JSR-082, tetapi hal yang pasti code ini jalan pada Symbian S60 3rd dan pada S40 3rd FP1. Untuk Symbian S60 memang sudah saya coba dengan real device bukan emulator, tetapi kalau untuk S40 baru sekedar pada emulato.
file BtHandler.java
import javax.obex.HeaderSet;
import javax.obex.ResponseCodes;
import javax.obex.ServerRequestHandler;
/**
*
* @author Hendrawan
*/
public class BtHandler extends ServerRequestHandler {
private HelloMIDlet BtMidlet;
public BtHandler(HelloMIDlet midlet) {
this.BtMidlet = midlet;
}
public int onConnect(HeaderSet request, HeaderSet reply) {
this.BtMidlet.SetMessage("Connection created");
return ResponseCodes.OBEX_HTTP_OK;
}
public int onPut(Operation op) {
return ResponseCodes.OBEX_HTTP_OK;
}
}
file BluetoothServer.java
import java.io.IOException;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.obex.SessionNotifier;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Hendrawan
*/
public class BluetoothServer implements Runnable {
private boolean listening = true;
private LocalDevice local_device;
private HelloMIDlet midlet;
private SessionNotifier notifier;
private BtHandler aBtHandler = null;
private final static UUID uuid = new UUID("A55665EE9F9146109085C2055C888B38", false);
private String deviceName;
/** Creates a new instance of BluetoothServer */
public BluetoothServer(HelloMIDlet midlet) {
this.midlet = midlet;
aBtHandler = new BtHandler(this.midlet);
Thread t = new Thread(this);
t.start();
}
public void run(){
midlet.SetMessage("Starting Server");
try {
local_device = LocalDevice.getLocalDevice();
local_device.setDiscoverable(DiscoveryAgent.GIAC);
deviceName = local_device.getFriendlyName();
String url = "btgoep://localhost:" + uuid + ";name=" + deviceName + ";authenticate=false;encrypt=false;master=false";
notifier = ( SessionNotifier ) Connector.open(url);
midlet.SetMessage("Server started with conn " + url);
while (listening) {
notifier.acceptAndOpen(aBtHandler);
midlet.SetMessage("Connection receive");
}
} catch(BluetoothStateException e){System.out.println(e);} catch(IOException f){System.out.println(f);}
}
}
CLIENT
file BluetoothClient.java
import java.io.IOException;
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.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
/**
*
* @author Hendrawan
*/
public class BluetoothClient implements DiscoveryListener {
LocalDevice local = null;
DiscoveryAgent agent = null;
int[] attrSet = null;
RemoteDevice btDev = null;
String serviceURL = null;
ClientSession con = null;
HeaderSet hdr = null;
HelloMIDlet aView = null;
private Vector devicesVector;
public BluetoothClient(HelloMIDlet view) throws BluetoothStateException{
// initialize the stack, if needed
local = LocalDevice.getLocalDevice();
agent = local.getDiscoveryAgent();
agent.startInquiry(DiscoveryAgent.GIAC, this);
devicesVector = new Vector();
aView = view;
aView.showAlert("Starting Client..");
}
public void ConnectingDevice(int aIndex) {
btDev = (RemoteDevice) devicesVector.elementAt(aIndex);
int ai[] = new int[1];
ai[0] = 256;
UUID[] uuids = new UUID[1];
uuids[0] = new UUID("A55665EE9F9146109085C2055C888B38", false);
try {
agent.searchServices(ai, uuids, btDev, this);
} catch (Exception ex) {
aView.showAlert(ex.getMessage().toString());
}
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
try {
devicesVector.addElement(btDevice);
aView.setMessage(btDevice.getFriendlyName(false));
} catch (IOException ex) {
ex.printStackTrace();
}
throw new UnsupportedOperationException("Not supported yet.");
}
public void inquiryCompleted(int discType) {
aView.showAlert("Searching device complete");
throw new UnsupportedOperationException("Not supported yet.");
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
//aView.showAlert(Integer.toString(servRecord.length));
for(int i =0; i < servRecord.length; i++){
serviceURL = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
}
throw new UnsupportedOperationException("Not supported yet.");
}
public void serviceSearchCompleted(int transID, int respCode) {
aView.showAlert("Service discover finish, url:" + serviceURL);
try {
con = (ClientSession)Connector.open(serviceURL);
} catch (Exception ex) {
}
throw new UnsupportedOperationException("Not supported yet.");
}
}
Code diatas hanya engine-nya saja tanpa Midlet-nya, jadi kalau ingin meng-aplikasikan code diatas kamu harus bikin Midlet dulu baru kemudia bikin Command untuk menjalankan setiap Engine baik server maupun client. Untuk bagian server kamu hanya harus memanggil fungsi BluetoothServer dengan argument Midlet-nya, dan untuk bagian client kamu harus memanggil fungsi BluetoothClient dengan argumen Midlet-nya juga.
Oh... yaa untuk tambahan saja, kalau kamu menggunakan Symbian S60 3rd FP2 ME SDK, kamu bisa merubah bagian agent.startInquiry(DiscoveryAgent.GIAC, this); menjadi agent.startInquiry(DiscoveryAgent.LIAC, this);, tetapi terus terang saya sampai hari ini belum tahu fungsinya untuk apa.
Terima kasih, semoga bermanfaat.
mas hendrawan blh saya konsul ga?
klo blh tlg email saya ya.
friscia_anthony@hotmail.com
Anonime->silahkan saya. Kirim email ajah ke wawan_redhat@yahoo.com
Mas Hendrawan, boleh tanya yaa ?
kalo koneksi ke servernya di php gimana ya caranya ??
trims ya Mas
Post a Comment