コード例 #1
0
  /**
   * @return true if we want this neighbor to be added to the neighbors list fragment, which is the
   *     case when this neighbor has no duplicate. Besides that, we upload to DB if location is not
   *     null, but we still show in fragment if location is null.
   */
  public boolean addNeighborInDb(WiFiP2pService d) {
    final Location gpsLastLocation = mLocationListeners[0].getLastLocation();
    final Location networkLastLocation = mLocationListeners[1].getLastLocation();
    final Location passiveLastLocation = mLocationListeners[2].getLastLocation();

    String locProvider = "none";
    float acc = 0;
    double lng = 0;
    double lat = 0;

    Location location =
        bestMostRecentLocation(gpsLastLocation, networkLastLocation, passiveLastLocation);

    if (location == null) {
      Log.i(TAG, "inserting neighbor - no - location is null");
      Toast.makeText(this, "We're having issues getting your location", Toast.LENGTH_LONG).show();
    } else {
      lat = location.getLatitude();
      lng = location.getLongitude();
      acc = location.getAccuracy();
      locProvider = location.getProvider();
    }

    String model = FileIndexingService.getDeviceName();
    String deviceId = CUtils.getDeviceId(this);
    if (!cNeighborsDataSource.isThereAnotherCNeighborWithSameNameAndSameTimeDetectedInDb(d)) {
      quickLog("Cool! Doesn't have a duplicate!");

      final CNeighbor cNeighbor =
          new CNeighbor(
              d.getDevice().status,
              d.getTimeDetectedMs(),
              d.getDevice().deviceName,
              d.getDevice().deviceAddress,
              d.getDevice().primaryDeviceType,
              d.getDevice().secondaryDeviceType,
              lat,
              lng,
              acc,
              locProvider,
              deviceId,
              model,
              d.getServiceRegistrationType(),
              d.getInstanceName());
      cNeighborsDataSource.insertCNeighbor(cNeighbor);

      Log.w("KarimX", "Neighbors (new), inserted neighbors:" + cNeighbor.toJSONString());

      return true;
    } else {
      quickLog("Duplicate detected!");
      return false;
    }
  }
コード例 #2
0
  @Override
  public void onCreate() {
    super.onCreate();

    cNeighborsDataSource = new CNeighborsDataSource(this);
    cNeighborsDataSource.open();

    startRequestingLocationUpdates();

    /** NEW * */
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

    manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    channel =
        manager.initialize(
            this,
            getMainLooper(),
            new WifiP2pManager.ChannelListener() {
              @Override
              public void onChannelDisconnected() {
                quickLog("Channel disconnected");
              }
            });

    startOrRestartRegistrationAndDiscovery();

    wifiDirectBroadcastReceiver = new WifiDirectBroadcastReceiverMode2(manager, channel);
    registerReceiver(wifiDirectBroadcastReceiver, intentFilter);
    /** NEW * */
  }