2016-08-07 197 views
2

首先,我知道这个问题已被问了很多关于SO。我经历了所有这些尝试解决它,但无济于事。蓝牙不发现设备

我有一个简单的应用程序,扫描蓝牙设备。这是我的代码。

import CoreBluetooth 
import UIKit 

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate { 

    var manager: CBCentralManager! 
    var peripheral: CBPeripheral! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     manager = CBCentralManager(delegate: self, queue: nil) 
    } 

    // MARK: - CBCentralManagerDelegate 
    func centralManagerDidUpdateState(central: CBCentralManager) { 
     print(#function) 

     switch central.state { 
     case .Unsupported: 
      print("Unsupported") 
     case .Unauthorized: 
      print("Unauthorized") 
     case .PoweredOn: 
      print("Powered On") 
      central.scanForPeripheralsWithServices(nil, options: nil) 
     case .Resetting: 
      print("Resetting") 
     case .PoweredOff: 
      print("Powered Off") 
     case .Unknown: 
      print("Unknown") 
     } 
    } 

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

     print("Discovered \(peripheral.name) at \(RSSI)") 

     if peripheral.name!.containsString(name) { 
      manager.stopScan() 

      self.peripheral = peripheral 
      self.peripheral.delegate = self 

      manager.connectPeripheral(peripheral, options: nil) 
     } 
    } 

    func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) { 
     print(#function) 
    } 

    func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) { 
     central.scanForPeripheralsWithServices(nil, options: nil) 
    } 


    // MARK: - CBPeripheralDelegate 
    func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) { 
     print(#function) 

     guard let services = peripheral.services else { 
      return 
     } 

     for service in services { 
      print(service.UUID) 
      if service.UUID == serviceUUID { 
       peripheral.discoverCharacteristics(nil, forService: service) 
      } 
     } 
    } 

    func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) { 
     print(#function) 

     guard let characteristics = service.characteristics else { 
      return 
     } 

     for characteristic in characteristics { 
      print(characteristic.UUID) 
      if characteristic.UUID == scratchUUID { 
       peripheral.setNotifyValue(true, forCharacteristic: characteristic) 
      } 
     } 
    } 

    func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) { 
     print(#function) 

     var count: UInt32 = 0 
     if characteristic.UUID == scratchUUID { 
      characteristic.value!.getBytes(&count, length: sizeof(UInt32)) 
      print(String(format: "%llu", count)) 
     } 
    } 

} 

我在我的iPhone 5(iOS 9.3.4)中运行它。我有一台运行Windows的笔记本电脑,一部Android手机,一部iPad Air 2和我的Mac,所有的蓝牙都打开了。但是没有发现这些设备。 centralManagerDidUpdateState方法被调用并开始扫描,但就是这样。 didDiscoverPeripheral委托方法永远不会被调用。

我重复了这个过程,这次它在iPad Air 2中运行,但结果相同。

如果我去设备上的蓝牙菜单,我看到其他设备被发现。

在一些SO答案中,我看到它在主队列中运行会起作用。像这样,manager = CBCentralManager(delegate: self, queue: dispatch_get_main_queue())。但是这对我也不起作用。

我真的很感谢这方面的帮助。

+4

是否有任何广告BLE服务的设备?试试App Store中的LightBlue应用程序,看它是否可以发现任何东西 – Paulw11

+0

我认为默认是主队列。你可以尝试用'let queue = dispatch_queue_create(“meme”,DISPATCH_QUEUE_SERIAL);'创建你自己的队列。除此之外,您可以尝试在Mac上获取[LightBlue](https://itunes.apple.com/cn/app/lightblue/id639944780?mt=12)并查看它是否可以看到任何内容。该文件说,搜索没有指定服务标识符的外设是“不推荐”,但它应该仍然有效。 –

+0

我会+1 @ Paulw11。您的设备是否宣传BLE?这不是因为他们宣传BLE。换句话说,要找到一个设备,设备必须告诉它它在这里(做广告)。所以试试LightBlue。 – Larme

回答

1

您的其他设备可能不会广告BLE服务(如评论中提到的Paulw11)。您提到的所有设备都不会显示为可发现,除非它们位于“蓝牙设置”页面中。

有适用于iOS,Android和Windows的应用程序可让他们通过LE进行宣传。最容易使用的是LightBlue for iOS。在iPhone上运行您的应用程序,而其他iOS设备正在广告,您应该看到广告。在Windows或Android上获取LE广告扫描器来检查LightBlue是否正在广告可能很有用。

注意的Windows还不支持外设角色蓝牙LE, 所以即使你将能够看到广告就不会 能够连接到它。

+0

跟进同样的问题.... LightBlue如何做到这一点? 阅读文档时,我应该能够在开始扫描时看到所有内容都提供'nil'参数吗?... LightBlue如何看到外设,但看不到我的代码? –