Aplikasi Bluetooth with MIDP

Written by oktasihotang | dibaca : 2093 kali

Asli….Aq baru saja memodifikasi source code untuk aplikasi bluetooth with MIDP (produk dari sun, red), coz itu merupakan project SDPM aq ma teman – teman aq!!, but it`s from another people too !!

Disisi Server (Host)
1. Create Connection
2. Share Service
3. Update Service

Code :
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.bluetooth.*;
public class HostServer extends Thread
{
// UUID
private static final String UUID_STRING = "F0E0D0C0B0A000908070605040302010";
// Service name
private static final String SERVICE_NAME = "host";
private Host ecm;
private StreamConnectionNotifier server;
private Vector handlers;
private boolean isRunning = false;
public HostServer(Host ecm)
{
createConnection();
}
public void createConnection(){
this.ecm = ecm;
handlers = new Vector();
try {
LocalDevice local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
}
catch (BluetoothStateException e) {
System.out.println(e);
return;
}
try {
server = (StreamConnectionNotifier) Connector.open(
"btspp://localhost:" + UUID_STRING + ";name=" + SERVICE_NAME);
}
catch (IOException e) {
System.out.println(e);
return;
}
}
public void run()
// Wait for client connections, creating a handler for each one
{
isRunning = true;
try {
while (isRunning) {
StreamConnection conn = server.acceptAndOpen();
ThreadedHostHandler hand = new ThreadedHostHandler(conn, ecm);
handlers.addElement(hand);
hand.start();
}
}
catch (IOException e)
{ System.out.println(e); }
}
}

Catatan :
UUID -> Merupakan Identifier dan bersifat unik key…..terdiri dari string dengan (16- or 32-bit) dan long (128-bit) UUIDs.


getAttributeIDs() and getAttributeValue()

retrieve the service record’s attributes.
getConnectionURL()
gets the connection URL for the server hosting the service record.
getHostDevice()
gets the RemoteDevice that hosts the service.


Disisi Client
1. Create Connection
2. Search Service

Code :

di method run


public void run()
{
String servURL = servRecord.getConnectionURL(
ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
if (servURL != null) {
clientForm.setStatus("Found Echo Server URL");
try {
conn = (StreamConnection) Connector.open(servURL);
out = conn.openOutputStream();
in = conn.openInputStream();
clientForm.setStatus("Connected to Echo Server");
clientForm.setEnable(true);
isClosed = false;
}
catch (Exception ex)
{ System.out.println(ex);
clientForm.setStatus("Connection Failed");
}
}
else
clientForm.setStatus("No Service Found");
}

Search Service


try {
LocalDevice local = LocalDevice.getLocalDevice();
agent = local.getDiscoveryAgent();
deviceList = new Vector();
ecm.setStatus("Searching for Devices...");
System.out.println("Searching for Devices...");
agent.startInquiry(DiscoveryAgent.GIAC, this);
}
catch (Exception e) {
System.out.println(e);
ecm.searchError("Search Error");
}
private void searchForService(RemoteDevice dev, int[] attrSet, UUID[] uuids) {
System.out.println("Searching device: " + getDeviceName(dev));
try {
int trans = agent.searchServices(attrSet, uuids, dev, this);
waitForSearchEnd(trans);
}
catch (BluetoothStateException e) {
System.out.println(e);
}
}

Untuk mastikan kalau HP kita ada bluetooth

private void showInquiryCode(int inqCode)
{
if(inqCode == INQUIRY_COMPLETED)
System.out.println("Device Search Completed");
else if(inqCode == INQUIRY_TERMINATED)
System.out.println("Device Search Terminated");
else if(inqCode == INQUIRY_ERROR)
System.out.println("Device Search Error");
else
System.out.println("Unknown Device Search Status: " + inqCode);
}

Intinya seperti itu…
Semoga membantu :)

1 Comment (Upil) Betebaran Disini...

  1. This web site is known as a walk-by means of for the entire info you wished about this and didn’t know who to ask. Glimpse right here, and also you’ll definitely discover it.

Tinggalkan Upilmu Disini...