ios - CBPeripheral 对象的名称始终为 nil 值并且处于断开状态

标签 ios swift xcode bluetooth core-bluetooth

我正在编写一个应用程序,允许我通过 BLE 将图像发送到其他人的应用程序。

为了正确测试它,我需要能够连接到设备,但我扫描的外围设备总是断开连接,并且值为零。我不确定我这样做是否正确。我一直在阅读指南,并且遵循正确的程序,所以有人可以向我指出我可能做错了什么吗?我正在检测哪些外围设备?

输出:

<CBPeripheral: 0x165b9c90, identifier = 6B74A074-6F5B-0E3A-94EB-6E7BB890569C, name = (null), state = disconnected>
<CBPeripheral: 0x165a0940, identifier = A35AC32E-5668-BD0D-4DBC-D4BF959B9242, name = (null), state = disconnected>
<CBPeripheral: 0x166a8220, identifier = 4D9FA1A1-0090-465F-B53D-363B0F3BBD27, name = (null), state = disconnected>

编辑:添加更多代码

这是我的代码

import Foundation
import CoreBluetooth
import UIKit

class BluetoothController: UITableViewController, CBCentralManagerDelegate, CBPeripheralManagerDelegate,  CBPeripheralDelegate {

    var centralManager: CBCentralManager!
    var peripheralManager: CBPeripheralManager!
    var service : CBMutableService!

    let uuid:CBUUID = CBUUID(string: "09d921ff-b80c-47b1-bc2b-5bbaadf62010")
    let charaUuid:CBUUID = CBUUID(string: "09d921ff-b80c-47b1-bc2b-5bbaadf62021")

    override func viewDidLoad() {
        print("Initialzing managers")
        peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
        centralManager = CBCentralManager(delegate: self, queue: nil)
        service = CBMutableService(type: uuid, primary: true) //<--Probably not causing it but without a service...

        let properties: CBCharacteristicProperties = [.notify, .read, .write]
        let permissions: CBAttributePermissions = [.readable, .writeable]
        let characteristic = CBMutableCharacteristic(
            type: charaUuid,
            properties: properties,
            value: nil,
            permissions: permissions)

        service.characteristics = [characteristic];
        peripheralManager.add(service)
    }

    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
        print("peripheral state updated")
        print("\(peripheral.description)")
    }

    func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?)
    {
        print("started advertising")
    }

    // Check if the bluetooth is powered on
    // Start looking for devices
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if central.state == .poweredOn {
            print("Scanning for devices")
            centralManager.scanForPeripherals(withServices: nil, options: nil)
            startAdvert()
        }  else {
            print("Bluetooth not available.")
        }
    }

    func startAdvert(){
        let advertisingData = [CBAdvertisementDataLocalNameKey:"Test Device", CBAdvertisementDataServiceUUIDsKey: uuid] as [String : Any]
        peripheralManager.startAdvertising(advertisingData)
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber){

        print(peripheral)
        //didReadPeripheral(peripheral, rssi: RSSI)
    }

}

最佳答案

正如 Paulw11 所指出的,您未连接是因为您从未调用 connect()。您会发现周围有一些与您的应用无关的随机 BLE 设备。这是因为您正在扫描所有可能的服务:

centralManager.scanForPeripherals(withServices: nil, options: nil)

你几乎不应该这样做。这对电池不利,而且很少是您想要的。您想要扫描您的服务。

centralManager.scanForPeripherals(withServices: [uuid], options: nil)

然后,当您发现其他设备宣传您的服务时,您需要连接到它们。

<小时/>

对于@WholeCheese 的评论,问题的细节与零名称并不真正相关,但为了解决标题中的问题,如果设备不通过广告本地名称,您将不会看到广告中的名称。 BLE。这很常见。广告数据包中没有太多数据空间(大约 30 字节),并且本地名称可能相当大。

如果您正在构建扫描应用程序并想要显示这些名称,则需要连接到设备。此时,核心蓝牙将读取 GAP 名称(而不是公布的本地名称)。在大多数情况下,GAP 名称应该在那里(并非所有设备都 promise ,但通常应该在那里)。核心蓝牙会缓存大量信息,因此如果您以前连接过该设备,即使没有公布,peripheral.name 也可能会在未连接的情况下被填写。但如果没有,您就需要进行连接。

关于ios - CBPeripheral 对象的名称始终为 nil 值并且处于断开状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46687694/

相关文章:

ios - 从通知中心删除观察者的最佳位置在哪里

xcode - 从xcode部署资源文件

iOS9 Xcode 7 - 核心数据 - 避免重复对象

IOS 切换 View 和 Storyboard不起作用

ios - 如何通过 UIDocumentInteractionController 共享文本字符串

ios - Xamarin:UICollection 图像重新排序问题

swift - 从以前的 VC 转到 ViewController

iOS:当单元格背景更改时,UITableViewCell 的侧面显示不同的颜色(Xcode 8、Swift 3)

ios - 如何设置和传递也可以在另一个 View Objective-C 中访问的变量的值

ios - 如何在 Swift 中使用时区格式化日期?