// class BluetoothGATTServer ////////////////////////
var _BluetoothGATTServerServices = [];
+var _isBluetoothGATTServerRunning = false;
+
+function _BluetoothGattServerIsRunningChangeListener(result) {
+ _isBluetoothGATTServerRunning = result.state;
+}
/*
* This set is used in BluetoothGATTServer::start() to check which services
return _BluetoothGATTServerServices;
},
set: function() {}
+ },
+ isRunning: {
+ enumerable: true,
+ get: function() {
+ return _isBluetoothGATTServerRunning;
+ },
+ set: function() {}
}
});
+
+ // Register listener for managing GATTServer start / stop
+ native.addListener(
+ 'BluetoothGattServerIsRunningChangeListener',
+ _BluetoothGattServerIsRunningChangeListener
+ );
};
var AbortError = new WebAPIException('AbortError', 'An unknown error occurred');
*/
#include "bluetooth/bluetooth_gatt_server.h"
+#include "bluetooth/bluetooth_instance.h"
#include "bluetooth/bluetooth_util.h"
#include "common/logger.h"
namespace extension {
namespace bluetooth {
-BluetoothGATTServer::BluetoothGATTServer(const BluetoothInstance& instance,
+BluetoothGATTServer::BluetoothGATTServer(BluetoothInstance& instance,
BluetoothGATTServerService& service)
: instance_{instance},
service_{service},
}
LoggerD("bt_gatt_server_start(): success");
- running_ = true;
+ SetRunningState(true);
ReportSuccess(out);
}
ReportError(result, &out);
} else {
ReportSuccess(out);
- running_ = false;
+ SetRunningState(false);
}
}
-void BluetoothGATTServer::GetConnectionMtu(const picojson::value& args,
- picojson::object& out) {
+void BluetoothGATTServer::GetConnectionMtu(const picojson::value& args, picojson::object& out) {
ScopeLogger();
const auto client_address = args.get("clientAddress").get<std::string>();
return true;
}
+void BluetoothGATTServer::SetRunningState(bool state) {
+ ScopeLogger();
+ if (state != running_) {
+ LoggerD("Changing GATTServer running state to: %s", state ? "start" : "stop");
+ running_ = state;
+ picojson::value result{picojson::object{}};
+ auto& obj = result.get<picojson::object>();
+
+ obj.insert(std::make_pair("listenerId", "BluetoothGattServerIsRunningChangeListener"));
+ obj.insert(std::make_pair("state", picojson::value(running_)));
+
+ Instance::PostMessage(&instance_, result.serialize().c_str());
+ }
+}
+
} // namespace bluetooth
} // namespace extension
class BluetoothGATTServer {
public:
- BluetoothGATTServer(const BluetoothInstance& instance, BluetoothGATTServerService& service);
+ BluetoothGATTServer(BluetoothInstance& instance, BluetoothGATTServerService& service);
~BluetoothGATTServer();
void Start(picojson::object& out);
static bool DestroyDescriptor(int total, int index, bt_gatt_h handle, void* user_data);
private:
- const BluetoothInstance& instance_;
+ BluetoothInstance& instance_;
BluetoothGATTServerService& service_;
bool initialized_;
+ // always use SetRunningState(bool) to change value of running_
bool running_;
bt_gatt_server_h handle_;
+ void SetRunningState(bool state);
+
PlatformResult UnregisterAllServicesImpl();
PlatformResult DestroyAllGATTObjects();
PlatformResult Initialize();