コード例 #1
0
void DevicesManager::findBleDevices()
{
    emit startedScanningForDevices();

    bb::system::SystemToast toast;
    toast.setBody("Searching for Bluetooth LE devices ... please wait until search has completed ...");
    toast.setPosition(bb::system::SystemUiPosition::MiddleCenter);
    toast.show();

    if (!bt_initialised) {

        // Initialise the Bluetooth device and allocate the required resources for the library. Specify a call back function for Bluetooth events.
        bt_device_init(btEvent);
        // make sure the Bluetooth radio is switched on
        if (!bt_ldev_get_power()) {
            bt_ldev_set_power(true);
        }
        bt_initialised = true;
    }

    DataContainer *dc = DataContainer::getInstance();
    dc->clearDeviceList();
    // note that this is a blocking call. For each device discovered however, a call back is made to btEvent with event type BT_EVT_DEVICE_ADDED
    bt_disc_start_inquiry(BT_INQUIRY_GIAC);

    bt_remote_device_t **remoteDeviceArray = 0;
    bt_remote_device_t *remoteDevice = 0;

    remoteDeviceArray = bt_disc_retrieve_devices(BT_DISCOVERY_ALL, 0);

    int device_count;
    device_count=0;

    if (remoteDeviceArray) {
        for (int i = 0; (remoteDevice = remoteDeviceArray[i]); ++i) {
            const int deviceType = bt_rdev_get_type(remoteDevice);
            if ((deviceType == BT_DEVICE_TYPE_LE_PUBLIC) || (deviceType == BT_DEVICE_TYPE_LE_PRIVATE)) {
                device_count++;
                DevicesManager::getDevicesManager()->extractAndStoreBleDeviceAttributes(remoteDevice);
            }
            bt_rdev_free(remoteDevice);
        }
//        qDebug() << "YYYY DevicesManager::findBleDevices() - freeing buffer";
//        if (remoteDeviceArray) {
//            bt_rdev_free_array(remoteDeviceArray);
//        }
    }

    qDebug() << "XXXX number of devices found=" << device_count;
    dc->setDeviceCount(device_count);

    emit setDeviceCount(QVariant(device_count));
    emit finishedScanningForDevices();

}
コード例 #2
0
//! [1]
void RemoteDeviceInfo::update(const QString &deviceAddress)
{
    bt_remote_device_t *remote_device = bt_rdev_get_device(deviceAddress.toAscii());

    if (!remote_device)
        return;

    bool ok = false;
    char buffer[128];
    const int bufferSize = sizeof(buffer);

    const QString unknown = tr("Unknown");
    const QString notAvailable = tr("N/A");

    // Display all known basic device information.
    ok = (bt_rdev_get_friendly_name(remote_device, buffer, bufferSize) == 0);
    m_name = (ok ? QString::fromLatin1(buffer) : unknown);

    m_address = deviceAddress;

    const int deviceClass = bt_rdev_get_device_class(remote_device, BT_COD_DEVICECLASS);
    if (deviceClass >= 0) {
        m_deviceClass.sprintf("0x%x", deviceClass);
    } else {
        m_deviceClass = unknown;
    }

    const int deviceType = bt_rdev_get_type(remote_device);
    m_deviceType = ((deviceType == BT_DEVICE_TYPE_LE_PUBLIC || deviceType == BT_DEVICE_TYPE_LE_PRIVATE) ? tr("Low energy") : tr("Regular"));

    m_encrypted = ((bt_rdev_is_encrypted(remote_device) >= 0) ? tr("true") : tr("false"));

    bool paired = false;
    ok = (bt_rdev_is_paired(remote_device, &paired) == 0);
    m_paired = (ok ? (paired ? tr("true") : tr("false")) : unknown);

    m_trusted = (bt_rdev_is_trusted(remote_device) ? tr("true") : tr("false"));

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

    // Display all known low energy parameters.
    uint16_t min_conn_ivl, max_conn_ivl, latency, super_tmo, appearance;
    ok = (bt_rdev_get_le_conn_params(remote_device, &min_conn_ivl, &max_conn_ivl, &latency, &super_tmo) == 0);

    m_minimumConnectionInterval = (ok ? QString::number(min_conn_ivl) : notAvailable);
    m_maximumConnectionInterval = (ok ? QString::number(max_conn_ivl) : notAvailable);
    m_latency = (ok ? QString::number(latency) : notAvailable);
    m_supervisoryTimeout = (ok ? QString::number(super_tmo) : notAvailable);

    uint8_t flags, connectable;
    ok = (bt_rdev_get_le_info(remote_device, &appearance, &flags, &connectable) == 0);

    m_appearance = (ok ? QString::number(appearance) : notAvailable);
    m_flags = (ok ? QString::number(flags) : notAvailable);
    m_connectable = (ok ? QString::number(connectable) : notAvailable);

    // clear model
	m_model->clear();

    // Display any found regular Bluetooth services.
	// get more info here: http://www.bluetooth.org/en-us/specification/assigned-numbers-overview/service-discovery
    char **services_array = bt_rdev_get_services(remote_device);
    if (services_array) {
        for (int i = 0; services_array[i]; i++) {
            QVariantMap map;

            const QString uuid = QString::fromLatin1(services_array[i]);

            map["uuid"] = uuid;
            map["address"] = m_address;

            if (uuid.startsWith("0x0001"))
                map["serviceType"] = tr("SDP");
            else if (uuid.startsWith("0x0003"))
                map["serviceType"] = tr("RFCOMM");
            else if (uuid.startsWith("0x0008"))
                map["serviceType"] = tr("OBEX");
            else if (uuid.startsWith("0x000c"))
                map["serviceType"] = tr("HTTP");
            else if (uuid.startsWith("0x0100"))
                map["serviceType"] = tr("L2CAP");
            else if (uuid.startsWith("0x000f"))
                map["serviceType"] = tr("BNEP");
            else if (uuid.startsWith("0x1000"))
                map["serviceType"] = tr("Service Discovery");
            else if (uuid.startsWith("0x1001"))
                map["serviceType"] = tr("Browse Group Descriptor");
            else if (uuid.startsWith("0x1002"))
                map["serviceType"] = tr("Public Browse Group");
            else if (uuid.startsWith("0x1101"))
                map["serviceType"] = tr("Serial Port");
            else if (uuid.startsWith("0x1102"))
                map["serviceType"] = tr("Public Browse Group");
            else if (uuid.startsWith("0x1105"))
                map["serviceType"] = tr("OBEX Object Push");
            else if (uuid.startsWith("0x1106"))
                map["serviceType"] = tr("OBEX File Transfer");
            else if (uuid.startsWith("0x1115"))
                map["serviceType"] = tr("Personal Area Networking");
            else if (uuid.startsWith("0x1116"))
                map["serviceType"] = tr("Network Access Point");
            else if (uuid.startsWith("0x1117"))
                map["serviceType"] = tr("Group Network");
            else if (uuid.startsWith("0x1124"))
				map["serviceType"] = tr("Human Interface Device (HID)");
            else if (uuid.startsWith("0x1200"))
				map["serviceType"] = tr("Device Identification (DID)");
            else
                map["serviceType"] = tr("Other");

            m_model->insert(map);
        }

        bt_rdev_free_services(services_array);
    }

    // Display any found Bluetooth low energy services.
    if (deviceType == BT_DEVICE_TYPE_LE_PUBLIC || deviceType == BT_DEVICE_TYPE_LE_PRIVATE) {
        services_array = bt_rdev_get_services_gatt(remote_device);
        if (services_array) {
            for (int i = 0; services_array[i]; i++) {
                QVariantMap map;

                map["uuid"] = QString::fromLatin1(services_array[i]);
                map["address"] = m_address;
                map["serviceType"] = tr("GATT");

                m_model->insert(map);
            }

            bt_rdev_free_services(services_array);
        }
    }

    bt_rdev_free(remote_device);

    emit changed();
}