Вы находитесь на странице: 1из 5

Code listings for "Wirelessly Recover Your Device's Address Book, Part 2," Java

Magazine, November/December 2012


Listing 1
private void startRestore() {
log("Restore Started At: " + new Date());
btServices.doReceive(this);
}
Listing 2
byte[] doReceive(final MIDlet caller) {
// this might take a while, let the user know
working.setLabel("Starting local server... ");
display.setCurrent(workingForm);

// prepare a URL to create a notifier - this is our
// server string
final StringBuffer url =
new StringBuffer(
"btgoep://localhost:6674;name=Backup;authorize=false");
// This starts the listening server in its own thread
new Thread() {
public void run() {
try {
// create the server notifier and the handler
SessionNotifier notifier = null;
RequestHandler handler = new RequestHandler(
workingForm, caller);

notifier = (SessionNotifier) Connector.open(
url.toString ());
notifier.acceptAndOpen(handler);

} catch (Exception ex) {
handleError(ex);
}
}
}.start();

return null;
}
Listing 3
// this class handles the basic commands required to receive
// the file from the client end
class RequestHandler extends ServerRequestHandler {
private Form form;
private byte[] payLoad;
private MIDlet caller;
public RequestHandler(Form workingForm, MIDlet caller) {
this.form = workingForm;
this.caller = caller;
}
public int onPut(Operation op) {
InputStream in = null;
int data;
Long dataLength = null;
try {
HeaderSet header = op.getReceivedHeaders();
// we can't proceed if we don't receive any headers
if(header == null) {
return ResponseCodes.OBEX_HTTP_BAD_REQUEST;
}
// get the input stream
in = op.openDataInputStream();
// check its length
dataLength = (Long) header.getHeader(HeaderSet.LENGTH);
// make the payload as long as the header tells us it is
int ltOfData = (int)dataLength.longValue();
payLoad = new byte[ltOfData];
// and read it
data = in.read(payLoad, 0, ltOfData);
form.append("Received backup data from remote client");
((LaunchMIDlet)caller).receiveBackup(payLoad);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
// nothing more to do
}
}

return ResponseCodes.OBEX_HTTP_OK;
}
public byte[] getPayLoad() { return this.payLoad; }
public int onConnect(HeaderSet request, HeaderSet reply) {
return ResponseCodes.OBEX_HTTP_OK;
}
}
Listing 4a
public void servicesDiscovered(int transID,
ServiceRecord[] servRecord) {
Operation op = null;
OutputStream out = null;
for (int i = ; i ! servRecord"len#t$; i%%) {
Strin# conn&R' = servRecord[i]"#et(onnection&R'(
ServiceRecord")O*&+,-)+I(*+-.)O-)(R/0+, false);
S1stem"err"println(conn&R');
22 found t$e ri#$t service3
if (conn&R'"contains(454)) {
S1stem"err"println(46ound service, connectin# to it"""4);
tr1 {
22 read t$e local bac7up 8le
InputStream input =
ne9 :u;eredInputStream(ne9 6ileInputStream(4data"#<4));
22 read it in to our buc7et
b1te[] buc7et = ne9 b1te[=> ? @>A];
:1te*rra1OutputStream result = null;
result = ne9 :1te*rra1OutputStream(buc7et"len#t$);
int b1tesRead = ;
9$ile (b1tesRead B= C@) {
b1tesRead = input"read(buc7et);
if (b1tesRead D ) {
result"9rite(buc7et, , b1tesRead);
E
E
Listing 4b
(lientSession clientSession =
((lientSession) (onnector"open(conn&R');
,eaderSet r,eaders = clientSession"connect(null);
if (r,eaders"#etResponse(ode() B= Response(odes"O:-F.,++0.OG) {
S1stem"err"println(4Invalid $eaderH 4 %
r,eaders"#etResponse(ode());
return;
E
22 if 9e are $ere, t$en response code 9as o7
b1te[] sm = result"to:1te*rra1();
22 create a ne9 set of $eaders
,eaderSet $eaders = clientSession"create,eaderSet();
$eaders"set,eader(,eaderSet")*I-, 4data"#<4);
$eaders"set,eader(,eaderSet"+/0-, 4binar14);
$eaders"set,eader(,eaderSet"'-)J+,, ne9 'on#(sm"len#t$));
op = clientSession"put($eaders);
out = op"openOutputStream();
out"9rite(sm);
clientSession"disconnect(null);
E catc$ (-Kception eK) {
eK"printStac7+race();
E 8nall1 {
tr1 { out"close(); op"close(); E
catc$ (-Kception e) {
E E E E E
'istin# 5
0IIItem[] contacts =
0II"#etInstance()"fromSerial6ormat(l<c, 4&+6CL4);
22 add to contacts C so open it up in read%9rite mode"
(ontact'ist addressboo7 =
((ontact'ist) (0II"#etInstance()"open0II'ist(
0II"(O)+*(+.'IS+, 0II"R-*D.MRI+-));
22 t$e contacts 9ill contain onl1 t$e 8rst vcard C t$is
22 is a limitation of t$e fromSerial6ormat met$od; I left
22 t$e addition of t$e rest of t$e contacts as an eKercise
22 for t$e reader
(ontact 8rst.contact = ((ontact) contacts[];
22 import it into t$e addressboo7
(ontact cn = addressboo7"import(ontact(8rst.contact);
22 and commit
cn"commit();
ms#*lert"setStrin#(4:ac7up RestoredB4);
(op1ri#$t >@>, Oracle (orporation

Вам также может понравиться