www.pudn.com > Bluegammon蓝牙的应用编程.rar > Device.java


// Copyright (c) 2005 Sony Ericsson Mobile Communications AB 
// 
// This software is provided "AS IS," without a warranty of any kind.  
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,  
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A  
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.  
// 
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se) 
 
package bluegammon; 
 
import javax.microedition.lcdui.Display; 
import javax.microedition.midlet.MIDlet; 
 
/** 
 * The Device represents the  
 * device the MIDlet is running on. 
 *  
 * @author Peter Andersson 
 */ 
public final class Device 
{ 
  /** Canvas keycode for left softbutton */ 
  public static final int KEYCODE_LEFT_SOFT = -6;  
  /** Canvas keycode for right softbutton */ 
  public static final int KEYCODE_RIGHT_SOFT = -7; 
  /** Canvas keycode for back button */ 
  public static final int KEYCODE_BACK = -11; 
  /** The display */ 
  protected static Display m_display; 
  /** The midlet instance */ 
  protected static MIDlet m_midlet; 
   
  /** 
   * Bluetooth support flag, 0 mean unsearched, 1 means searched 
   * and found, -1 means searched and not found. 
   */ 
  private static int m_bluetoothSupport = 0; 
   
  /** 
   * Initializes the Device class. 
   * @param midlet		The midlet instance. 
   * @param display		The display. 
   */ 
  public static void init(MIDlet midlet, Display display) 
  { 
    m_midlet = midlet; 
    m_display = display; 
  } 
   
  /** 
   * Returns the midlet instance. 
   * @return	The midlet. 
   */ 
  public static MIDlet getMidlet() 
  { 
    return m_midlet; 
  } 
   
  /** 
   * Returns the display of this midlet. 
   * @return	The display. 
   */ 
  public static Display getDisplay() 
  { 
    return m_display; 
  } 
   
  /** 
   * Returns whether this device implements bluetooth-apis or not. 
   * @return	True if JSR82 is implemented, false otherwise. 
   */ 
  public static boolean canBluetooth() 
  { 
    if (m_bluetoothSupport == 0) 
    { 
      try 
      { 
        Class.forName("javax.bluetooth.LocalDevice"); 
        m_bluetoothSupport = 1; 
      } 
      catch (Throwable t) 
      { 
        m_bluetoothSupport = -1; 
      } 
    } 
    return m_bluetoothSupport == 1; 
  } 
   
  /** 
   * Returns whether this device can vibrate via J2ME or not. 
   * @return	True if vibration functionality is enabled, false otherwise. 
   */ 
  public static boolean canVibrate() 
  { 
    return true; 
  } 
   
  /** 
   * Vibrates repeatedly according to specified parameters. 
   * @param onInterval    Time in milliseconds to vibrate. 
   * @param offInterval	  Time in milliseconds to pause between each repeat. 
   * @param repeat		  How many times to vibrate-pause. 
   */ 
  public static void vibrate(final int onInterval, final int offInterval, final int repeat) 
  { 
    if (RmsFacade.getBoolean(Bluegammon.VIBRA_OFF)) return; 
     
    new Thread(new Runnable() { 
      public void run() 
      { 
        for (int i = 0; i < repeat; i++) 
        { 
          getDisplay().vibrate(onInterval); 
          try 
          { 
            Thread.sleep(onInterval); 
          } catch (InterruptedException e) {} 
          if (i < repeat - 1) 
          { 
            getDisplay().vibrate(0); 
            try 
            { 
              Thread.sleep(offInterval); 
            } catch (InterruptedException e) {} 
          } 
        } 
        getDisplay().vibrate(0); 
      } 
    },"Bzzzer").start(); 
  } 
   
  /** 
   * Returns a unique id for this device. The K750i supports 
   * the systemproperty "com.sonyericsson.imei" giving the IMEI number. This is used for 
   * calculating a unique id. If this property does not exist, current time is 
   * used for id creation instead. 
   * The id is cached in RMS and is thus only calculated once. 
   *  
   * @return	Device identifyer 
   */ 
  public static int getDeviceId() 
  { 
    int id = RmsFacade.getInt(Bluegammon.DEVICE_ID); 
    if (id == 0) 
    { 
      String idStr = System.getProperty("com.sonyericsson.imei"); 
      if (idStr == null) 
      { 
        idStr = Long.toString(System.currentTimeMillis()); 
      } 
      id = calcIdFromString(idStr); 
      RmsFacade.setInt(Bluegammon.DEVICE_ID, id); 
    } 
    return id; 
  } 
   
  /** 
   * Calculates an indentifer from a string. 
   * @param s	The string 
   * @return	The id 
   */ 
  protected static int calcIdFromString(String s) 
  { 
    int res = 0; 
    // Left shift, XOR the char val, XOR shift overflow bit 
    for (int i = s.length()-1; i >= 0; i--) 
    { 
      res = ((res << 1) ^ s.charAt(i)) ^ ((res & 0x80000000) != 0 ? 1 : 0); 
    } 
    return res; 
  } 
 
  /** Prohibit construction */ 
  private Device() {} 
}