Ejemplo n.º 1
0
  // region BleAdcCallback.Listener
  @Override
  public void onStartSuccess(AdvertiseSettings settingsInEffect) {
    if (this.callback == null) return;

    queue.dispatch(
        new Runnable() {
          @Override
          public void run() {
            // Logger.debug("ble advertise onStartSuccess()");
          }
        });
  }
Ejemplo n.º 2
0
  @Override
  public void stopAdvertise() {
    if (this.callback == null) return;

    queue.cancel(stopCommand);
    stopCommand = null;

    try {
      this.advertiser.stopAdvertising(callback);
    } catch (Exception ex) {
    }

    this.advertiser = null;
    this.callback = null;

    queue.dispatch(
        new Runnable() {
          @Override
          public void run() {
            // Logger.debug("ble advertise stopped");
            listener.onAdvertiseStopped(BleAdvertiser.this, false);
          }
        });
  } // stopAdvertise()
Ejemplo n.º 3
0
  @Override
  public void startAdvertise(long durationMs) {
    if (Build.VERSION.SDK_INT < 21) {
      queue.dispatch(
          new Runnable() {
            @Override
            public void run() {
              listener.onAdvertiseStopped(BleAdvertiser.this, true);
            }
          });
      return;
    }

    if (this.callback != null) return;

    if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
      Logger.error("Bluetooth LE is not supported on this device.");
      queue.dispatch(
          new Runnable() {
            @Override
            public void run() {
              listener.onAdvertiseStopped(BleAdvertiser.this, true);
            }
          });
      return;
    }

    final BluetoothManager bluetoothManager =
        (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    this.adapter = bluetoothManager.getAdapter();

    if (this.adapter == null) {
      Logger.error("Bluetooth is not supported on this device.");
      queue.dispatch(
          new Runnable() {
            @Override
            public void run() {
              listener.onAdvertiseStopped(BleAdvertiser.this, true);
            }
          });
      return;
    }

    this.manager = bluetoothManager;

    if (!adapter.isMultipleAdvertisementSupported()) {
      Logger.warn("ble peripheral mode is not supported on this device.");

      queue.dispatch(
          new Runnable() {
            @Override
            public void run() {
              listener.onAdvertiseStopped(BleAdvertiser.this, true);
            }
          });
      return;
    }

    this.advertiser = adapter.getBluetoothLeAdvertiser();
    if (advertiser == null) {
      Logger.error("ble peripheral failed to get BluetoothLeAdvertiser instance");
      queue.dispatch(
          new Runnable() {
            @Override
            public void run() {
              listener.onAdvertiseStopped(BleAdvertiser.this, true);
            }
          });
      return;
    }

    this.callback = new BleAdvCallback(this, queue);

    queue.dispatch(
        new Runnable() {
          @Override
          public void run() {
            // Logger.debug("ble advertise started");
            listener.onAdvertiseStarted(BleAdvertiser.this);
          }
        });

    stopCommand =
        queue.dispatchAfter(
            durationMs,
            new Runnable() {
              @Override
              public void run() {
                stopAdvertise();
              }
            });

    ManufacturerData data = listener.onAdvertisementDataRequested();
    if (data == null) stopAdvertise();

    advertise(data);
  } // startAdvertise()