| Index: chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.cc
|
| diff --git a/chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.cc b/chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.cc
|
| index 066d9932703ac9c937634f588593c7198c7e3ed6..2218a308cfadb481de441ec37ddbeb6f93ab455d 100644
|
| --- a/chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.cc
|
| +++ b/chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.cc
|
| @@ -25,6 +25,8 @@ namespace {
|
|
|
| const char kErrorAdapterNotInitialized[] =
|
| "Could not initialize Bluetooth adapter";
|
| +const char kErrorAlreadyConnected[] = "Already connected";
|
| +const char kErrorNotConnected[] = "Not connected";
|
| const char kErrorNotFound[] = "Instance not found";
|
| const char kErrorOperationFailed[] = "Operation failed";
|
| const char kErrorPermissionDenied[] = "Permission denied";
|
| @@ -40,6 +42,10 @@ std::string StatusToString(BluetoothLowEnergyEventRouter::Status status) {
|
| return kErrorPermissionDenied;
|
| case BluetoothLowEnergyEventRouter::kStatusErrorNotFound:
|
| return kErrorNotFound;
|
| + case BluetoothLowEnergyEventRouter::kStatusErrorAlreadyConnected:
|
| + return kErrorAlreadyConnected;
|
| + case BluetoothLowEnergyEventRouter::kStatusErrorNotConnected:
|
| + return kErrorNotConnected;
|
| case BluetoothLowEnergyEventRouter::kStatusSuccess:
|
| NOTREACHED();
|
| break;
|
| @@ -125,6 +131,88 @@ bool BluetoothLowEnergyExtensionFunction::RunAsync() {
|
| return true;
|
| }
|
|
|
| +bool BluetoothLowEnergyConnectFunction::DoWork() {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| +
|
| + BluetoothLowEnergyEventRouter* event_router =
|
| + GetEventRouter(browser_context());
|
| +
|
| + // The adapter must be initialized at this point, but return an error instead
|
| + // of asserting.
|
| + if (!event_router->HasAdapter()) {
|
| + SetError(kErrorAdapterNotInitialized);
|
| + SendResponse(false);
|
| + return false;
|
| + }
|
| +
|
| + scoped_ptr<apibtle::Connect::Params> params(
|
| + apibtle::Connect::Params::Create(*args_));
|
| + EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
|
| +
|
| + BluetoothLowEnergyEventRouter::Status status =
|
| + event_router->Connect(
|
| + GetExtension(),
|
| + params->device_address,
|
| + base::Bind(&BluetoothLowEnergyConnectFunction::SuccessCallback, this),
|
| + base::Bind(&BluetoothLowEnergyConnectFunction::ErrorCallback, this));
|
| +
|
| + if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) {
|
| + SetError(StatusToString(status));
|
| + SendResponse(false);
|
| + return false;
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| +void BluetoothLowEnergyConnectFunction::SuccessCallback() {
|
| + SendResponse(true);
|
| +}
|
| +
|
| +void BluetoothLowEnergyConnectFunction::ErrorCallback(
|
| + BluetoothLowEnergyEventRouter::Status status) {
|
| + SetError(StatusToString(status));
|
| + SendResponse(false);
|
| +}
|
| +
|
| +bool BluetoothLowEnergyDisconnectFunction::DoWork() {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| +
|
| + BluetoothLowEnergyEventRouter* event_router =
|
| + GetEventRouter(browser_context());
|
| +
|
| + // The adapter must be initialized at this point, but return an error instead
|
| + // of asserting.
|
| + if (!event_router->HasAdapter()) {
|
| + SetError(kErrorAdapterNotInitialized);
|
| + SendResponse(false);
|
| + return false;
|
| + }
|
| +
|
| + scoped_ptr<apibtle::Disconnect::Params> params(
|
| + apibtle::Disconnect::Params::Create(*args_));
|
| + EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
|
| +
|
| + BluetoothLowEnergyEventRouter::Status status =
|
| + event_router->Disconnect(
|
| + GetExtension(),
|
| + params->device_address,
|
| + base::Bind(&BluetoothLowEnergyDisconnectFunction::SuccessCallback,
|
| + this));
|
| +
|
| + if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) {
|
| + SetError(StatusToString(status));
|
| + SendResponse(false);
|
| + return false;
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| +void BluetoothLowEnergyDisconnectFunction::SuccessCallback() {
|
| + SendResponse(true);
|
| +}
|
| +
|
| bool BluetoothLowEnergyGetServiceFunction::DoWork() {
|
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
|
|
|
|