【问题标题】:Swift: Why can't my iOS scanned other bluetooth devicesSwift:为什么我的 iOS 不能扫描其他蓝牙设备
【发布时间】:2015-12-08 01:33:47
【问题描述】:

现在我正在做一个应用程序项目,需要我的 iPhone 扫描附近的其他蓝牙设备并列出它们。我想知道我的代码有什么问题吗?

代码:

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate {

    var manager: CBCentralManager!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        manager = CBCentralManager (delegate: self, queue: nil)
    }


    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {

            print("Peripheral: \(peripheral)")
    }

    func centralManagerDidUpdateState(central: CBCentralManager) {
        print("Checking")
        switch(central.state)
        {
        case.Unsupported:
            print("BLE is not supported")
        case.Unauthorized:
            print("BLE is unauthorized")
        case.Unknown:
            print("BLE is Unknown")
        case.Resetting:
            print("BLE is Resetting")
        case.PoweredOff:
            print("BLE service is powered off")
        case.PoweredOn:
            print("BLE service is powered on")
            print("Start Scanning")
            manager.scanForPeripheralsWithServices(nil, options: nil)
        default:
            print("default state")
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }  
}

我使用的是 iPhone 5 (iOS 9),我确定我的蓝牙已打开。

当我在 iPhone 中运行应用程序时,控制台只记录以下输出:

Checking
BLE service is powered on
Start Scanning

但是输出中没有显示蓝牙设备的名称。即使我打开我的 iPad (iPad Mini 4 iOS 8),列表仍然不会更新。

有时它会扫描我的 MacBook Pro 蓝牙并且输出会是这样的:

Peripheral: <CBPeripheral: 0x14d70e00, identifier = 54738076-6C97-FD04-18CF-5E1AF6705865, name = vivien’s MacBook Pro, state = disconnected>

那么,为什么会这样呢?谁能给我解释一下?

【问题讨论】:

  • 如果 ipad 上没有运行作为 BLE 外围设备的应用程序,那么您的应用程序将找不到任何东西。您可以使用 LightBlue 应用宣传心率监测器等外围设备
  • @Paulw11 正如你所说,当我在 LightBlue 应用程序中添加虚拟外围设备时,我的 iPhone 能够扫描我的 iPad。如果删除虚拟外围设备,则无法再次扫描我的 iPad。我以为所有的手机、平板电脑都是外围设备,不是吗?那么,要启用扫描其他 iPhone 和 iPad,我需要在后台运行 LightBlue 应用程序吗?
  • 为了省电iOS只在需要的时候做广告;即当应用程序正在宣传外围设备时
  • @Paulw11 所以无论如何我仍然需要在后台运行 LightBlue 应用程序,对吧?还是有其他办法?
  • 嗯,您需要在后台添加一些应用广告,才能让您的设备被发现

标签: ios iphone swift ipad bluetooth


【解决方案1】:

案例1:
您必须使用 GKSession 来扫描和连接其他 iOS 设备,而不是 CoreBluetooth。
案例2:
您的蓝牙设备是蓝牙 3.0 配件。您的 iPhone 可以在设置->蓝牙中发现并显示它。
但此消息不会传递到您的应用,因此您的应用不会发现它。
请使用蓝牙 4.0 配件重试。

【讨论】:

  • 但即使在我的 iPhone 设置>蓝牙发现列表中,我的 iPad mini 和其他蓝牙 4.0 设备(如 Nexus 5)也没有出现。所以现在的问题是,我只想扫描并列出所有其他蓝牙设备(不配对)。这可以使用您提到的 GKSession 来完成吗?
  • @vivien 你可以在app Store下载LightBlue App。它是一个很好的CoreBluetooth调试工具。当您尝试连接两个 iOS 设备而不是所有蓝牙设备时,GKSession 很有用。
  • 我已经下载了。仍然只有我的 MacBook 出现在“周边周边”列表中。一个设备如何算作外围设备?它是否与外围设备有关,因此我的应用程序的扫描列表仅显示我的 Macbook 而不是附近的其他蓝牙设备?
  • case1:你的蓝牙设备坏了。case2:你的蓝牙设备已经连接到另一个设备,所以它不会广播,你也无法发现。case 3:你的蓝牙不将状态更改为可发现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多