Beispiel #1
0
  @Override
  public void onCreate() {
    super.onCreate();
    // Gets an instance of the Hue SDK.
    phHueSDK = PHHueSDK.create();
    // Set the Device Name (name of your app). This will be stored in your bridge whitelist entry.
    phHueSDK.setAppName("QuickStartApp");
    phHueSDK.setDeviceName(Build.MODEL);
    // Register the PHSDKListener to receive callbacks from the bridge.
    phHueSDK.getNotificationManager().registerSDKListener(listener);

    String lastIpAddress = "192.168.0.14";
    String lastUsername = "******";

    // Automatically try to connect to the last connected IP Address.  For multiple bridge support a
    // different implementation is required.
    if (lastIpAddress != null && !lastIpAddress.equals("")) {
      PHAccessPoint lastAccessPoint = new PHAccessPoint();
      lastAccessPoint.setIpAddress(lastIpAddress);
      lastAccessPoint.setUsername(lastUsername);

      if (!phHueSDK.isAccessPointConnected(lastAccessPoint)) {
        phHueSDK.connect(lastAccessPoint);
      }
    } else { // First time use, so perform a bridge search.
      doBridgeSearch();
    }
  }
 @Override
 public void onAuthenticationRequired(PHAccessPoint accessPoint) {
   Log.w(TAG, "Authentication Required.");
   phHueSDK.startPushlinkAuthentication(accessPoint);
   Log.w(TAG, "Access Point IP addr: " + accessPoint.getIpAddress());
   Log.w(TAG, "Access Point MAC addr: " + accessPoint.getMacAddress());
   Log.w(TAG, "Access Point key: " + accessPoint.getUsername());
 }
 @Override
 public void onConnectionLost(PHAccessPoint accessPoint) {
   Log.v(TAG, "onConnectionLost : " + accessPoint.getIpAddress());
   if (!phHueSDK.getDisconnectedAccessPoint().contains(accessPoint)) {
     phHueSDK.getDisconnectedAccessPoint().add(accessPoint);
   }
 }
  public void start(BundleContext bundleContext) throws Exception {
    Activator.context = bundleContext;

    ServiceReference<Context> ref = Activator.context.getServiceReference(Context.class);
    AppContext = Activator.context.getService(ref);

    // Gets an instance of the Hue SDK.
    phHueSDK = PHHueSDK.getInstance();

    // Set the Device Name (name of your app). This will be stored in your
    // bridge whitelist entry.
    phHueSDK.setAppName("QuickStartApp");
    phHueSDK.setDeviceName(android.os.Build.MODEL);

    // Register the PHSDKListener to receive callbacks from the bridge.
    phHueSDK.getNotificationManager().registerSDKListener(listener);

    adapter = new AccessPointListAdapter(AppContext, phHueSDK.getAccessPointsFound());

    // Try to automatically connect to the last known bridge. For first time
    // use this will be empty so a bridge search is automatically started.
    prefs = HueSharedPreferences.getInstance(AppContext);
    String lastIpAddress = prefs.getLastConnectedIPAddress();
    String lastUsername = prefs.getUsername();

    // Automatically try to connect to the last connected IP Address. For
    // multiple bridge support a different implementation is required.
    if (lastIpAddress != null && !lastIpAddress.equals("")) {
      PHAccessPoint lastAccessPoint = new PHAccessPoint();
      lastAccessPoint.setIpAddress(lastIpAddress);
      lastAccessPoint.setUsername(lastUsername);

      if (!phHueSDK.isAccessPointConnected(lastAccessPoint)) {
        phHueSDK.connect(lastAccessPoint);
      }
    } else { // First time use, so perform a bridge search.
      doBridgeSearch();
      Log.w(TAG, "Searching for Bridges.");
    }

    myLight = new Light();
  }
  /**
   * Get a View that displays the data at the specified position in the data set.
   *
   * @param position The row index.
   * @param convertView The row view.
   * @param parent The view group.
   * @return A View corresponding to the data at the specified position.
   */
  public View getView(final int position, View convertView, ViewGroup parent) {

    BridgeListItem item;

    if (convertView == null) {
      convertView = mInflater.inflate(R.layout.selectbridge_item, null);

      item = new BridgeListItem();
      item.bridgeMac = (TextView) convertView.findViewById(R.id.bridge_mac);
      item.bridgeIp = (TextView) convertView.findViewById(R.id.bridge_ip);

      convertView.setTag(item);
    } else {
      item = (BridgeListItem) convertView.getTag();
    }
    PHAccessPoint accessPoint = accessPoints.get(position);
    item.bridgeIp.setTextColor(Color.BLACK);
    item.bridgeIp.setText(accessPoint.getIpAddress());
    item.bridgeMac.setTextColor(Color.DKGRAY);
    item.bridgeMac.setText(accessPoint.getMacAddress());

    return convertView;
  }
        @Override
        public void onAccessPointsFound(List<PHAccessPoint> accessPoint) {
          Log.w(TAG, "Access Points Found. " + accessPoint.size());
          if (accessPoint != null && accessPoint.size() > 0) {
            phHueSDK.getAccessPointsFound().clear();
            phHueSDK.getAccessPointsFound().addAll(accessPoint);
            runOnUiThread(
                new Runnable() {
                  @Override
                  public void run() {
                    adapter.updateData(phHueSDK.getAccessPointsFound());
                  }
                });

            HueSharedPreferences prefs2 = HueSharedPreferences.getInstance(AppContext);
            PHAccessPoint accessPoint2 = (PHAccessPoint) adapter.getItem(0);
            accessPoint2.setUsername(prefs2.getUsername());

            PHBridge connectedBridge = phHueSDK.getSelectedBridge();

            if (connectedBridge != null) {
              String connectedIP =
                  connectedBridge.getResourceCache().getBridgeConfiguration().getIpAddress();
              Log.w(TAG, "Connected IP: " + connectedIP);
              if (connectedIP != null) { // We
                // are
                // already
                // connected
                // here:-
                Log.w(TAG, "Connected IP != null");
                phHueSDK.disableHeartbeat(connectedBridge);
                phHueSDK.disconnect(connectedBridge);
              }
            }
            phHueSDK.connect(accessPoint2);
          }
        }