iOS开发之蓝牙

//

//  ViewController.m

//  13-蓝牙

//

//  Created by hongqiangli on 2017/7/21.

//  Copyright © 李洪强. All rights reserved.

//

//蓝牙4.0

 

iOS中提供了4个框架用于实现蓝牙连接

GameKit.framework(用法简单)

只能用于iOS设备之间的连接,多用于游戏(比如五子棋对战),从iOS7开始过期

 

MultipeerConnectivity.framework

只能用于iOS设备之间的连接,从iOS7开始引入,主要用于文件共享(仅限于沙盒的文件)

 

ExternalAccessory.framework

可用于第三方蓝牙设备交互,但是蓝牙设备必须经过苹果MFi认证(国内较少)

 

CoreBluetooth.framework(时下热门)

可用于第三方蓝牙设备交互,必须要支持蓝牙4.0

硬件至少是4s,系统至少是iOS6

蓝牙4.0以低功耗著称,一般也叫BLEBluetooth Low Energy

目前应用比较多的案例:运动手坏、嵌入式设备、智能家居

 

 

Core Bluetooth测试比较麻烦,正常情况下,得至少有2台真实的蓝牙4.0设备

 

如何让iOS模拟器也能测试蓝牙4.0程序?

买一个CSR蓝牙4.0 USB适配器,插在Mac

在终端输入sudo nvram bluetoothHostControllerSwitchBehavior="never"

重启Mac

Xcode 4.6调试代码,将程序跑在iOS 6.1的模拟器上

(苹果把iOS 7.0模拟器对BLE的支持移除掉了)

 

Core Bluetooth的使用场景

运动手环、智能家居、嵌入式设备等等(金融刷卡器、心电测量器)

 

每个蓝牙4.0设备都是通过服务(Service)和特征(Characteristic)来展示自己的

一个设备必然包含一个或多个服务,每个服务下面又包含若干个特征

 

特征是与外界交互的最小单位

比如说,一台蓝牙4.0设备,用特征A来描述自己的出厂信息,用特征B来收发数据

 

服务和特征都是用UUID来唯一标识的,通过UUID就能区别不同的服务和特征

 

设备里面各个服务(service)和特征(characteristic)的功能,均由蓝牙设备硬件厂商提供,比如哪些是用来交互(读写),哪些可获取模块信息(只读)

 

 

 

#import "ViewController.h"

#import <CoreBluetooth/CoreBluetooth.h>

@interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>

@property (nonatomic ,strong)CBCentralManager *manager;

@property (nonatomic ,strong)NSMutableArray *peripherals;

@end

 

@implementation ViewController

 

-(CBCentralManager *)manager{

    //1.创建中心设备

    if(!_manager){

        _manager = [[CBCentralManager alloc]init];

    }

    return _manager;

}

 

- (NSMutableArray *)peripherals{

    if(!_peripherals){

        _peripherals = [NSMutableArray array];

    }

    return _peripherals;

}

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    for(CBPeripheral *peripheral in self.peripherals){

        //连接外部设备

        [self.manager connectPeripheral:peripheral options:nil];

        peripheral.delegate = self;

    }

    

}

 

 

建立中心设备

 

扫描外设(Discover Peripheral

 

连接外设(Connect Peripheral)

 

扫描外设中的服务和特征(Discover Services And Characteristics)

 

利用特征与外设做数据交互(Explore And Interact)

 

断开连接(Disconnect)

- (void)viewDidLoad {

    [super viewDidLoad];

    //2 扫描外设

    //找到特定的服务或者特征来做事情 没有返回值

    //服务的UID  789

//    门类下的特征: 1000   2000

    [self.manager scanForPeripheralsWithServices:@[@"789",@"777"] options:nil];

    self.manager.delegate = self;

}

 

//扫描到外设之后执行的方法

 

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:

(NSNumber *)RSSI{

    if(![self.peripherals containsObject:peripheral]){

        //数组里面放的 是所有外设列表

        

        [self.peripherals addObject:peripheral];

    }

    

    

}

 

//连接成功之后就会调用

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)

peripheral{

    //peripheral

    //找名字叫789的服务

    

    [peripheral discoverServices:@[@"789",@"777"]];

    

    

    

}

#pragma mark

//扫描到服务的时候就会调用

//peripheral  外设

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{

    for(CBService *service in peripheral.services){

        //发现之后扫描特征

        [peripheral discoverCharacteristics:@[@"1000,2000"] forService:service];

    }

    

    

    

}

 

//发现之后就会调用

//扫描到服务中的特征的时候,就会调用

//peripheral    外设

//service   服务

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(nonnull CBService *)service error:(nullable NSError *)error{

    for(CBCharacteristic *characteristic in service.characteristics){

        if([characteristic.UUID.UUIDString isEqualToString:@"1000"]){

            NSLog(@"进行交互返回心率");

        }

        if([characteristic.UUID.UUIDString isEqualToString:@"2000"]){

            NSLog(@"返回出场信息");

        }

    }

    

}

//更新状态改变的时候

- (void)centralManagerDidUpdateState:(CBCentralManager *)central{

    NSLog(@"更新状态");

    

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

绝大多数智能手机支持蓝牙 4.0BLE

 

蓝牙芯片发展迅速,在性能和效率方面都有很大提高,且不断变得更小更便宜

 

iBeacon + 蓝牙,前景一片光明

应用之一:室内导航

Estimote公司为iBeacon提供基站

3iBeacon基站的预购价格为99美元(约合人民币610元)

Estimote公司推出的iBeacon基站的最远传输距离为50m,但是他们推荐在10m范围内的使用效果最好

 

一块纽扣电池就能为一个iBeacon基站提供长达 2 年的使用寿命,而且是在设备不断对外发射信号的情况下

@end

posted @ 2017-07-21 15:12  李洪强  阅读(286)  评论(0编辑  收藏  举报