IOS 蓝牙开发

写给蓝牙开发的初学者:
这个需求是前几天公司提出的,有个蓝牙设备需要连接自己的app,作为重来没有接触过蓝牙的我也是费了好些时间才摸索出来的. 首先来说下步骤.
1.app需要搜索到你蓝牙设备的信息.
2.判断搜索到的设备是否是你所需设备
3.根据该蓝牙设备信息读取,服务以及特征值(有好多的)
4.根据读取的特征以及服务判断哪个是你所需的特征(可以写入的).
5.进行交互,根据特征值写入数据,然后在回调中可以接收到蓝牙设备返回的信息.
然后步入正题:
在此之前需要介绍一个非常厉害的第三方库简单方便快捷就可以搞定这一系列的步骤.
< BabyBluetooth> 自己从github上下载
下载地址:https://github.com/coolnameismy/BabyBluetooth
简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮

//导入
#import 
#import "BabyBluetooth.h"

#pragma mark -初始化蓝牙
-(void)initBLE{
    //初始化BabyBluetooth 蓝牙库
    _baby = [BabyBluetooth shareBabyBluetooth];
    //设置蓝牙委托
    [self babyDelegate];
    //停止之前的连接
    [_baby cancelAllPeripheralsConnection];
    //设置委托后直接可以使用,无需等待CBCentralManagerStatePoweredOn状态。
    ////查找Peripherals
    _baby.scanForPeripherals().begin();
}
#pragma mark -蓝牙配置和操作
-(void)babyDelegate{
    
    __weak typeof(self)weakSelf = self;
    //设备状态改变的委托
    [_baby setBlockOnCentralManagerDidUpdateState:^(CBCentralManager *central) {
        if (central.state == CBCentralManagerStatePoweredOn) {
            NSLog(@"设备打开成功,开始扫描设备");
        }else{
           NSLog(@"请打开手机蓝牙");
        }
    }];
    //扫描到设备
    [_baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
        weakSelf.la.text=[NSString stringWithFormat:@"搜索到了设备:%@",peripheral.name];
        [SVProgressHUD showWithStatus:@"正在连接..."];
 //连接设备
weakSelf.baby.having(peripheral).and.channel(channelOnPeropheralView).then.connectToPeripherals().discoverServices().discoverCharacteristics().readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin();
    }];
    //设置设备连接成功的委托,同一个baby对象,使用不同的channel切换委托回调
    [_baby setBlockOnConnectedAtChannel:channelOnPeropheralView block:^(CBCentralManager *central, CBPeripheral *peripheral) {
        weakSelf.la.text=[NSString stringWithFormat:@"连接成功"];
        [SVProgressHUD showWithStatus:@"蓝牙连接成功"];
    }];
    //设置设备连接失败的委托
    [_baby setBlockOnFailToConnectAtChannel:channelOnPeropheralView block:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) {
         [SVProgressHUD dismiss];
         weakSelf.la.text=[NSString stringWithFormat:@"连接失败请重试!"];
    }];
    //设置设备断开连接的委托
    [_baby setBlockOnDisconnectAtChannel:channelOnPeropheralView block:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) {
        weakSelf.la.text=[NSString stringWithFormat:@"连接已断开请重试!"];
    }];
    [_baby setBlockOnReadValueForCharacteristicAtChannel:channelOnPeropheralView block:^(CBPeripheral *peripheral, CBCharacteristic *characteristic, NSError *error) {
           if([characteristic.UUID.UUIDString isEqualToString:@"00001524-1212-EFDE-1523-785FEABCD123"]){
               
                    [SVProgressHUD showWithStatus:@"正在写入数据..."];
                    if(![characteristic isNotifying]){
                        [peripheral setNotifyValue:YES forCharacteristic:characteristic];
                        NSLog(@"开始监听");
                    }
                    //要写入的数据(需要根据蓝牙协议来写)
                   //这里写数据可能会需要先求出校验和checkSUM
                   //比如:下面的字节协议要求最后一位是校验和(0x1a)至于怎么计算下一篇中我会贴出公式
                    Byte byte[] = {0x51,0x26,0x00,0x00,0x00,0x00,0xa3,0x1a};
                    NSData *data = [NSData dataWithBytes:byte length:sizeof(byte)];
                    //写入数据
                    [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
                    
                    [weakSelf.baby notify:peripheral characteristic:characteristic block:^(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error) {
                        [SVProgressHUD showWithStatus:@"正在读取数据..."];
                        Byte *testByte = (Byte *)[characteristics.value bytes];
                        if([characteristics.value length]>2){
                        [SVProgressHUD showWithStatus:@"读取数据成功!"];
                         //断开所有设备连接
                        [weakSelf.baby cancelAllPeripheralsConnection];

                        }
                    }];
        }
    }];
}

点此查看校验和计算
到此为止就完事了,如果本文有哪里不足和欠缺之处请私信我,或者留言.
欢迎各位前来切磋,共同进步~~

你可能感兴趣的:(IOS 蓝牙开发)