iOS蓝牙开发入门06--demo的外设代码释义

BLEPeripheralViewController.swift说明

参考资料

CBPeripheralManager的学习,http://blog.csdn.net/dwarven/article/details/37873663

CBMutableCharacteristic学习笔记,http://blog.csdn.net/dwarven/article/details/37873707

CBMutableService学习笔记,http://blog.csdn.net/dwarven/article/details/37873731


0)UUID,是蓝牙服务和特征的通用唯一识别码(Universally Unique Identifier ),有些是规范好的UUID不需要人为设置,这里是人为配置的UUID,为128-bits,标准格式是8-4-4-4-12。如下设置服务UUID是transferServiceUUID, 特征的UUID是transferCharacteristicUUID,需要的数据就在这个服务特征上。

<span style="font-size:14px;"><span style="font-size:14px;">import UIKit
import CoreBluetooth

let TRANSFER_SERVICE_UUID = "E20A39F4-73F5-4BC4-A12F-17D1AD666661"
let TRANSFER_CHARACTERISTIC_UUID = "08590F7E-DB05-467E-8757-72F6F66666D4"
let NOTIFY_MTU = 20

let transferServiceUUID = CBUUID(string: TRANSFER_SERVICE_UUID)
let transferCharacteristicUUID = CBUUID(string: TRANSFER_CHARACTERISTIC_UUID)</span></span>

1)第一步,建立外设管理器CBPeripheralManager,delegate代理的是当前类BLEPeripheralViewController,queue为nil表示在主线程中使用

<span style="font-size:14px;"><span style="font-size:14px;">    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // Start up the CBPeripheralManager
        //第一步,建立外设管理器
        peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
    }</span></span>
2) 第二步,把特征transferCharacteristic添加到服务transferService,再把服务transferService添加到外设。

CBMutableCharacteristic,UUID是transferCharacteristicUUID, 特征的值为nil,标识这个特征的是属性是Notify通知,权限是Readable可读的;

CBMutableService, UUID是transferServiceUUID,服务类型是主要类型(primary = true)

<span style="font-size:14px;"><span style="font-size:14px;">func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {
        // Opt out from any other state
        //第二步,外设添加服务service
        if (peripheral.state != CBPeripheralManagerState.PoweredOn) {
            return
        }
        
        // We're in CBPeripheralManagerStatePoweredOn state...
        println("self.peripheralManager powered on.")
        
        // ... so build our service.
        
        // Start with the CBMutableCharacteristic
        transferCharacteristic = CBMutableCharacteristic(
            type: transferCharacteristicUUID,
            properties: CBCharacteristicProperties.Notify,
            value: nil,
            permissions: CBAttributePermissions.Readable
        )
        
        // Then the service
        let transferService = CBMutableService(
            type: transferServiceUUID,
            primary: true
        )
        
        // Add the characteristic to the service
        transferService.characteristics = [transferCharacteristic!]
        
        // And add it to the peripheral manager
        peripheralManager!.addService(transferService)
    }
</span>

3)第三步,当central订阅(subscribe)外设的特征时,开始给central发送数据。下图是mac上用LightBlue软件,与iPhone蓝牙通信,四栏依次是外设Peripheral,服务Services,特征Characteristics,详细资料Details,在Details栏下有一Subscribe按钮,按下表示central订阅外设的该服务的该特征。



<span style="font-size:14px;">    /** Catch when someone subscribes to our characteristic, then start sending them data
    */
    func peripheralManager(peripheral: CBPeripheralManager!, central: CBCentral!, didSubscribeToCharacteristic characteristic: CBCharacteristic!) {
        println("Central subscribed to characteristic")
        
        // Get the data
        dataToSend = textView.text.dataUsingEncoding(NSUTF8StringEncoding)
        
        // Reset the index
        sendDataIndex = 0;
        
        // Start sending
        sendData()
    }</span>
4)第四步,发送数据sendData()

EOM, End of Message信息结束,文本框里的内容按每20个字节(一个英文字母、一个空格或一个数字占一个字节)依次发送。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值