【问题标题】:CoreBluetooth Advertising but cannot see from other deviceCoreBluetooth 广告,但无法从其他设备看到
【发布时间】:2017-01-06 11:50:02
【问题描述】:

我试图将 iOS iPhone 设置为蓝牙外围设备,以便我可以从另一个 iDevice 看到它并发送信息。

我想我是在为自己做广告,但我在其他 iPhone、iPad 或 Mac 上都看不到它。控制台输出表明它是广告?我是否遗漏了一些明显的东西,(我环顾四周,用谷歌搜索过这里)但没有运气。

控制台:

self.peripheralManager powered on.
Service Added
peripheralManagerDidStartAdvertising(_:error:)
Advertising

代码:

import UIKit
import CoreBluetooth
class ViewController: UIViewController,CBPeripheralManagerDelegate {
    var peripheralManager:CBPeripheralManager!
    var transferCharacteristic: CBMutableCharacteristic?
    let MyP_Service_UUID = CBUUID(string: "1B981746-2064-4F68-BBB8-69A185314FC6")
    let MyP_Characteristics_UUID = CBUUID(string: "4271CEC1-652D-489B-8484-7C3550C6075E")
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        // Don't keep it going while we're not showing.
        peripheralManager.stopAdvertising()
    }
    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
        if (peripheral.state != .poweredOn) {
            return
        }
        print("self.peripheralManager powered on.")
        // ... so build our service.
        let myCharacteristic = CBMutableCharacteristic(type: MyP_Characteristics_UUID, properties: [.notify], value: nil, permissions: .readable)
        let myService = CBMutableService(type: MyP_Service_UUID, primary: true)
        myService.characteristics?.append(myCharacteristic)
        peripheralManager.add(myService)
        peripheralManager.startAdvertising([ CBAdvertisementDataServiceUUIDsKey: [MyP_Service_UUID], CBAdvertisementDataLocalNameKey : "MyP"])
    }

    func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
        print(error ?? "Service Added")
    }
    func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
        print(#function)
        print(error ?? "Advertising")
    }
    func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
        print(#function)
    }
    func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didUnsubscribeFrom characteristic: CBCharacteristic) {
        print(#function)
    }
    func peripheralManagerIsReady(toUpdateSubscribers peripheral: CBPeripheralManager) {
        print(#function)
    }

}

【问题讨论】:

  • 您如何尝试查看广告设备?您使用的是自己的代码还是 LightBlue 等应用程序
  • 我想我现在明白了。我需要另一个设备来监听我的广告,因为它是 BLE。所以它不会出现在标准蓝牙设备中。也许我应该删除这个问题!

标签: ios bluetooth swift3 core-bluetooth


【解决方案1】:

为了获得更好的支持,我们需要在 BLE Central 端查看您的代码。您是否在扫描设备上扫描相同的服务 UUID?

顺便说一句,根据您的用例,您可能需要检查其他可以为您处理所有蓝牙交互的 SDK。 例如:MultipeerConnectivity、p2pkit、google 附近。

免责声明: 我在开发 p2pkit 的团队工作

【讨论】:

    【解决方案2】:

    我太新手了,不了解 BLE 的工作原理。我现在看到我需要宣传正确的服务等,然后其他设备将订阅。

    我最初预计会在我的 iPhone 的蓝牙配对屏幕上看到它。

    谢谢

    【讨论】:

      【解决方案3】:

      您无法在设备蓝牙列表中看到您自己的广告设备。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-27
        • 1970-01-01
        • 2019-12-21
        • 1970-01-01
        • 1970-01-01
        • 2017-03-07
        • 2013-11-28
        • 2021-02-27
        相关资源
        最近更新 更多