void ConnectionManager::connectToServer(QString addr) {
	if (!is_connected) {
		const int fd = bt_spp_open(addr.toAscii().data(), (char*) SERVER_UUID,
				false);

		if (fd >= 0) {
			m_IDT.init(fd);
			m_IDT.start();
			is_connected = true;
		} else
			std::cout << "Open SPP Fail" << std::endl;
	}
}
Ejemplo n.º 2
0
void QBluetoothSocketPrivate::connectToService(const QBluetoothAddress &address,
                                               const QBluetoothUuid &uuid,
                                               QIODevice::OpenMode openMode)
{
    Q_Q(QBluetoothSocket);
    Q_UNUSED(openMode);
    qCDebug(QT_BT_QNX) << "Connecting socket";

    m_peerAddress = address;
#ifdef QT_QNX_BT_BLUETOOTH
    QByteArray b_uuid = uuid.toByteArray();
    b_uuid = b_uuid.mid(1, b_uuid.length() - 2);
    socket = bt_spp_open(address.toString().toUtf8().data(), b_uuid.data(), false);
    if (socket == -1) {
        qCWarning(QT_BT_QNX) << "Could not connect to" << address.toString() << b_uuid <<  qt_error_string(errno);
        errorString = qt_error_string(errno);
        q->setSocketError(QBluetoothSocket::NetworkError);
        return;
    }

    delete readNotifier;
    delete connectWriteNotifier;

    readNotifier = new QSocketNotifier(socket, QSocketNotifier::Read);
    QObject::connect(readNotifier, SIGNAL(activated(int)), this, SLOT(_q_readNotify()));
    connectWriteNotifier = new QSocketNotifier(socket, QSocketNotifier::Write, q);
    QObject::connect(connectWriteNotifier, SIGNAL(activated(int)), this, SLOT(_q_writeNotify()));

    connecting = true;
    q->setOpenMode(openMode);
#else
    m_uuid = uuid;
    if (isServerSocket)
        return;

    if (state != QBluetoothSocket::UnconnectedState) {
        qCDebug(QT_BT_QNX) << "Socket already connected";
        return;
    }

    ppsSendControlMessage("connect_service", 0x1101, uuid, address.toString(), QString(), this, BT_SPP_CLIENT_SUBTYPE);
    ppsRegisterForEvent(QStringLiteral("service_connected"),this);
    ppsRegisterForEvent(QStringLiteral("get_mount_point_path"),this);
#endif
    q->setSocketState(QBluetoothSocket::ConnectingState);
}
void BlueToothManager::startSPPClient(int which) {
	Gate g(this, "startSPPClient()");

	g.log() << "Size of addresses" << addresses.size();
	g.log() << "Requested" << which;
	g.log() << "Value" << addresses[which];

	QString address = addresses[which];
	QByteArray data = address.toAscii();
	char * dataPtr = data.data();

	g.log() << "Address" << dataPtr;
	int rc = bt_spp_open(dataPtr, SPP_SERVICE_UUID, false);
	g.log() << "bt_spp_open";
	if (rc == -1) {
		g.log() << "Errno" << errno;
	} else {
		sppCallback(rc);
	}
}
Ejemplo n.º 4
0
ArduinoBTController::ArduinoBTController(QObject* parent)
    : QObject(parent)
{
    // Initialize the bt device and SPP library APIs.
    int errorCode;

    errorCode = bt_device_init(BTControllerCallbackArduino);
    errorCode = bt_spp_init();

    bt_remote_device_t **remote_device_array;
    bt_remote_device_t *next_remote_device = 0;

    // Retrieve and show all devices.
    remote_device_array = bt_disc_retrieve_devices(BT_DISCOVERY_ALL, 0);
    if (remote_device_array) {
        for (int i = 0; (next_remote_device = remote_device_array[i]); ++i) {
            QVariantMap map;
            char buffer[128];
            const int bufferSize = sizeof(buffer);

            bt_rdev_get_friendly_name(next_remote_device, buffer, bufferSize);
            map["deviceName"] = QString::fromLatin1(buffer);
            bt_rdev_get_addr(next_remote_device, buffer);
            map["deviceAddress"] = QString::fromLatin1(buffer);
            if ( (strcmp(buffer, "00:12:09:12:02:05") == 0) or  (strcmp(buffer, "20:13:06:18:04:04") == 0) ) { //this are my devicesis my device's mac adress
                qDebug() << "Found the BT device";
                int rssi = 0;
                bool ok = false;
                QString m_rssi;
                const QString unknown = tr("Unknown");
                bt_remote_device_t *remote_device = bt_rdev_get_device(buffer);

                ok = (bt_rdev_get_rssi(remote_device, &rssi) == 0);
                m_rssi = (ok ? QString::number(rssi) : unknown);

                const int fd = bt_spp_open(buffer, (char *) SPP_SERVICE_UUID,
                        false);

                if (fd >= 0) {

                    fflush(stdout);
                    if (fd >= 0) {
                        //starting the reciever/thread, I could use this to get data from a sensor
                            m_sppDataThread.init(fd,false);
                            m_sppDataThread.start();

                            //the shield has the bad habit of disconnecting if the app idles to long, so I'm sending a heartbeatsignal
                            m_keepAliveThread.init(fd);
                            m_keepAliveThread.start();

                            if (m_sppDataThread.active()) {
                                char buffer[80] = "HELLO"; //it is nice to say hello!
                                write(m_sppDataThread.getFD(), buffer, strlen(buffer));

                            }

                        }
                    } else {
                        qDebug() << "spp_open fail errno =" << QString::number(errno);
                    }

                }

            map["deviceClass"] = QString::number(
                    bt_rdev_get_device_class(next_remote_device,
                            BT_COD_DEVICECLASS));
            map["deviceType"] = tr("Bluetooth Devices  Paired");
            //  qDebug() << "map" << map;
        }
        bt_rdev_free_array(remote_device_array);
    }

    // Initialize the btdevice and SPP library APIs.
    bt_device_init(BTControllerCallbackArduino);
    bt_spp_init();
}