美文网首页iOS Developer
iOS蓝牙扫描总结

iOS蓝牙扫描总结

作者: jackyshan | 来源:发表于2017-03-14 11:05 被阅读2045次
image

蓝牙低能耗(BLE)技术是低成本、短距离、可互操作的鲁棒性无线技术,工作在免许可的2.4GHz ISM射频频段。它从一开始就设计为超低功耗(ULP)无线技术。它利用许多智能手段最大限度地降低功耗。蓝牙低能耗技术采用可变连接时间间隔,这个间隔根据具体应用可以设置为几毫秒到几秒不等。另外,因为BLE技术采用非常快速的连接方式,因此平时可以处于“非连接”状态(节省能源),此时链路两端相互间只是知晓对方,只有在必要时才开启链路,然后在尽可能短的时间内关闭链路。

1、初始化CBCentralManager

func start() {
      stop()
      centerManager = CBCentralManager(delegate: self, queue: dispatch_get_main_queue())
}

2、实现CBCentralManagerDelegate代理方法

检测到蓝牙打开,开始扫描设备

// MARK: CBCentralManagerDelegate
func centralManagerDidUpdateState(central: CBCentralManager) {
    if central.state != .PoweredOn {
        switch central.state {
        case .Unknown:
            print("蓝牙未知原因")
            break
        case .Resetting:
            print("蓝牙连接超时")
            break
        case .Unsupported:
            print("不支持蓝牙4.0")
            break
        case .Unauthorized:
            print("连接失败")
            break
        case .PoweredOff:
            print("蓝牙未开启")
            break
        
        default:
            break
        }
        
        print(">>>设备不支持BLE或者未打开")
        bleOpenFail()
        stop()
    }
    else {
        print(">>>BLE状态正常")
        bleOpenSucc()
        centerManager!.scanForPeripheralsWithServices([CBUUID(string: "60000-B5F3-F3F3-F0A9-E50F24DCCF9E")], options: [CBCentralManagerScanOptionAllowDuplicatesKey:false])
//            centerManager!.scanForPeripheralsWithServices(nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey:false])
      }
  }

扫描到设备,开始连接设备

  func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)  {
    print(">>>>扫描周边设备 .. 设备id:\(peripheral.identifier.UUIDString), rssi: \(RSSI.stringValue), advertisementData: \(advertisementData), peripheral: \(peripheral)")
  peripheral.delegate = self
  centerManager?.connectPeripheral(peripheral, options: nil)
}

3、实现CBPeripheralDelegate代理方法

连接到设备,开始扫描设备上的服务

func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
    print("和周边设备连接成功。")
    peripheral.discoverServices(nil)
    print("扫描周边设备上的服务..")
}

发现设备上的服务,开始扫描服务携带的特性

func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
    if error != nil {
        print("发现服务时发生错误: \(error)")
        return
    }
    
    print("发现服务 ..")
    
    for service in peripheral.services! {
        peripheral.discoverCharacteristics(nil, forService: service)
    }
}

发现设备特性,所以信息获得完毕

func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
    print("发现服务 \(service.UUID), 特性数: \(service.characteristics?.count)")
    
    for c in service.characteristics! {
        if let data = c.value {
            peripheral.readValueForCharacteristic(c)
            print("特性值byte: \(data.bytes)")
            print("特性值string: \(String(data: data, encoding: NSUTF8StringEncoding))")
        }
    }
}

4、停止蓝牙扫描

func stop() {
    bleViewerPerArr.removeAll()
    centerManager?.stopScan()
    centerManager = nil
}

代码参考
https://github.com/jackyshan/bleIbeaconscanner/blob/master/BLEScanner.swift

相关文章

  • iOS蓝牙扫描总结

    蓝牙低能耗(BLE)技术是低成本、短距离、可互操作的鲁棒性无线技术,工作在免许可的2.4GHz ISM射频频段。它...

  • iOS蓝牙开发

    iOS蓝牙开发是围绕着CoreBluetooth框架实现的 1.iOS开发的关键词 中心设备:用于扫描周围的蓝牙硬...

  • 蓝牙问题集锦

    扫描蓝牙 手机锁屏状态下,后台服务无法扫描蓝牙设备频繁扫描,导致扫描蓝牙失败定位服务关闭时,后台无法扫描蓝牙

  • iOS 与 BLE “HCI_EIR_datatype_t” 的

    对于iOS来说,蓝牙设备不能像 Android 那样,直接通过扫描adv就能拿到设备的mac地址,所以蓝牙设备就需...

  • Android:蓝牙总结

    总结蓝牙相关的东西 一.蓝牙ble扫描有超时时间吗? 函数startLeScan不会主动超时停止,要自己调用sto...

  • Android 扫描蓝牙设备并获取设备类型

    目前流行的蓝牙4.0标准包括传统的蓝牙(BT)和低功耗蓝牙模块(BLE)。 这里扫描的是传统蓝牙模块,先给出扫描结...

  • iOS 蓝牙 5.0 后台扫描

    后台长久任务: 打开后台模式中的使用蓝牙功能(手机为中心模式):TARGET→Capabilities→Backg...

  • iOS蓝牙通讯开发

    刚开发完一款APP,其中涉及到应用和硬件进行蓝牙通讯需求,记录分享总结.场景:APP扫描制定的蓝牙设备,连接设备,...

  • iOS蓝牙之扫描、链接、读写数据(二)

    接上篇iOS蓝牙之扫描、链接、读写数据(一) 一、关于蓝牙从连接到读发数据 在这篇文章中,我会按照上篇文章中介绍的...

  • iOS蓝牙开发(四):BabyBluetooth蓝牙库介绍

    iOS蓝牙开发(四):BabyBluetooth蓝牙库介绍 iOS蓝牙开发(四):BabyBluetooth蓝牙库介绍

网友评论

    本文标题:iOS蓝牙扫描总结

    本文链接:https://www.haomeiwen.com/subject/rlvcnttx.html