Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d03d128d authored by Marie Janssen's avatar Marie Janssen Committed by Gerrit Code Review
Browse files

Merge "service/example: Add advertise flag for hr server"

parents e9dc9bab c9b00d70
Loading
Loading
Loading
Loading
+62 −1
Original line number Diff line number Diff line
@@ -27,14 +27,65 @@

namespace heart_rate {

class CLIBluetoothLowEnergyCallback
    : public ipc::binder::BnBluetoothLowEnergyCallback {
 public:
  CLIBluetoothLowEnergyCallback(android::sp<ipc::binder::IBluetooth> bt)
      : bt_(bt) {}

  // IBluetoothLowEnergyCallback overrides:
  void OnConnectionState(int status, int client_id, const char* address,
                         bool connected) override {}
  void OnScanResult(const bluetooth::ScanResult& scan_result) override {}

  void OnClientRegistered(int status, int client_id){
    if (status != bluetooth::BLE_STATUS_SUCCESS) {
      LOG(ERROR) << "Failed to register BLE client, will not start advertising";
      return;
    }

    LOG(INFO) << "Registered BLE client with ID: " << client_id;

    std::vector<uint8_t> data;
    base::TimeDelta timeout;

    bluetooth::AdvertiseSettings settings(
        bluetooth::AdvertiseSettings::MODE_LOW_POWER,
        timeout,
        bluetooth::AdvertiseSettings::TX_POWER_LEVEL_MEDIUM,
        true);

    bluetooth::AdvertiseData adv_data(data);
    adv_data.set_include_device_name(true);
    adv_data.set_include_tx_power_level(true);

    bluetooth::AdvertiseData scan_rsp;

    bt_->GetLowEnergyInterface()->
        StartMultiAdvertising(client_id, adv_data, scan_rsp, settings);
  }

  void OnMultiAdvertiseCallback(int status, bool is_start,
      const bluetooth::AdvertiseSettings& /* settings */) {
    LOG(INFO) << "Advertising" << (is_start?" started":" stopped");
  };

 private:
  android::sp<ipc::binder::IBluetooth> bt_;
  DISALLOW_COPY_AND_ASSIGN(CLIBluetoothLowEnergyCallback);
};


HeartRateServer::HeartRateServer(
    android::sp<ipc::binder::IBluetooth> bluetooth,
    scoped_refptr<base::SingleThreadTaskRunner> main_task_runner)
    scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
    bool advertise)
    : simulation_started_(false),
      bluetooth_(bluetooth),
      server_if_(-1),
      hr_notification_count_(0),
      energy_expended_(0),
      advertise_(advertise),
      main_task_runner_(main_task_runner),
      weak_ptr_factory_(this) {
  CHECK(bluetooth_.get());
@@ -270,6 +321,16 @@ void HeartRateServer::OnServiceAdded(

  LOG(INFO) << "Heart Rate service added";
  pending_run_cb_(true);

  if (advertise_) {
    auto ble = bluetooth_->GetLowEnergyInterface();
    if (!ble.get()) {
      LOG(ERROR) << "Failed to obtain handle to IBluetoothLowEnergy interface";
      return;
    }
    ble->RegisterClient(new CLIBluetoothLowEnergyCallback(bluetooth_));
  }

}

void HeartRateServer::OnCharacteristicReadRequest(
+5 −1
Original line number Diff line number Diff line
@@ -35,7 +35,8 @@ class HeartRateServer : public ipc::binder::BnBluetoothGattServerCallback {
 public:
  HeartRateServer(
      android::sp<ipc::binder::IBluetooth> bluetooth,
      scoped_refptr<base::SingleThreadTaskRunner> main_task_runner);
      scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
      bool advertise);
  ~HeartRateServer() override;

  // Set up the server and register the GATT services with the stack. This
@@ -122,6 +123,9 @@ class HeartRateServer : public ipc::binder::BnBluetoothGattServerCallback {
  // mapping, so we do it ourselves here.
  std::unordered_map<std::string, uint8_t> device_ccc_map_;

  // Wether we should also start advertising
  bool advertise_;

  // libchrome task runner that we use to post heart rate measurement
  // notifications on the main thread.
  scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
+3 −1
Original line number Diff line number Diff line
@@ -129,9 +129,11 @@ int main(int argc, char* argv[]) {
    main_loop.QuitWhenIdle();
  };

  bool advertise = base::CommandLine::ForCurrentProcess()->HasSwitch("advertise");

  // Create the Heart Rate server.
  std::unique_ptr<heart_rate::HeartRateServer> hr(
      new heart_rate::HeartRateServer(bluetooth, main_loop.task_runner()));
      new heart_rate::HeartRateServer(bluetooth, main_loop.task_runner(), advertise));
  if (!hr->Run(callback)) {
    LOG(ERROR) << "Failed to start Heart Rate server";
    return EXIT_FAILURE;