Swift蓝牙开发

作者: 江水东流 | 来源:发表于2017-03-15 14:54 被阅读264次

    github demo链接

    新项目中要用蓝牙开发,把漆面仪测量到的数据通过蓝牙传给app,一般需要设备提供方提供开发用的UUID参数,但是对方也不知道参数,最后发现用如果你不知道外设UUID 用 lightBlue 这个app查看.

    D29F0E70A3A6AE01925F76E42C20E7AB.png

    蓝牙项目用一个服务UUID连接设备,用特征UUID接受外设传来的数据,初始化中心设备后等状态成poweredOn后再扫描,否则不执行代理方法.

    蓝牙开发流程

    • 定义 中心设备(CBCentralManager) 外设(CBPeripheral)
    • 中心设备设置代理
    • centralManagerDidUpdateState 这个方法里面扫描设备
    • 扫码到设备后选出指定UUID或者名字的外设,然后连接特征.
    • 连接到特征后外设设置接受通知
    • 接受数据(Data类型)

    import UIKit
    import CoreBluetooth
    //服务
    let service1 = "00A9B803-EF34-4311-8EFE-F3D25901878E"
    //读
    let service3 = "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
    class ViewController: UIViewController,CBCentralManagerDelegate,CBPeripheralDelegate {
    //中心设备
    lazy var magager: CBCentralManager = CBCentralManager()
    //外设列表
    var connectPeripheral : CBPeripheral?

    override func viewDidLoad() {
        super.viewDidLoad()
        magager.delegate = self
    }
    
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        
        if (central.state == .poweredOn){
            print("打开蓝牙了")
            //必须状态ok后才可以扫描 要不不执行代理方法
            magager.scanForPeripherals(withServices: nil, options: nil)
        }
    }
    
    //外设更新名字
    func peripheralDidUpdateName(_ peripheral: CBPeripheral) {
        
    }
    
    //扫码到设备后回调方法
    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)
    {
        print("发现外设")
        print("名字 \(peripheral.name)")
        print("Rssi: \(RSSI)")
        print("外设UUID-->",peripheral.identifier.uuidString,"service-->",peripheral.services)
        
        if  (peripheral.name?.contains("QNix"))! != false
        {
            self.magager.stopScan()
            connectPeripheral = peripheral
            self.magager.connect(peripheral, options: nil)
        }
    
    }
    
    //外设链接成功
    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        connectPeripheral?.delegate = self
        
        var array : [CBUUID]?
        let uuid3 = CBUUID(string: service3)
        let uuid4 = CBUUID(string: service4)
        array?.append(uuid3)
        array?.append(uuid4)
        //发现设备服务
        connectPeripheral?.discoverServices(array)
    }
    
    //连接外设失败
    func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
        print("连接外设失败")
    }
    
    //发现服务
    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        
        print("发现服务UUID-->",peripheral.identifier.uuidString)
    
        //连接服务
        for service in peripheral.services! {
            connectPeripheral?.discoverCharacteristics(nil, for: service)
        }
    }
    
    //发现特征
    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
        //弹框
        
        for characteristic in service.characteristics! {
            print("发现特征UUID---> ",String(describing:characteristic.uuid))
            //接受通知
            if characteristic.uuid.uuidString ==  service4 {
                self.connectPeripheral?.setNotifyValue(true, for: characteristic)
            }
        }
        
    }
    
    //接受数据
    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
        
        let backToString = String(data: characteristic.value!, encoding: String.Encoding.utf8) as String!
        print("backToString",backToString ?? "",characteristic.uuid)
    }

    相关文章

      网友评论

        本文标题:Swift蓝牙开发

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