![](/img/trans.png)
[英]How to get connect with closest bluetooth low energy device from android ?
[英]How to connect to a Bluetooth low energy device from iOS with Qt?
我正在创建一个Qt应用程序,将iOS与BLE板连接在一起。
void EasyConnect::startDiscovery()
{
discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &EasyConnect::addDevice);
// connect(discoveryAgent, QOverload<QBluetoothDeviceDiscoveryAgent::Error>::of(&QBluetoothDeviceDiscoveryAgent::error), this, &Device::deviceScanError);
// connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Device::deviceScanFinished);
discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}
读完附近的ble组件后,我像这样连接Android设备:
void EasyConnect::connectToService(QBluetoothAddress address)
{
m_control = new QLowEnergyController(address);
connect(m_control, &QLowEnergyController::serviceDiscovered, this, &EasyConnect::serviceDiscovered);
// connect(m_control, &QLowEnergyController::discoveryFinished,
// this, &DeviceHandler::serviceScanDone);
connect(m_control, static_cast<void (QLowEnergyController::*)(QLowEnergyController::Error)>(&QLowEnergyController::error),
this, [this](QLowEnergyController::Error error) {
Q_UNUSED(error);
qDebug() << "Cannot connect to remote device.";
});
connect(m_control, &QLowEnergyController::connected, this, [this]() {
qDebug() << "controller connect.";
m_control->discoverServices();
});
connect(m_control, &QLowEnergyController::disconnected, this, [this]() {
qDebug() << "controller disconneted.";
});
// Connect
m_control->connectToDevice();
}
因此,在Android上,我从
QBluetoothDeviceInfo
但是iOS不能从ble获得我的mac地址,而只能从UUID获得。 我尝试在线搜索doc,但没有找到有关通过uuid连接ios和蓝牙服务的信息。
有一种在Mac地址中转换uuid的模式,或者有一个库,用于通过UUID连接服务ble设备和ios。
您应该使用QLowEnergyController。如果将设备用作外围设备,请使用此方法。 我创建了包含QLowEnergyController实例的类并对其进行处理。 这是我在应用程序中使用的部分代码:
void BLEAdvertisementCommunicator::startAdvertisingService() {
mController = QLowEnergyController::createPeripheral(this);
mAdvertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
mAdvertisingData.setIncludePowerLevel(false);
mAdvertisingData.setLocalName("MyApplication");
QLowEnergyServiceData serviceData;
serviceData.setType(QLowEnergyServiceData::ServiceTypePrimary);
serviceData.setUuid(QBluetoothUuid(serviceUuid));
auto service = mController->addService(serviceData, mController);
connect(mController, SIGNAL(connected()), this, SLOT(onDeviceConnected()));
connect(mController, SIGNAL(disconnected()), this, SLOT(onDeviceDisconnected()));
connect(mController, SIGNAL(error(QLowEnergyController::Error)), this, SLOT(onError(QLowEnergyController::Error)));
mResponseData.setServices({QBluetoothUuid(serviceUuid)});
mController->startAdvertising(QLowEnergyAdvertisingParameters(), mAdvertisingData,
mResponseData);
}
无法通过在iOS上进行扫描来获取BLE外围设备的MAC地址。 这意味着您可能需要在BLE外围板上找到或添加一些其他数据,以便能够在BLE扫描期间识别它。
有关更多详细信息,请参见此答案: 获取蓝牙低功耗外设的MAC地址
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.