1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
import java.io.*;
import java.util.*;
import javax.bluetooth.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class MonClient extends MIDlet implements DiscoveryListener, CommandListener{
protected Command commandExit;
protected Command commandConnect;
protected Command commandBrowse;
protected Form infoArea = new Form("Mon client");
// For the connection with the other phone
protected UUID uuidPhone = new UUID(0x1101);
protected int inquiryMode = DiscoveryAgent.GIAC;
protected int connectionOptions = ServiceRecord.NOAUTHENTICATE_NOENCRYPT;
protected Vector deviceList = new Vector();
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(infoArea);
commandExit = new Command("Exit", Command.SCREEN,1);
commandConnect = new Command("Send", Command.SCREEN,1);
commandBrowse = new Command("Open",Command.SCREEN,1);
infoArea.addCommand(commandBrowse);
infoArea.addCommand(commandConnect);
infoArea.addCommand(commandExit);
infoArea.setCommandListener(this);
}
private void startDeviceInquiry() {
try {
deviceList.removeAllElements();
log("Searching for devices..");
DiscoveryAgent agent = getAgent();
agent.startInquiry(inquiryMode, this);
} catch (Exception e) {
log("Error : "+e);
}
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
deviceList.addElement(btDevice);
}
public void inquiryCompleted(int discType) {
if(deviceList.size() !=0)
makeDeviceSelectionGUI();
else
log("No device founded");
}
/*-
* ------- Service search section -------
*/
private void startServiceSearch(RemoteDevice device) {
try {
UUID uuids[] = new UUID[] { uuidPhone };
getAgent().searchServices(null, uuids, device, this);
} catch (Exception e) {
log(e);
}
}
/**
* This method is called when a service(s) are discovered.This method starts
* a thread that handles the data exchange with the server.
*/
public void servicesDiscovered(int transId, ServiceRecord[] records) {
for (int i = 0; i < records.length; i++) {
ServiceRecord rec = records[i];
String url = rec.getConnectionURL(connectionOptions, false);
handleConnection(url);
}
}
public void serviceSearchCompleted(int transID, int respCode) {
String msg = null;
switch (respCode) {
case SERVICE_SEARCH_COMPLETED:
msg = "the service search completed normally";
break;
case SERVICE_SEARCH_TERMINATED:
msg = "the service search request was cancelled by a call to DiscoveryAgent.cancelServiceSearch()";
break;
case SERVICE_SEARCH_ERROR:
msg = "an error occurred while processing the request";
break;
case SERVICE_SEARCH_NO_RECORDS:
msg = "no records were found during the service search";
break;
case SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
msg = "the device specified in the search request could not be reached or the local device could not establish a connection to the remote device";
break;
}
log("Service search completed - " + msg);
}
/*-
* ------- The actual connection handling. -------
*/
private void handleConnection(final String url) {
Thread writer = new Thread() {
public void run() {
StreamConnection stream = null;
try {
stream = (StreamConnection) Connector.open(url);
log("Bluetooth stream open.");
InputStream in = stream.openInputStream();
DataInputStream reader = new DataInputStream(in);
OutputStream out = stream.openOutputStream();
DataOutputStream writer = new DataOutputStream(out);
log("Start the exchange.");
while (true) {
// TODO Ecrire ici les echanges avec le serveur bluetooth
}
} catch (IOException e) {
log(e);
}catch(Exception e){
log("Error");
log(e);
}
finally {
log("Bluetooth stream closed.");
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
log(e);
}
}
}
}
};
writer.start();
}
/*-
* ------- Graphic User Interface section -------
*/
private void makeInformationAreaGUI() {
infoArea.deleteAll();
Display.getDisplay(this).setCurrent(infoArea);
}
private void makeDeviceSelectionGUI() {
final List devices = new List("Select a device", List.IMPLICIT);
for (int i = 0; i < deviceList.size(); i++)
devices.append(
getDeviceStr((RemoteDevice) deviceList.elementAt(i)), null);
devices.setCommandListener(new CommandListener() {
public void commandAction(Command arg0, Displayable arg1) {
makeInformationAreaGUI();
startServiceSearch((RemoteDevice) deviceList.elementAt(devices
.getSelectedIndex()));
}
});
Display.getDisplay(this).setCurrent(devices);
}
synchronized public void log(String msg) {
infoArea.append(msg);
infoArea.append("\n\n");
}
public void log(Throwable e) {
log(e.getMessage());
log(e.toString());
}
public void commandAction(Command c, Displayable s)
{
if (c == commandExit){
try{
destroyApp(false);
notifyDestroyed();
}catch(MIDletStateChangeException e){
log(e);
}
}
if(c == commandConnect){
try {
startDeviceInquiry();
} catch (Throwable t) {
log(t);
}
}
if(c == commandBrowse){
}
}
/*
* Utils section
*/
public String hashToString(byte[] hash) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
int v = hash[i] & 0xFF;
if(v < 16) {
sb.append("0");
}
sb.append(Integer.toString(v, 16));
}
return sb.toString();
}
private DiscoveryAgent getAgent() {
try {
return LocalDevice.getLocalDevice().getDiscoveryAgent();
} catch (BluetoothStateException e) {
throw new Error(e.getMessage());
}
}
private String getDeviceStr(RemoteDevice btDevice) {
return getFriendlyName(btDevice) + " - 0x"
+ btDevice.getBluetoothAddress();
}
private String getFriendlyName(RemoteDevice btDevice) {
try {
return btDevice.getFriendlyName(false);
} catch (IOException e) {
return "no name available";
}
}
} |
Partager