Moderators: roger, 99jonathan, imaqine
import java.util.Vector;
import javax.bluetooth.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.io.*;
import java.io.IOException;
import java.io.*;
public class BlueDisc extends MIDlet implements CommandListener,
DiscoveryListener {
private LocalDevice local = null;
private DiscoveryAgent agent = null;
private Vector devicesFound = null;
private ServiceRecord[] servicesFound = null;
private Alert a;
StreamConnection con;
public void startApp() {
/* Add your MIDlet specific code here.
* You probably want to show the user
* a welcome screen.
* The call to doDeviceDiscovery() is
* here for the example's sake. You
* should call doDeviceDiscovery() when
* the user actively asks for it.
*/
//infoAlert("Discovering Services");
doDeviceDiscovery();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
}
private void infoAlert(String msg) {
a = new Alert("Info",msg, null, AlertType.INFO);
a.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(a);
}
public void deviceDiscovered(RemoteDevice remoteDevice,
DeviceClass deviceClass) {
infoAlert("Device discovered: " + deviceClass.getMajorDeviceClass());
devicesFound.addElement(remoteDevice);
}
public void inquiryCompleted(int param) {
/*We should give the user an alert based on the
*inquiry status code
*/
// infoAlert("Inquiry Completed");
switch (param) {
case DiscoveryListener.INQUIRY_COMPLETED:
//Inquiry completed normally, add appropriate code here
// infoAlert("Inquiry Completed Normally");
for(int i=0;i<devicesFound.size();i++) {
RemoteDevice rd = (RemoteDevice) devicesFound.elementAt(i);
//try {
//infoAlert("Searching for " + rd.getFriendlyName(false));
//} catch (IOException ioe) {}
doServiceSearch(rd);
}
break;
case DiscoveryListener.INQUIRY_ERROR:
//Error during inquiry, add appropriate code here
infoAlert("Inquiry Completed with error");
break;
case DiscoveryListener.INQUIRY_TERMINATED:
/*
* Inquiry terminated, caused by agent.cancelInquiry()
* Add appropriate code here
*/
infoAlert("Inquiry Terminated");
break;
}
}
public void serviceSearchCompleted(int transID, int respCode) {
switch(respCode) {
case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
/*
* Service search completed successfully
* Add appropriate code here
*/
//infoAlert("Service completed OK");
break;
case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
// device not reachable, add appropriate code here
infoAlert("Device not reachable");
break;
case DiscoveryListener.SERVICE_SEARCH_ERROR:
// Error during service search, add appropriate code here
infoAlert("Service Search Error");
break;
case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
// No records found, add appropriate code here
infoAlert("No Services");
break;
case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
/*
* Search terminated, caused by agent.cancelServiceSearch(..)
* Add appropriate code here
*/
infoAlert("Service Search Terminated");
break;
}
}
public void servicesDiscovered(int transID,
ServiceRecord[] serviceRecord) {
//Services discovered, keep a reference to the ServiceRecord array
//infoAlert("Services Discovered");
servicesFound = serviceRecord;
String url = serviceRecord[0].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
//infoAlert(url);
try{
byte[] b = new byte[4];
byte[] r = new byte[7];
b[0] = 2;
b[1] = 0;
b[2] = 0;
b[3] = 0x0B;
con = (StreamConnection) Connector.open(url);
OutputStream os = con.openOutputStream();
os.write(b);
os.flush();
InputStream is = con.openInputStream();
is.read(r);
infoAlert("Battery: " + ((r[5] & 0xFF) + ((r[6] & 0xFF) << 8)));
is.close();
os.close();
}
catch(IOException e){
infoAlert("" + e.getMessage());
}
}
private void doDeviceDiscovery() {
try {
local = LocalDevice.getLocalDevice();
}catch (BluetoothStateException bse) {
// Error handling code here
infoAlert("No local device");
}
agent = local.getDiscoveryAgent();
// infoAlert((agent == null ? "No agent" : "Got agent"));
devicesFound = new Vector();
try {
if(!agent.startInquiry(DiscoveryAgent.GIAC,this)) {
//Inquiry not started, error handling code here
infoAlert("Inqiry not started");
}
}catch(BluetoothStateException bse) {
//Error handling code here
infoAlert("Inquiry Exception");
}
}
private void doServiceSearch(RemoteDevice device) {
/*
* Service search will always give the default attributes:
* ServiceRecordHandle (0x0000), ServiceClassIDList (0x0001),
* ServiceRecordState (0x0002), ServiceID (0x0003) and
* ProtocolDescriptorList (0x004).
*
* We want additional attributes, ServiceName (0x100),
* ServiceDescription (0x101) and ProviderName (0x102).
*
* These hex-values must be supplied through an int array
*/
int[] attributes = {0x100,0x101,0x102};
/*
* Supplying UUIDs in an UUID array enables searching for specific
* services. PublicBrowseRoot (0x1002) is used in this example. This
* will return any services that are public browsable. When searching
* for a specific service, the service's UUID should be supplied here.
*/
UUID[] uuids = new UUID[1];
//uuids[0] = new UUID(0x1002);
uuids[0] = new UUID("1101",true); // Serial Port
try {
agent.searchServices(attributes,uuids,device,this);
} catch (BluetoothStateException e) {
infoAlert("Service Search Exception");
infoAlert( "" + e);
// Error handling code here
}
}
}
Users browsing this forum: No registered users and 1 guest