From b602d29e0e5601216947f27e5202ac8bc3a8b96f Mon Sep 17 00:00:00 2001 From: Andrzej Popowski Date: Fri, 7 Aug 2015 10:35:53 +0200 Subject: [PATCH] [Bluetooth] - fixing BLE device disconnect function Change-Id: I8b2a01e5a000619779bd37a34d78fd7ed32dc4d5 Signed-off-by: Andrzej Popowski --- src/bluetooth/bluetooth_api.js | 7 +++++-- src/bluetooth/bluetooth_le_device.cc | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/bluetooth/bluetooth_api.js b/src/bluetooth/bluetooth_api.js index 6f90376..f813278 100755 --- a/src/bluetooth/bluetooth_api.js +++ b/src/bluetooth/bluetooth_api.js @@ -652,7 +652,7 @@ BluetoothLEDevice.prototype.disconnect = function() { nullable : true } ]); - var callback = function(result) { + var callback = function(result) { if (native.isFailure(result)) { native.callIfPossible(args.errorCallback, native.getErrorObject(result)); } else { @@ -660,7 +660,10 @@ BluetoothLEDevice.prototype.disconnect = function() { } }; - native.call('BluetoothLEDevice_disconnect', {address : this.address}, callback); + var result = native.call('BluetoothLEDevice_disconnect', {address : this.address}, callback); + if (native.isFailure(result)) { + throw native.getErrorObject(result); + } }; BluetoothLEDevice.prototype.getService = function() { diff --git a/src/bluetooth/bluetooth_le_device.cc b/src/bluetooth/bluetooth_le_device.cc index bcc22c4..c4601eb 100755 --- a/src/bluetooth/bluetooth_le_device.cc +++ b/src/bluetooth/bluetooth_le_device.cc @@ -313,14 +313,28 @@ void BluetoothLEDevice::Disconnect(const picojson::value& data, const auto callback_handle = util::GetAsyncCallbackHandle(data); const auto& args = util::GetArguments(data); - const auto& address = common::FromJson(args, "address"); - int ret = bt_gatt_disconnect(address.c_str()); + bool connected = false; + int ret = bt_device_is_profile_connected(address.c_str(), BT_PROFILE_GATT, &connected); + if (BT_ERROR_NONE != ret) { + instance_.AsyncResponse( + callback_handle, + PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to disconnect.")); + return; + } + if (!connected) { + ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR, + "Bluetooth low energy device is not connected"), + &out); + return; + } + + ret = bt_gatt_disconnect(address.c_str()); if (BT_ERROR_NONE != ret) { instance_.AsyncResponse( callback_handle, - PlatformResult(util::GetBluetoothError(ret, "Failed to disconnect."))); + PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to disconnect.")); return; } -- 2.7.4