iOS 蓝牙基本使用

蓝牙使用的基本流程

1.创建中心设备
2.利用中心设备扫描外部设备
3、模拟点击, 然后连接指定的外设
4、连接外设成功调用,扫描外设中得服务
5、从需要的服务中查找需要的特征, 从peripheral中得service中扫描特征
6、遍历特征, 拿到需要的特征处理
7、对特征进行读写

蓝牙协议的常用方法

4,CBCentralManager *cm = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    cm.delegate = self;
 //scan外设
        NSDictionary* scanOptions = [NSDictionary dictionaryWithObject:[NSNumbernumberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
        [cm scanForPeripheralsWithServices:nil options:scanOptions];

- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI//scan到设备就会调用此方法

//connect设备
NSDictionary* scanOptions = [NSDictionary dictionaryWithObject:[NSNumbernumberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
        [cm connectPeripheral:(CBPeripheral *)[_peripheralArrayobjectAtIndex:indexPath.row] options:scanOptions];

- (void) centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error//连接外设时调用

- (void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral*)peripheral//当连接上一个外设  CBCentralManager 代理 处理此方法

4,//查询设备服务特征,注意 这块的peripheral 一定得为连接上的设备 后续发送接收数据有用
[peripheral setDelegate:self];
[peripheral discoverServices:nil];
#pragma mark -- CBPeripheralDelegate
//返回的蓝牙服务通知通过代理实现[_peripheral discoverServices:nil];

- (void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
//查询服务所带的特征值[_peripheral discoverCharacteristics:nil forService:myService];
- (void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

5,
//发送数据
[peripheral writeValue:datastr forCharacteristic:writeCharacteristictype:CBCharacteristicWriteWithResponse];
//发送成功调用
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
//处理蓝牙发过来的数据
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

实例.m文件

#import "ViewController.h"
#import 

@interface ViewController ()
/**
 *  外设
 */
@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

你可能感兴趣的:(iOS 蓝牙基本使用)