Merge "service/example: Add advertise flag for hr server"
diff --git a/service/example/heart_rate/heart_rate_server.cpp b/service/example/heart_rate/heart_rate_server.cpp
index 5fb9853..a1efb64 100644
--- a/service/example/heart_rate/heart_rate_server.cpp
+++ b/service/example/heart_rate/heart_rate_server.cpp
@@ -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 @@
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(
diff --git a/service/example/heart_rate/heart_rate_server.h b/service/example/heart_rate/heart_rate_server.h
index 1b5ed84..4c87484 100644
--- a/service/example/heart_rate/heart_rate_server.h
+++ b/service/example/heart_rate/heart_rate_server.h
@@ -35,7 +35,8 @@
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 @@
// 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_;
diff --git a/service/example/heart_rate/server_main.cpp b/service/example/heart_rate/server_main.cpp
index 4890398..80f82bc 100644
--- a/service/example/heart_rate/server_main.cpp
+++ b/service/example/heart_rate/server_main.cpp
@@ -129,9 +129,11 @@
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;