void QLowEnergyControllerPrivate::writeDescriptor( const QSharedPointer<QLowEnergyServicePrivate> service, const QLowEnergyHandle /*charHandle*/, const QLowEnergyHandle descHandle, const QByteArray &newValue) { Q_ASSERT(!service.isNull()); QAndroidJniEnvironment env; jbyteArray payload; payload = env->NewByteArray(newValue.size()); env->SetByteArrayRegion(payload, 0, newValue.size(), (jbyte *)newValue.constData()); bool result = false; if (hub) { qCDebug(QT_BT_ANDROID) << "Write descriptor with handle " << descHandle << newValue.toHex() << "(service:" << service->uuid << ")"; result = hub->javaObject().callMethod<jboolean>("writeDescriptor", "(I[B)Z", descHandle, payload); } if (env->ExceptionOccurred()) { env->ExceptionDescribe(); env->ExceptionClear(); result = false; } env->DeleteLocalRef(payload); if (!result) service->setError(QLowEnergyService::DescriptorWriteError); }
qint64 QBluetoothSocketPrivate::writeData(const char *data, qint64 maxSize) { //TODO implement buffered behavior (so far only unbuffered) Q_Q(QBluetoothSocket); if (state != QBluetoothSocket::ConnectedState || !outputStream.isValid()) { qCWarning(QT_BT_ANDROID) << "Socket::writeData: " << state << outputStream.isValid(); errorString = QBluetoothSocket::tr("Cannot write while not connected"); q->setSocketError(QBluetoothSocket::OperationError); return -1; } QAndroidJniEnvironment env; jbyteArray nativeData = env->NewByteArray((qint32)maxSize); env->SetByteArrayRegion(nativeData, 0, (qint32)maxSize, reinterpret_cast<const jbyte*>(data)); outputStream.callMethod<void>("write", "([BII)V", nativeData, 0, (qint32)maxSize); env->DeleteLocalRef(nativeData); emit q->bytesWritten(maxSize); if (env->ExceptionCheck()) { qCWarning(QT_BT_ANDROID) << "Error while writing"; env->ExceptionDescribe(); env->ExceptionClear(); errorString = QBluetoothSocket::tr("Error during write on socket."); q->setSocketError(QBluetoothSocket::NetworkError); return -1; } return maxSize; }
qint64 QSerialPortPrivate::writeToPort(const char *data, qint64 maxSize) { if (deviceId == BAD_PORT) { q_ptr->setError(QSerialPort::NotOpenError); return 0; } QAndroidJniEnvironment jniEnv; jbyteArray jarrayL = jniEnv->NewByteArray(maxSize); jniEnv->SetByteArrayRegion(jarrayL, 0, maxSize, (jbyte *)data); if (jniEnv->ExceptionCheck()) jniEnv->ExceptionClear(); int resultL = QAndroidJniObject::callStaticMethod<jint>( kJniClassName, "write", "(I[BI)I", deviceId, jarrayL, internalWriteTimeoutMsec); if (jniEnv->ExceptionCheck()) { jniEnv->ExceptionClear(); q_ptr->setErrorString(QStringLiteral("Writing to the device threw an exception")); jniEnv->DeleteLocalRef(jarrayL); return 0; } jniEnv->DeleteLocalRef(jarrayL); return resultL; }
void QLowEnergyControllerPrivate::writeCharacteristic( const QSharedPointer<QLowEnergyServicePrivate> service, const QLowEnergyHandle charHandle, const QByteArray &newValue, bool writeWithResponse) { //TODO don't ignore WriteWithResponse, right now we assume responses Q_ASSERT(!service.isNull()); if (!service->characteristicList.contains(charHandle)) return; QAndroidJniEnvironment env; jbyteArray payload; payload = env->NewByteArray(newValue.size()); env->SetByteArrayRegion(payload, 0, newValue.size(), (jbyte *)newValue.constData()); bool result = false; if (hub) { qCDebug(QT_BT_ANDROID) << "Write characteristic with handle " << charHandle << newValue.toHex() << "(service:" << service->uuid << ", writeWithResponse:" << writeWithResponse << ")"; result = hub->javaObject().callMethod<jboolean>("writeCharacteristic", "(I[BI)Z", charHandle, payload, writeWithResponse ? QLowEnergyService::WriteWithResponse : QLowEnergyService::WriteWithoutResponse); } if (env->ExceptionOccurred()) { env->ExceptionDescribe(); env->ExceptionClear(); result = false; } env->DeleteLocalRef(payload); if (!result) service->setError(QLowEnergyService::CharacteristicWriteError); }