Пример #1
0
  @Override
  protected void onResuming() {
    App.holder().init();
    App.holder()
        .subscribeBlData(
            new ActionBlDataSub(null, IBlDataSubscriber.class, null)
                .getHandlerImpl(IBlDataSubscriber.class));
    App.holder()
        .access(
            App.holder().new HolderAccess() {
              @Override
              public void onRun() {
                ActScan.this.runOnUiThread(
                    new Runnable() {
                      @Override
                      public void run() {
                        Iterator<BlData> it = getData();
                        while (it.hasNext()) {
                          BlData b = it.next();
                          if (b.isActive()) {
                            rcycAdaptDevices.getBlData().add(b);
                          }
                        }
                        rcycAdaptDevices.notifyItemInserted(0);
                        checkVisibleScans();
                      }
                    });
              }
            });

    registerReceiver(brcstRcvrBlAdapt, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
  }
Пример #2
0
  private void updateHolderData(String macAddress, UUID uuid, byte[] value) {
    App.holder()
        .access(
            App.holder().new HolderAccess() {
              public String macAddress;
              public UUID uuid;
              public byte[] value;

              public HolderBlData.HolderAccess init(String macAddress, UUID uuid, byte[] value) {
                this.macAddress = macAddress;
                this.uuid = uuid;
                this.value = value;
                return this;
              }

              @Override
              public void onRun() {
                Iterator<BlData> i = getData();
                while (i.hasNext()) {
                  BlData d = i.next();
                  if (d.getAddress().equals(macAddress)) {
                    boolean found = false;
                    for (GattData g : d.getGattData()) {
                      if (g.getUuid().equals(uuid)) {
                        g.setData(value);
                        found = true;
                        break;
                      }
                    }
                    if (!found) {
                      d.getGattData().add(new GattData(uuid, value));
                    }
                    ActScan.this.runOnUiThread(
                        new Runnable() {
                          public BlData d;

                          public Runnable init(BlData d) {
                            this.d = d;
                            return this;
                          }

                          @Override
                          public void run() {
                            AdaptScans.ViewHolder viewHolder =
                                (AdaptScans.ViewHolder)
                                    rcycDevices.findViewHolderForAdapterPosition(
                                        rcycAdaptDevices.getBlData().indexOf(d));
                            if (viewHolder != null) {
                              viewHolder.parseData(d);
                            }
                          }
                        }.init(d));
                    break;
                  }
                }
              }
            }.init(macAddress, uuid, value));
  }
Пример #3
0
  @Override
  public void onRefreshedScanReceived(ScanResult result) {
    App.holder()
        .access(
            App.holder().new HolderAccess() {
              private ScanResult result;

              public HolderBlData.HolderAccess init(ScanResult result) {
                this.result = result;
                return this;
              }

              @Override
              public void onRun() {
                boolean found = false;
                Iterator<BlData> i = getData();
                while (i.hasNext()) {
                  BlData b = i.next();
                  if (b.getAddress().equals(result.getDevice().getAddress())) {
                    if (!b.isActive()) {
                      Intent showIntent =
                          new Intent(getString(R.string.intent_advert_gatt_connect));
                      showIntent.putExtra(getString(R.string.bndl_bl_scan_result), result);
                      LocalBroadcastManager.getInstance(ActScan.this).sendBroadcast(showIntent);
                    }
                    b.setActive(true);
                    b.setRssi(result.getRssi());
                    found = true;
                    break;
                  }
                }
                if (!found) {
                  BlData data = new BlData();
                  data.setActive(true);
                  data.setAddress(result.getDevice().getAddress());
                  data.setRssi(result.getRssi());
                  //noinspection ConstantConditions
                  data.setAdvertisement(
                      result.getScanRecord().getServiceData(Constants.UUID_ADVERT_SERVICE_MULTI));
                  Log.d(
                      TAG,
                      "Already found Display not found in Data and will be added: "
                          + data.getAddress());
                  SoundPlayer.playExampleBeep(440, 1, 0);
                  VibratorBuilder.vibrate(VibratorBuilder.SHORT_SHORT);
                  addData(data);
                  Intent showIntent = new Intent(getString(R.string.intent_advert_gatt_connect));
                  showIntent.putExtra(getString(R.string.bndl_bl_scan_result), result);
                  LocalBroadcastManager.getInstance(ActScan.this).sendBroadcast(showIntent);
                }
              }
            }.init(result));
  }
Пример #4
0
  @Override
  public void onScanLost(ScanResult lostResult) {
    App.holder()
        .access(
            App.holder().new HolderAccess() {
              private ScanResult result;

              public HolderBlData.HolderAccess init(ScanResult result) {
                this.result = result;
                return this;
              }

              @Override
              public void onRun() {
                Iterator<BlData> i = getData();
                while (i.hasNext()) {
                  BlData b = i.next();
                  if (b.getAddress().equals(result.getDevice().getAddress())) {
                    b.setActive(false);
                  }
                  ActScan.this.runOnUiThread(
                      new Runnable() {
                        @Override
                        public void run() {
                          for (int i = 0; i < rcycAdaptDevices.getBlData().size(); i++) {
                            BlData b = rcycAdaptDevices.getBlData().get(i);
                            if (!b.isActive()
                                && b.getAddress().equals(result.getDevice().getAddress())) {
                              rcycAdaptDevices.getBlData().remove(b);
                              rcycAdaptDevices.notifyDataSetChanged();
                            }
                          }
                        }
                      });
                }
              }
            }.init(lostResult));
  }
Пример #5
0
  @Override
  public void onScanResultReceived(ScanResult result) {
    Log.d(TAG, "onScanResultReceived: " + result.getDevice().getAddress());
    findViewById(R.id.txt_headline_displays).setVisibility(View.VISIBLE);
    for (BlData b : rcycAdaptDevices.getBlData()) {
      if (b.isActive() && b.getAddress().equals(result.getDevice().getAddress())) {
        return;
      }
    }
    BlData data = new BlData();
    data.setActive(true);
    data.setAddress(result.getDevice().getAddress());
    data.setRssi(result.getRssi());
    //noinspection ConstantConditions
    data.setAdvertisement(
        result.getScanRecord().getServiceData(Constants.UUID_ADVERT_SERVICE_MULTI));
    App.holder()
        .access(
            App.holder().new HolderAccess() {
              private BlData data;
              private ScanResult result;

              public HolderBlData.HolderAccess init(BlData data, ScanResult result) {
                this.data = data;
                this.result = result;
                return this;
              }

              @Override
              public void onRun() {
                Iterator<BlData> i = getData();
                boolean found = false;
                while (i.hasNext()) {
                  BlData b = i.next();
                  if (b.getAddress().equals(data.getAddress())) {
                    if (!b.isActive()) {
                      Intent showIntent =
                          new Intent(getString(R.string.intent_advert_gatt_connect));
                      showIntent.putExtra(getString(R.string.bndl_bl_scan_result), result);
                      LocalBroadcastManager.getInstance(ActScan.this).sendBroadcast(showIntent);
                    }
                    b.setActive(true);
                    found = true;
                    break;
                  }
                }
                if (!found) {
                  Log.d(TAG, "Display not found and will be added: " + data.getAddress());
                  SoundPlayer.playExampleBeep(440, 1, 0);
                  VibratorBuilder.vibrate(VibratorBuilder.SHORT_SHORT);
                  addData(data);
                  Intent showIntent = new Intent(getString(R.string.intent_advert_gatt_connect));
                  showIntent.putExtra(getString(R.string.bndl_bl_scan_result), result);
                  LocalBroadcastManager.getInstance(ActScan.this).sendBroadcast(showIntent);
                }
              }
            }.init(data, result));

    if (gattListenWeather == null) {
      gattProviderWeather =
          service.subscribeGattConnection(
              Constants.WEATHER.GATT_SERVICE_WEATHER.getUuid(),
              gattListenWeather = new GattWeather());
    }
    if (gattListenShout == null) {
      gattProviderShout =
          service.subscribeGattConnection(
              Constants.SHOUT.GATT_SERVICE_SHOUT.getUuid(), gattListenShout = new GattShout());
    }
    if (gattListenNews == null) {
      gattProviderNews =
          service.subscribeGattConnection(
              Constants.NEWS.GATT_SERVICE_NEWS.getUuid(), gattListenNews = new GattNews());
    }
    if (gattListenBooking == null) {
      gattProviderBooking =
          service.subscribeGattConnection(
              Constants.BOOKING.GATT_SERVICE_BOOKING.getUuid(),
              gattListenBooking = new GattBooking());
    }
  }