Documentation
JavaDoc
Browse the JavaDoc: Click here
Example
Here is an example usage of the API. The example is a small Bluetooth marketing application. The app inquire for devices, and test how close a device is. If the device is close, a file is pushed to the remote device.
package test;
import java.io.OutputStream;
import javax.bluetooth.*;
import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.Operation;
import javax.obex.ResponseCodes;
import net.sourceforge.blueonjop.BTPositioning;
import ejip.CS8900;
import ejip.LinkLayer;
import ejip.Net;
import ejip.Packet;
import ejip.Udp;
import ejip.UdpHandler;
import joprt.RtThread;
import util.Dbg;
import util.Timer;
public class FilePusher implements DiscoveryListener {
static Net net;
static LinkLayer ipLink;
static int IP = (130 << 24) + (226 << 16) + (33 << 8) + 184;
static int PORT = 2346;
static boolean iknowyou, service, connect;
static FilePusher fp;
static LocalDevice locDev;
static DiscoveryAgent agent;
static HeaderSet hs;
static Connection c;
static ClientSession cs;
static Operation op;
static OutputStream out;
static String[] bdAddr;
static int bdAddrIndex;
static String rssi;
public static void main(String[] args) {
Dbg.init();
RtThread.initClass();
net = Net.init();
ipLink = CS8900.init(Net.eth, Net.ip);
new RtThread(4, 2000) {
public void run() {
for (;;) {
waitForNextPeriod();
net.loop();
}
}
};
new RtThread(4, 2000) {
public void run() {
for (;;) {
waitForNextPeriod();
ipLink.loop();
}
}
};
UdpHandler adder;
adder = new UdpHandler() {
public void request(Packet p) {
//loop over the data content of the packet
int[] data = new int[7];
int cnt = 0;
for (int i = Udp.DATA; i < (p.len/4); i++) {
data[cnt++] = p.buf[i];
}
if(data[0] == 1) {
udpLogger("I know you");
iknowyou = true;
} else if (data[0] == 0){
iknowyou = false;
}
p.setStatus(Packet.FREE);
}
};
Udp.addHandler(2345, adder); //The JOP server port!
System.out.println("Program start");
fp = new FilePusher();
locDev = LocalDevice.getLocalDevice();
agent = locDev.getDiscoveryAgent();
hs = HeaderSet.init();
bdAddr = new String[10];
bdAddrIndex = 0;
RtThread.startMission();
//run forever
for (;;) {
Timer.wd();
int t = Timer.getTimeoutMs(500);
while (!Timer.timeout(t)) {
if(iknowyou) {
agent.startInquiry(DiscoveryAgent.GIAC, fp);
while(!connect) {
//wait
}
if(bdAddrIndex > 0) {
for(int x=0; x<bdAddrIndex; x++) {
udpLogger(bdAddr[x]);
}
c = Connector.open(bdAddr[0]+",1105");
rssi = BTPositioning.RSSI();
udpLogger(rssi);
int r = stringToInt(rssi);
if(c != null && r > 9) {
cs = (ClientSession) c;
hs = cs.connect(hs);
if(hs != null) {
if(hs.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) {
udpLogger("Connect error "+hs.getResponseCode());
} else {
udpLogger("Connect ok");
hs.setHeader(HeaderSet.NAME, "a");
op = cs.put(hs);
if(op != null) {
if(op.getResponseCode() != ResponseCodes.OBEX_HTTP_CONTINUE) {
udpLogger("Put error");
} else {
udpLogger("Put continue");
out = op.openOutputStream();
byte[] b = new byte[2];
b[0] = 'b';
b[1] = 'c';
out.write(b);
}
}
hs = cs.disconnect(hs);
if(hs != null) {
if(hs.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) {
udpLogger("Disconnect error "+hs.getResponseCode());
} else
udpLogger("Disconnect ok");
}
}
}
c.close();
} else {
udpLogger("RSSI to low/No connection?");
if(c!=null)
c.close();
}
}
bdAddrIndex = 0;
//iknowyou = false;
}
}
}
}
public void deviceDiscovered(RemoteDevice btDevice, String cod) {
bdAddr[bdAddrIndex] = btDevice.getBluetoothAddress();
bdAddrIndex++;
}
public void inquiryCompleted(int discType) {
connect = true;
}
public void servicesDiscovered(int transID, String[] servRecord) {
//no implementation
}
public void serviceSearchCompleted(int transID, int respCode) {
//no implementation
}
static int val, tmp, ten;
static int NEGATIVE = (int)'-';
static boolean neg;
public static int stringToInt(String s) {
val = 0;
tmp = 0;
neg = false;
ten = 0;
for(int i=s.length()-1; i>=0;i--) {
if(i==0) {
if(NEGATIVE == (int)s.charAt(0)) {
neg = true;
}
}
if(!neg) {
if(i!=s.length()-1) {
ten = ten * 10;
}
tmp = ((int) s.charAt(i)) - 48;
if(ten!=0)
tmp = tmp*ten;
val = val + tmp;
if(i==s.length()-1)
ten = 1;
}
}
if(neg)
val = val * -1;
return val;
}
public static void udpLogger(String s) {
Packet p = FilePusher.getPacket();
Udp.setData(p, new StringBuffer(s));
Udp.build(p, IP, PORT);
} //END public static void udpLogger(String s)
public static Packet getPacket() {
Packet p = null;
for (; p == null;) {
p = Packet.getPacket(Packet.FREE, Packet.ALLOC);
p.interf = ipLink;
RtThread.sleepMs(10);
}
return p;
} //END public static Packet getPacket(Packet p)
}
