ios中蓝牙开发

首先,苹果提供了4个框架用于蓝牙连接
GameKit.framework(方法简单) 不过在ios7之后就过时了
MultipeerConnectivity.framework 用于ios设备之间,用于文件共享(仅限于沙盒)
ExternalAccessory.framework 第三方蓝牙交互,必须经过苹果MFI认证
coreBluetoot.framework 第三方蓝牙交互,必须支持蓝牙4.0

在这里简单的说一下那个过期的方法的简单步骤,毕竟简单,让大家有个印象
1. 创建选择其他蓝牙设备的控制器
2. 成为该控制器的代理
3. 显示蓝牙控制器
4. 实现代理方法:1.保存会话,2. 接受,回调函数,3.关闭显示蓝牙控制器

讲道理,上面不明所以也没关系。下面才是重点
首先我们来了解几个常识
BLE的全拼是(Bluetooth Low Energy)

  1. 每个蓝牙4.0设备都是通过服务(Service)和特征(Characteristic)
    一个设备必然包含一个或多个服务,每个服务下面又包含若干个特诊
  2. 特征是与外界交互的最小单位
    通过每个不同的特征代表不同的信息
  3. 服务和特征是用UUID来唯一标识
  4. 服务与特征,由设备上提供

那么接着看,BLE用到类吧
Main objects ——CBCentralManager——CBPeripheral
Data objects——CBService——CBCharacteristic
Helper objects——CBUUID

说了那么多,相信口水都干了,来波代码压压惊

#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>

@interface ViewController ()<CBCentralManagerDelegate, CBPeripheralDelegate>
/**
 *  外设
 */
@property (nonatomic, strong) NSMutableArray *peripherals;
/**
 *  中心管理者
 */
@property (nonatomic, strong) CBCentralManager *mgr;
@end

@implementation ViewController

- (NSMutableArray *)peripherals
{
    if (!_peripherals) {
        _peripherals = [NSMutableArray array];
    }
    return _peripherals;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // 1.创建中心设备
    CBCentralManager *mgr = [[CBCentralManager alloc] init];
    self.mgr = mgr;


    // 设置代理
    mgr.delegate = self;

    // 2.利用中心设备扫描外部设备
    /*
     如果指定数组代表只扫描指定的设备
     */
    [mgr scanForPeripheralsWithServices:nil options:nil];
}
#pragma mark - CBCentralManagerDelegate
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{

    // 保存扫描到得外部设备
    // 判断如果数组中不包含当前扫描到得外部设置才保存
    if (![self.peripherals containsObject:peripheral]) {

        peripheral.delegate = self;
        [self.peripherals addObject:peripheral];
    }
}

/**
 *  模拟点击, 然后连接所有的外设
 */
- (void)start
{
    for (CBPeripheral *peripheral in self.peripherals) {
        /**
         *  连接外设
         */
        [self.mgr connectPeripheral:peripheral options:nil];
    }
}
/**
 *  连接外设成功调用
 */
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    // 扫描外设中得服务
    [peripheral discoverServices:nil];
}
/**
 *  连接外设失败调用
 */
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{

}

#pragma makr - CBPeripheralDelegate
/**
 *  只要扫描到服务就会调用
 *
 *  @param peripheral 服务所在的外设
 */
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{

    // 获取外设中所有扫描到得服务
    NSArray *services = peripheral.services;
    for (CBService *service in services) {
        // 拿到需要的服务
        if ([service.UUID.UUIDString isEqualToString:@"123"])
        {
            // 从需要的服务中查找需要的特征
            // 从peripheral中得service中扫描特征
            [peripheral discoverCharacteristics:nil forService:service];
        }
    }
}

/**
 *  只要扫描到特征就会调用
 *
 *  @param peripheral 特征所属的外设
 *  @param service    特征所属的服务
 */
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{

    // 拿到服务中所有的特诊
    NSArray *characteristics =  service.characteristics;
    // 遍历特征, 拿到需要的特征处理
    for (CBCharacteristic * characteristic in characteristics) {
        if ([characteristic.UUID.UUIDString isEqualToString:@"8888"]) {
            NSLog(@"设置闹钟");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值