Bluetooth: can connect devices

pull/55/head
reionwong 4 years ago
parent b44defecbe
commit cea85b8928

1
debian/control vendored

@ -16,6 +16,7 @@ Build-Depends: cmake,
modemmanager-qt-dev,
qtbase5-dev,
libqt5x11extras5-dev,
libkf5bluezqt-dev,
qtdeclarative5-dev,
qtquickcontrols2-5-dev,
qttools5-dev,

@ -3,6 +3,7 @@
#include <BluezQt/InitManagerJob>
#include <BluezQt/Adapter>
#include <BluezQt/Device>
BluetoothManager::BluetoothManager(QObject *parent)
: QObject(parent)
@ -13,6 +14,8 @@ BluetoothManager::BluetoothManager(QObject *parent)
initJob->start();
connect(initJob, &BluezQt::InitManagerJob::result, this, &BluetoothManager::onInitJobResult);
connect(m_agent, &BluetoothAgent::confirmationRequested, this, &BluetoothManager::confirmationRequested);
connect(m_manager, &BluezQt::Manager::bluetoothBlockedChanged, this, [=] (bool blocked) {
if (!blocked) {
BluezQt::AdapterPtr adaptor = m_manager->adapters().first();
@ -39,6 +42,37 @@ void BluetoothManager::setName(const QString &name)
adaptor->setName(name);
}
void BluetoothManager::connectToDevice(const QString address)
{
BluezQt::AdapterPtr adaptor = m_manager->usableAdapter();
BluezQt::DevicePtr device = adaptor->deviceForAddress(address);
qDebug() << "hello: " << address << device->name();
m_device = device;
device->setTrusted(true);
BluezQt::PendingCall *call = m_device->connectToDevice();
// connect(call, &BluezQt::PendingCall::finished, this, &Bluetooth::connectFinished);
// connect(m_device.data(), &BluezQt::Device::connectedChanged, this, &Bluetooth::connectedStateChanged);
}
void BluetoothManager::requestParingConnection(const QString address)
{
BluezQt::AdapterPtr adaptor = m_manager->usableAdapter();
BluezQt::DevicePtr device = adaptor->deviceForAddress(address);
m_device = device;
// m_address = address;
BluezQt::PendingCall *pairCall = m_device->pair();
// connect(pairCall, &BluezQt::PendingCall::finished, this, &Bluetooth::pairingFinished);
}
void BluetoothManager::confirmMatchButton(const bool match)
{
if (match){
m_req.accept();
} else{
m_req.reject();
}
}
void BluetoothManager::onInitJobResult(BluezQt::InitManagerJob *job)
{
if (job->error()) {
@ -68,3 +102,9 @@ void BluetoothManager::operationalChanged(bool operational)
BluezQt::Manager::startService();
}
}
void BluetoothManager::confirmationRequested(const QString &passkey, const BluezQt::Request<> &req)
{
m_req = req;
Q_EMIT showPairDialog(m_device->name(), passkey);
}

@ -14,15 +14,24 @@ public:
~BluetoothManager();
Q_INVOKABLE void setName(const QString &name);
Q_INVOKABLE void connectToDevice(const QString address);
Q_INVOKABLE void requestParingConnection(const QString address);
Q_INVOKABLE void confirmMatchButton(const bool match);
signals:
void showPairDialog(const QString name, const QString pin);
private slots:
void onInitJobResult(BluezQt::InitManagerJob *job);
void operationalChanged(bool operational);
void confirmationRequested(const QString &passkey, const BluezQt::Request<> &req);
private:
BluezQt::Manager *m_manager;
BluetoothAgent *m_agent;
BluezQt::AdapterPtr m_adapter;
BluezQt::DevicePtr m_device;
BluezQt::Request<> m_req;
QString m_name;
};

@ -40,7 +40,6 @@ ItemPage {
for (var i = 0; i < Bluez.Manager.adapters.length; ++i) {
var adapter = Bluez.Manager.adapters[i]
console.log(adapter + ", " + enabled)
adapter.powered = enabled
}
}
@ -56,6 +55,15 @@ ItemPage {
BluetoothManager {
id: bluetoothMgr
onShowPairDialog: {
_pairDialog.title = name
_pairDialog.visible = true
}
}
PairDialog {
id: _pairDialog
}
// Label {
@ -104,39 +112,76 @@ ItemPage {
}
ListView {
id: _listView
visible: count > 0
interactive: false
spacing: FishUI.Units.largeSpacing
property var itemHeight: 50
Layout.fillWidth: true
onCountChanged: {
console.log(count)
Layout.preferredHeight: {
var totalHeight = 0
for (var i = 0; i < _listView.visibleChildren.length; ++i) {
totalHeight += _listView.visibleChildren[i].height
}
return totalHeight
}
Layout.fillWidth: true
Layout.preferredHeight: itemHeight * count + ((count - 1) * spacing)
model: devicesProxyModel //Bluez.Manager.bluetoothOperational ? devicesModel : []
section.property: "Section"
section.delegate: Label {
color: FishUI.Theme.disabledTextColor
topPadding: FishUI.Units.largeSpacing
bottomPadding: FishUI.Units.largeSpacing
text: section == "Connected" ? qsTr("Connected devices")
: qsTr("Available devices")
}
delegate: Item {
width: ListView.view.itemHeight
height: 50
width: ListView.view.width
height: _itemLayout.implicitHeight + FishUI.Units.largeSpacing * 1.5
Rectangle {
anchors.fill: parent
radius: FishUI.Theme.smallRadius
color: FishUI.Theme.textColor
opacity: mouseArea.pressed ? 0.15 : mouseArea.containsMouse ? 0.1 : 0.0
}
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
acceptedButtons: Qt.LeftButton
onClicked: {
if (model.Connected && model.Paired){
return
}
if (model.Paired) {
bluetoothMgr.connectToDevice(model.Address)
} else {
bluetoothMgr.requestParingConnection(model.Address)
}
}
}
ColumnLayout {
id: _itemLayout
anchors.fill: parent
Label {
text: model.Device
text: model.DeviceFullName
}
}
}
}
}
Item {
height: FishUI.Units.largeSpacing * 2
}
}
}
}

@ -0,0 +1,61 @@
import QtQuick 2.4
import QtQuick.Window 2.12
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3
import Cutefish.Settings 1.0
import FishUI 1.0 as FishUI
import "../"
Window {
id: control
width: 200
height: 100
minimumWidth: 200
minimumHeight: 100
maximumWidth: 200
maximumHeight: 100
modality: Qt.WindowModal
flags: Qt.WindowStaysOnTopHint
visible: false
title: " "
Rectangle {
anchors.fill: parent
color: FishUI.Theme.secondBackgroundColor
}
ColumnLayout {
anchors.fill: parent
anchors.margins: FishUI.Units.largeSpacing
Label {
text: qsTr("Bluetooth Pairing Request")
}
RowLayout {
spacing: FishUI.Units.largeSpacing
Button {
text: qsTr("Cancel")
Layout.fillWidth: true
onClicked: {
control.visible = false
bluetoothMgr.confirmMatchButton(false)
}
}
Button {
text: qsTr("OK")
Layout.fillWidth: true
flat: true
onClicked: {
control.visible = false
bluetoothMgr.confirmMatchButton(true)
}
}
}
}
}

@ -119,5 +119,6 @@
<file>images/sidebar/dark/touchpad.svg</file>
<file>images/logo.png</file>
<file>images/dock_straight.svg</file>
<file>qml/Bluetooth/PairDialog.qml</file>
</qresource>
</RCC>

@ -468,17 +468,17 @@
</message>
<message>
<location filename="../src/qml/Bluetooth/Main.qml" line="30"/>
<location filename="../src/qml/Bluetooth/Main.qml" line="89"/>
<location filename="../src/qml/Bluetooth/Main.qml" line="97"/>
<source>Bluetooth</source>
<translation type="unfinished">Bluetooth</translation>
</message>
<message>
<location filename="../src/qml/Bluetooth/Main.qml" line="122"/>
<location filename="../src/qml/Bluetooth/Main.qml" line="137"/>
<source>Connected devices</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/qml/Bluetooth/Main.qml" line="123"/>
<location filename="../src/qml/Bluetooth/Main.qml" line="138"/>
<source>Available devices</source>
<translation type="unfinished"></translation>
</message>
@ -607,6 +607,24 @@
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>PairDialog</name>
<message>
<location filename="../src/qml/Bluetooth/PairDialog.qml" line="35"/>
<source>Bluetooth Pairing Request</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/qml/Bluetooth/PairDialog.qml" line="42"/>
<source>Cancel</source>
<translation type="unfinished">Cancel</translation>
</message>
<message>
<location filename="../src/qml/Bluetooth/PairDialog.qml" line="51"/>
<source>OK</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>QObject</name>
<message>

@ -468,17 +468,17 @@
</message>
<message>
<location filename="../src/qml/Bluetooth/Main.qml" line="30"/>
<location filename="../src/qml/Bluetooth/Main.qml" line="89"/>
<location filename="../src/qml/Bluetooth/Main.qml" line="97"/>
<source>Bluetooth</source>
<translation></translation>
</message>
<message>
<location filename="../src/qml/Bluetooth/Main.qml" line="122"/>
<location filename="../src/qml/Bluetooth/Main.qml" line="137"/>
<source>Connected devices</source>
<translation></translation>
</message>
<message>
<location filename="../src/qml/Bluetooth/Main.qml" line="123"/>
<location filename="../src/qml/Bluetooth/Main.qml" line="138"/>
<source>Available devices</source>
<translation></translation>
</message>
@ -607,6 +607,24 @@
<translation></translation>
</message>
</context>
<context>
<name>PairDialog</name>
<message>
<location filename="../src/qml/Bluetooth/PairDialog.qml" line="35"/>
<source>Bluetooth Pairing Request</source>
<translation></translation>
</message>
<message>
<location filename="../src/qml/Bluetooth/PairDialog.qml" line="42"/>
<source>Cancel</source>
<translation></translation>
</message>
<message>
<location filename="../src/qml/Bluetooth/PairDialog.qml" line="51"/>
<source>OK</source>
<translation></translation>
</message>
</context>
<context>
<name>QObject</name>
<message>

Loading…
Cancel
Save