Chromium Code Reviews| Index: device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm |
| diff --git a/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm b/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm |
| index e3ce08ecc427e90cd5c527bda8b30a1ef8037d6a..12956a9af4e775fb2b6841c71cb6d8ccf1df3776 100644 |
| --- a/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm |
| +++ b/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm |
| @@ -7,6 +7,7 @@ |
| #import <CoreBluetooth/CoreBluetooth.h> |
| #include "device/bluetooth/bluetooth_adapter_mac.h" |
| +#include "device/bluetooth/bluetooth_device_mac.h" |
| #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| namespace device { |
| @@ -65,7 +66,8 @@ BluetoothRemoteGattCharacteristicMac::BluetoothRemoteGattCharacteristicMac( |
| BluetoothRemoteGattServiceMac* gatt_service, |
| CBCharacteristic* cb_characteristic) |
| : gatt_service_(gatt_service), |
| - cb_characteristic_(cb_characteristic, base::scoped_policy::RETAIN) { |
| + cb_characteristic_(cb_characteristic, base::scoped_policy::RETAIN), |
| + characteristic_value_read_in_progress_(false) { |
| uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID( |
| [cb_characteristic_.get() UUID]); |
| identifier_ = |
| @@ -133,7 +135,19 @@ void BluetoothRemoteGattCharacteristicMac::StartNotifySession( |
| void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic( |
| const ValueCallback& callback, |
| const ErrorCallback& error_callback) { |
| - NOTIMPLEMENTED(); |
| + if (!IsReadable()) { |
| + error_callback.Run(BluetoothRemoteGattService::GATT_ERROR_NOT_PERMITTED); |
|
ortuno
2016/06/16 18:22:18
ReadRemoteCharacteristic should always be async. P
jlebel
2016/06/22 18:03:58
Can I denounce windows code?
https://cs.chromium.o
|
| + return; |
| + } |
| + if (characteristic_value_read_in_progress_) { |
| + error_callback.Run(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS); |
|
ortuno
2016/06/16 18:22:18
Same here, post a task.
jlebel
2016/06/22 18:03:58
Done.
|
| + return; |
| + } |
| + characteristic_value_read_in_progress_ = true; |
| + read_characteristic_value_callbacks_ = |
| + std::make_pair(callback, error_callback); |
| + [gatt_service_->GetCBPeripheral() |
| + readValueForCharacteristic:cb_characteristic_]; |
| } |
| void BluetoothRemoteGattCharacteristicMac::WriteRemoteCharacteristic( |
| @@ -143,6 +157,29 @@ void BluetoothRemoteGattCharacteristicMac::WriteRemoteCharacteristic( |
| NOTIMPLEMENTED(); |
| } |
| +void BluetoothRemoteGattCharacteristicMac::DidReadValue(NSError* error) { |
| + if (!characteristic_value_read_in_progress_) { |
| + return; |
| + } |
| + characteristic_value_read_in_progress_ = false; |
| + if (error) { |
| + VLOG(1) << "Bluetooth error while reading for characteristic, domain: " |
| + << error.domain.UTF8String << ", error code: " << error.code; |
| + BluetoothGattService::GattErrorCode error_code = |
| + BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| + read_characteristic_value_callbacks_.second.Run(error_code); |
| + return; |
| + } |
| + NSData* nsdata_value = cb_characteristic_.get().value; |
| + const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); |
|
ortuno
2016/06/16 18:22:18
nit: Shouldn't you use [nsdata_value bytes] since
jlebel
2016/06/22 18:03:58
Calling [nsdata_value bytes] or doing |nsdata_valu
|
| + value_.assign(buffer, buffer + nsdata_value.length); |
| + read_characteristic_value_callbacks_.first.Run(value_); |
| +} |
| + |
| +bool BluetoothRemoteGattCharacteristicMac::IsReadable() const { |
| + return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ; |
| +} |
| + |
| CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic() |
| const { |
| return cb_characteristic_.get(); |