📄 bluetooth.java
字号:
if(connection == null)
{
return false;
}
// Stop the loading screen.
splashScreen.stop();
return true;
} /* End of init() */
/* *************************************************************************************** */
/* Displays our fatalError(s) and waits for user input */
public void fatalError()
{
splash.stop();
display.setCurrent( splash );
if(bterror == "")
{
bterror = "Unknown error";
}
// Loop error message until the user press fire
while(splash.errorPressAnyKey())
{
// Wait..
}
if(serviceUrl != null)
{
mobileRoboticsMidlet.setDisplayToControllRobot(display);
try{
if(connection != null)
{
connection.close();
connection = null;
}
if(os != null)
{
os.close();
os = null;
}
} catch (Exception e)
{
// Do nothing
}
try {
/* Open bluetooth connection */
connection = (StreamConnection)Connector.open(serviceUrl);
}catch (Exception exception)
{
bterror = "Could not open a connection";
fatalError();
}
/* We want to open an OutputStream so we can actually send data */
try {
os = connection.openOutputStream();
}catch (Exception e)
{
bterror = "Could not open output stream";
fatalError();
}
}else{
mobileRoboticsMidlet.exit();
}
}
/* Used for splashScreen */
public int p1() { return p1; }
public int p2() { return p2; }
public int p3() { return p3; }
public int p4() { return p4; }
/* Load our images and Sprite */
public void loadImages()
{
try { start = Image.createImage ("/start.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { stopped = Image.createImage ("/stopped.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { running = Image.createImage ("/running.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { logo = Image.createImage ("/logo.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { Left = Image.createImage ("/left.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { right = Image.createImage ("/right.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { forward = Image.createImage ("/forward.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { backward = Image.createImage ("/backwards.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { nowhere = Image.createImage ("/nowhere.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { run = Image.createImage ("/run.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { clear = Image.createImage ("/clear.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { Wheel = Image.createImage ("/wheel.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { about = Image.createImage ("/about.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
wheel = new Sprite(Wheel, 118,118);
}
/* Used for splashScreen to retrieve our error message */
public String bterror()
{
return bterror;
}
/* Check for our needed Functions */
private void neededFunctions()
{
try
{
LocalDevice localDevice = LocalDevice.getLocalDevice();
discoveryAgent = localDevice.getDiscoveryAgent();
}catch (BluetoothStateException bse)
{
bterror = "Bluetooth couldn't be initiated:\n " + bse;
fatalError();
}
}
/* *************************************************************************************** */
/* Below this you will find the method used in init() to search for devices and service, */
/* and to connect with them */
/* Start our bluetooth discovery */
private void mobileRoboticsDeviceDiscovery()
{
/* A regular deviceDiscovery class,
but our getDiscoveryAgent has been moved to neededFunctions() */
try {
if(!discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this))
{
bterror = "Couldn't start Inquiry";
fatalError();
}
} catch(BluetoothStateException bse) {
bterror = "BluetoothStateException:\n " + bse;
fatalError();
}
// Device Discovery ran
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
{
// Device was discovered
// Adress: btDevice.getBluetoothAddress()
nrRemoteDevices = nrRemoteDevices + 1;
remoteDevices[nrRemoteDevices-1] = btDevice;
}
public void inquiryCompleted(int discType)
{
/* Do nothing */
discoveryWhile = true;
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
// We only got one service, you can search for more though.
// Services was discovered.
// (String)servRecord[i].getAttributeValue(0x100).getValue()
for(int i=0;i<servRecord.length;i++)
{
serviceUrl = servRecord[i].getConnectionURL(1, false);
}
}
public void serviceSearchCompleted(int transID, int responseCode) {
// Service search completed
// If you are wondering why we don't use any errorchecking it is
// due to the simply fact that they may come from any device
// near us. If a unkown cellphone that is in the pocket of a
// person near by throws a Service_search_error we don't want
// to halt the search.
servicesWhile = true;
}
/* *************************************************************************************** */
public void command(int command)
{
/*
* Bluetooth commands and their function
*
* 0) Power On
* 1) Power Off
* 2) Forward
* 3) Backward
* 4) Left
* 5) Right
* 6) Stop (forward/backward)
*
*/
/* If connection or os is null we don't have a working connection, then abort. */
if(connection != null && os != null)
{
try {
/*
It's easier for the palm to recieve it in byte format,
therefor we simply convert it from int to byte by using
the (byte)INT syntax.
*/
byte[] cmd = new byte[1];
cmd[0] = (byte)command;
// write to buffer
os.write(cmd);
// Send to buffer
os.flush();
// try{ Thread.sleep(25); } catch (Exception e) { /* Do nothing */ }
}catch (Exception e)
{
bterror = "Could not send command";
fatalError();
}
}else{
bterror = "No connection and/or stream";
fatalError();
}
}
public void cleanUp()
{
try {
os.close();
connection.close();
} catch (IOException ioe) {
System.err.println("Error Closing connection " + ioe);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -