92bool QBluetoothServer::listen(
const QBluetoothAddress &localAdapter, quint16 port)
94 Q_D(QBluetoothServer);
95 if (serverType() != QBluetoothServiceInfo::RfcommProtocol) {
96 d->m_lastError = UnsupportedProtocolError;
97 emit errorOccurred(d->m_lastError);
101 if (!ensureAndroidPermission(QBluetoothPermission::Access)) {
102 qCWarning(QT_BT_ANDROID) <<
"Bluetooth server listen() failed due to missing permissions";
103 d->m_lastError = QBluetoothServer::MissingPermissionsError;
104 emit errorOccurred(d->m_lastError);
108 const QList<QBluetoothHostInfo> localDevices = QBluetoothLocalDevice::allDevices();
109 if (localDevices.isEmpty()) {
110 qCWarning(QT_BT_ANDROID) <<
"Device does not support Bluetooth";
111 d->m_lastError = QBluetoothServer::UnknownError;
112 emit errorOccurred(d->m_lastError);
116 if (!localAdapter.isNull()) {
118 for (
const QBluetoothHostInfo &hostInfo : localDevices) {
119 if (hostInfo.address() == localAdapter) {
126 qCWarning(QT_BT_ANDROID) << localAdapter.toString() <<
"is not a valid local Bt adapter";
131 if (d->isListening())
135 QJniObject btAdapter = getDefaultBluetoothAdapter();
137 if (!btAdapter.isValid()) {
138 qCWarning(QT_BT_ANDROID) <<
"Device does not support Bluetooth";
139 d->m_lastError = QBluetoothServer::UnknownError;
140 emit errorOccurred(d->m_lastError);
144 const int state = btAdapter.callMethod<jint>(
"getState");
146 d->m_lastError = QBluetoothServer::PoweredOffError;
147 emit errorOccurred(d->m_lastError);
148 qCWarning(QT_BT_ANDROID) <<
"Bluetooth device is powered off";
155 for (
int i=1; ; i++){
156 if (__fakeServerPorts.key(i) == 0) {
163 if (__fakeServerPorts.key(port) == 0) {
164 __fakeServerPorts[d] = port;
166 qCDebug(QT_BT_ANDROID) <<
"Port" << port <<
"registered";
168 qCWarning(QT_BT_ANDROID) <<
"server with port" << port <<
"already registered or port invalid";
169 d->m_lastError = ServiceAlreadyRegisteredError;
170 emit errorOccurred(d->m_lastError);
174 connect(d->thread, SIGNAL(newConnection()),
175 this, SIGNAL(newConnection()));
176 connect(d->thread, SIGNAL(errorOccurred(QBluetoothServer::Error)),
this,
177 SIGNAL(errorOccurred(QBluetoothServer::Error)), Qt::QueuedConnection);