iOS之蓝牙开发—CoreBluetooth详解

CoreBluetooth的API是基于BLE4.0的标准的。这个框架涵盖了BLE标准的所有细节。仅仅只有新的iOS设备和Mac是和BLE标准兼容.在CoreBluetooth框架中,有两个主要的角色:周边和中央(Peripheral and Central) ,整个框架都是围绕这两个主要角色设计的,他俩之间有一系列的回调交换数据。下图1展示了周边和中央(Peripheral and Central),还有他俩之间的关系。

开发蓝牙这块,有两种模式,一个是中心模式,还有一个是外设模式。这里主要讲的是中心模式,中心模式的流程主要分为以下几步:1、建立中心设备 2、扫描外部设备 3、连接外部设备 4、扫描外部设备中的服务和特征 5、利用相关的特征与外部设备收发数据。进行讲解之前,我们得知道一个中心设备可以连接多个外部设备,一个外部设备包含一个或多个服务,一个服务包含一个或多个特征。

下面根据我初学蓝牙的角度来讲解一下:

    1、首先是导入框架#import,

          然后创建中心设备并设置代理(不要忘记签订协议):

   CBCentralManager *manager = [[CBCentralManageralloc]init];

   self.manager = manager;

  一旦设置代理在运行程序的时候,就会调用协议里一个必须要完成的方法:

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

  这个方法是查看中心设备是否打开蓝牙。

2、利用中心设备扫描外部设备:

  [manager scanForPeripheralsWithServices:niloptions:nil];

  第一个参数那里表示扫描带有相关服务的外部设备,例如填写@[[CBUUIDUUIDWithString:@"需要连接的外部设备的服务的UUID"]],即表示带有需要连接的外部设备的服务的UUID的外部设备,nil表示扫描全部设备;

  options处以后细讲,暂时可以写一个@{CBCentralManagerScanOptionAllowDuplicatesKey :@YES}这样的参数,YES表示会让中心设备不断地监听外部设备的消息,NO就是不能。

  一旦扫描到外部设备,就会进入协议中的

   - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI;

  在这个方法里,我们可以根据我们获取到的硬件的某些条件进行筛选,然后连接我们需要连接的外部设备,例如连接名字带有A的外部设备:

   if ([peripheral.namehasPrefix:@"A"] ) {

       //连接设备

       [manager connectPeripheral:peripheraloptions:nil];

   }

3、刚刚的if判断中的语句就是在进行中心设备和外部设备的连接。连接成功或者失败会分别进入

    - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;(连接成功)

    - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullableNSError *)error;(连接失败)

4、我们在连接成功的方法中开始扫描外部设备的服务:

    [peripheral discoverServices:nil];

   接着就会跳入发现服务的代理方法中去:

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

   我们在这个方法里面开始扫描服务中的特征:

    [peripheral discoverCharacteristics:nilforService:service];

   当我们扫描到特征的时候,就会跳入发现特征的协议方法里去:

    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error;

5、扫描到特征之后,我们就可以拿到相应的特征进行读写操作。

   例如进行读取数据的操作:

    if ([characteristics.UUID.UUIDStringisEqualToString:@"你需要的特征的UUID"]){

         //读取特征数据

         [peripheral readValueForCharacteristic:characteristics];

    }

   这就读取了特征包含的相关信息,只要读取就会进入另外一个方法:

    - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError *)error;

   在这个方法里,我们就可以拿到我们需要的数据了。进行写的操作是

    [peripheralwriteValue:data类型的数据 forCharacteristic:使用到的特征 type:CBCharacteristicWriteWithResponse];

   最后的type类型有两个,分别是CBCharacteristicWriteWithResponse和                                                  CBCharacteristicWriteWithoutResponse;

   选择第一个,每往硬件写入一次数据都会进入

    - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError *)error;

   这个方法会告诉我们这次的写入是否成功,但是如果我们不用考虑往硬件写入的数据成功与否的话,选择第二个类型就ok。

你可能感兴趣的:(iOS之蓝牙开发—CoreBluetooth详解)