Mar 31, 2009

Bluetooth Chat

In this post, i would like to explain, how to create an application to be used in chat communication by using bluetooth device.at the time of this writing, I use symbian 3rd FP1 ME SDK to build the application,any phones which already support MIDP 2.0, would be worked.
The application uses blt2cap protocol, i do not know, why we have to use blt2cap protocol in order to work properly,I was using other protocol called btgoep and unfortunately it does not work.


At the first time,to declaring the connection with btl2cap we use L2CAPConnectionNotifier class, the code would be displayed as shown below :


L2CAPConnectionNotifier notifier = (L2CAPConnectionNotifier) Connector.open(url);


Next we need to create a thread so the server will run with own resources.


public void run(){
try {
connection = notifier.acceptAndOpen();
startReciever();
} catch(Exception ex) {
System.out.println(ex);
}
}



In the client side, to connecting with the server we create the following code :


L2CAPConnection connection = (L2CAPConnection)Connector.open(serviceURL);



To receiving any message took from the server, we create the code as follow :


// start receiver
Thread t = new Thread(this);
t.start();

public void run() {
try {
startReciever();
} catch(Exception ex) {
System.out.println(ex);
}
}

public void startReciever() {
while(listening) {
try {
if(connection.ready()) {
int receiveMTU = connection.getReceiveMTU();
byte[] data = new byte[receiveMTU];
int length = connection.receive(data);

String message = new String(data,0,length);
aMainView.SetMessage(message);
}
} catch(IOException ioe4) {
try {
Disconnecting();
} catch (Exception ex) {
aMainView.SetMessage("Disconnecting fail");
}
aMainView.SetMessage("Service disconnected");
}
}
}



since the function is running on the thread, therefore there is no issue would be occured with the view



for better understanding and ensure the given source code to be worked,please download the application by clicking here and the source code in here

english version by Ahmad Ripai

Pada post kali ini saya ingin menerangakan masalah pembuatah aplikasi chat yang menggunakan media bluetooth. Seperti biasa saya menggunakan symbian 3rd FP1 ME sebagai SDK-nya, dan walaupun menggunakan Symbain SDK saya rasa aplikasi bluetooth chat ini akan kompatible dengan handphone non symbian tetapi jelas harus mempunyai kemampuan JSR-082. Gak usah banyak bicara dan teori lagi, mari langsung kita kerjakan.

Nahh.... untuk Bluetooth chat saya menggunakan protocol btl2cap, nggak ngerti kenapa harus menggunakan protocol ini, karena kemarin pernah menggunakan btgoep tapi gak bisa jalan, pokoknya jalannya aneh lah.... Nah.. untuk mendeklarasikan sebuah server kita harus menggunakan class L2CAPConnectionNotifier, dan contoh dari penggunakan L2CAPConnectionNotifier seperti ini

L2CAPConnectionNotifier notifier = (L2CAPConnectionNotifier) Connector.open(url);

Tapi deklarasi itu saja belum cukup, karena server harus bisa merespon setiap permintaan dari client. Nahh... agar proses listening server tidak mengganggu bagian view-nya kita deklarasikan dalam sebuah thread

public void run(){
try {
connection = notifier.acceptAndOpen();
startReciever();
} catch(Exception ex) {
System.out.println(ex);
}
}


Terus pada sisi client-nya kita juga harus menyesuaikan server, kita menggunakan baris berikut untuk membuat koneksi dengan server

L2CAPConnection connection = (L2CAPConnection)Connector.open(serviceURL);

Setelah client bisa terkoneksi ke server, client ataupun server butuh sebuah sumber daya untuk menerima setiap pertukaran data, yang untuk selanjutnya bisa ditampilkan pada view. Nah... pada kesempatan ini saya membuat sebuah fungsi dengan nama startReciever, yang tentunya berjalan dalam thread agar tidak mengganggu kerja dari view. Nah... codenya seperti berikut

// start receiver
Thread t = new Thread(this);
t.start();

public void run() {
try {
startReciever();
} catch(Exception ex) {
System.out.println(ex);
}
}

public void startReciever() {
while(listening) {
try {
if(connection.ready()) {
int receiveMTU = connection.getReceiveMTU();
byte[] data = new byte[receiveMTU];
int length = connection.receive(data);

String message = new String(data,0,length);
aMainView.SetMessage(message);
}
} catch(IOException ioe4) {
try {
Disconnecting();
} catch (Exception ex) {
aMainView.SetMessage("Disconnecting fail");
}
aMainView.SetMessage("Service disconnected");
}
}
}


Dah... dari pada bingung langsung saja download full code-nya disini, atau download aplikasi jadinya disini.
Semoga bermanfaat.
Anonymous said...

I don't know what are u talking about..it looks like the english version using indonesian style...:)

anyway thank you for your effort to translate into english..

NR Technology said...

Anonime -> ok Thanks. I will learn how to talk english.

gde ari prima said...

trima kasih infonya pak Hendrawan, kebetulan saya juga sedang mendesain aplikasi messenger dengan bluetooth.

kalau boleh tanya pak, apa dengan protocol rfcomm (btspp) aplikasi bisa dijalankan juga?
kalau boleh sya minta referensinya pak, soalnya sulit sekali mencari buku yg khusus membahas ttg pemrograman java bluetooth.

terima kasih

NR Technology said...

gde ari prima -> ok sama-sama. Klo messenger dengan bluetooth aku dah ada http://blogehendrawan.blogspot.com/2009/03/bluetooth-chat.html.
btspp itu kan buat kirim file dsb lah.. yang intinya per session trus putus. klo rfcomm kan itu istilahnya class aslinya, jadi bisa dimodifikasi, atau digunakan sesuai kebutuhan.
Klo referensi google.com atau cari di flazx.com. Klo bahasa Indonesia aku gak ngerti, penulisnya pada gak pengin nulis mungkin heheheh

Anonymous said...

pak hendra knp download aplikasi na gk bisa ya?
trus saya msk ke http://blogehendrawan.blogspot.com/2009/03/bluetooth-chat.html. knp gk bisa jg ya?

NR Technology said...

Anonim -> Server fCard dah kolep. Gak saya bayar. Trus backupnya ada di laptop, sekarang laptopnya rusak.

Anonymous said...

mas hendrawan saya boleh minta aplikasi bluetooth chatnya ( full script ) , tolong di upload lagi ya

Unknown said...

bisa tolong bantu saya mas hendrawan, saya punya ide untuk membuat aplikasi chat menggunakan bluetooth untuk proyek akhir saya, setelah saya googling eehh ternyata mas hendrawan punya aplikasi + full scriptnya bisa tolong dikirim ke email saya mas. email saya: artonodikky@gmail.com..
PLEASE ya MAS saya punya ide tapi dak tw sama sekali tentang JAVA

PLEASE MAS BANTU YA

Powered by Blogger.

Whatsapp Button works on Mobile Device only

Start typing and press Enter to search