我有一个可以将我的 iOS 设备连接到蓝牙 arduino 的应用程序(到目前为止很好),到目前为止,这主要用于练习,但现在我收到了我应该连接的真实内容。
问题是我找不到! (当我搜索它时)。
如果我执行 scanwithservices: nil,我可以看到设备并连接到它。但是,如果我 scanwithservices: [device'sCBUUID] 那么我什么也得不到。
我使用其他应用程序检查了 CBUUID,使用我自己的应用程序并查看了设备文档,但无论如何我都找不到它。
它有一个自定义的 CBUUID,从我读过的内容来看,这不是标准的,CBUUID 是:
BLETPodService = CBUUID(string: "4D494B45-414c-5741-5953-524F434B5321")
搜索它不会产生任何结果,但是如果我扫描 nil 我会找到它,如果我使用 Bluefruit 应用程序(来自 Adafruit)检查它的特征,我可以看到它的服务和特征 ID,它们与我在这里发布的字符串相匹配!
我告诉一个 friend ,他说这是一个存在多年的 BLE 错误(关于自定义 CBUUID),这是真的吗?真的没有解决办法吗?
编辑添加完整的扫描代码仅供引用:
func centralManagerDidUpdateState(_ central: CBCentralManager) {
var statusMessage = ""
switch (central.state)
{
case .unsupported:
statusMessage = "Bluetooth Low Energy is not supported!"
displayStatusAlert(localmsg: statusMessage)
print(statusMessage)
case .unauthorized:
statusMessage = "Bluetooth Low Energy is not authorized!"
displayStatusAlert(localmsg: statusMessage)
print(statusMessage)
case .unknown:
statusMessage = "Bluetooth Low Energy is unknown!"
displayStatusAlert(localmsg: statusMessage)
print(statusMessage)
case .poweredOff:
statusMessage = "Bluetooth Low Energy is powered off!"
displayStatusAlert(localmsg: statusMessage)
print(statusMessage)
case .resetting:
statusMessage = "Bluetooth Low Energy is resetting!"
displayStatusAlert(localmsg: statusMessage)
print(statusMessage)
case .poweredOn:
statusMessage = "BLE is ready!" //If BLE is ready then start scanning right away!
peripheralsFoundNames.removeAll()
peripheralsFoundCB.removeAll()
peripheralsFoundRSSIs.removeAll()
peripheralsFoundData.removeAll() //Remove previous data from previous scans
central.scanForPeripherals(withServices: nil, options: nil)
}
}
//What happens when you discover a peripheral
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
//What to do when it discovers a peripheral, add it to the array list
print("Peripheral found: " + (peripheral.name ?? "Unknown Name"))
peripheralsFoundNames.append((peripheral.name ?? "Unknown Name"))
peripheralsFoundData.append((advertisementData.description ))
peripheralsFoundCB.append(peripheral)
peripheralsFoundRSSIs.append(RSSI)
}
最佳答案
我也有这个问题,但我找到了一些棘手的方法。
您可以尝试直接在 didDiscoverPeripheral func 中匹配 UUID(字符串值):
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber){
if let services = peripheral.services {
for service in services {
if service.uuid.uuidString.lowercased() == "4D494B45-414c-5741-5953-524F434B5321".lowercased() {
if !peripheralsFoundCB.contains(peripheral) {
peripheralsFoundCB.append(peripheral)
}
}
}
}
}
如果这种方式对你不起作用,我还有一个:
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber){
peripheral.discoverServices(nil)
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if let services = peripheral.services {
for service in services {
if service.uuid.uuidString.lowercased() == "4D494B45-414c-5741-5953-524F434B5321".lowercased() {
if !peripheralsFoundCB.contains(peripheral) {
peripheralsFoundCB.append(periphera
}
}
}
}
}
当然你应该使用
关于swift - 无法搜索自定义 CBUUID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46414347/