iOS 开发蓝牙操作(用户端)

1、蓝牙技术基于 <CoreBluetooth/CoreBluetooth.h>框架,框架分为管理中心(使用手机等管理其他设备)和外设(被管理的设备,如门禁,家电)两部分,管理中心-CBCentralManager,外设-CBPeripheralManager;

2、蓝牙开发的逻辑流程

  打开蓝牙,本机搜索附近能被搜索的外设,或者按一定的uuid搜索外设,搜索到外设后尝试和某个外设进行连接,然后对外设进行深层扫描,进行数据解析,读取到外设的数据(心率获取)以及写入外设的数据(硬件控制),实现功能。

3、创建管理中心

  1)创建对象

    - (instancetype)init;

    //普通的创建方法

    - (instancetype)initWithDelegate:(nullable id<CBCentralManagerDelegate>)delegate

  queue:(nullable dispatch_queue_t)queue;

   //使用多线程在新建线程创建,避免卡顿,并使用代理进行数据获取

 

   - (instancetype)initWithDelegate:(nullable id<CBCentralManagerDelegate>)delegate

  queue:(nullable dispatch_queue_t)queue

options:(nullable NSDictionary<NSString *, id>   *)options NS_AVAILABLE(NA, 7_0) NS_DESIGNATED_INITIALIZER;

//options参数是一个搜索的配置参数,该参数的具体配置,自己了解还不是很清楚,平时一般出入nil就可以了

 

2)开始搜索周围设备

     管理中心代理有个必选代理

     - (void)centralManagerDidUpdateState:(CBCentralManager *)central

可以获取到本设备的蓝牙状态信息,在蓝牙已打开的情况下,

调用- (void)scanForPeripheralsWithServices:(nullable NSArray<CBUUID *> *)serviceUUIDs options:(nullable NSDictionary<NSString *, id> *)options;

方法对周边的外设进行扫描,

触发代理- (void)centralManager:(CBCentralManager *)central

 didDiscoverPeripheral:(CBPeripheral *)peripheral

     advertisementData:(NSDictionary<NSString *,id> *)advertisementData

                  RSSI:(NSNumber *)RSSI ,

在代理里面就可以获取到设备扫描到的外设,一个一个的响应触发代理,然后在通过外设的属性参数的选出自己需要的一个外设

 

3)管理中心连接外设

然后使用

  • (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options;

用管理中心对外设进行链接,链接外设后触发代理

  • (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral 

可以获取到自己链接外设有没有成功,获取到外设的一些参数

 

4)对选定的外设进行深度扫描

在代理里面获取到外设,对外设代理进行设置,然后扫描外设

  • (void)discoverServices:(nullable NSArray<CBUUID *> *)serviceUUIDs;,

扫描成功后会触发代理方法

  • (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error

然后我们就可以在代理方法中获取到外设的的服务特性service

 for (CBService *service in peripheral.services) 

然后扫描外设的service,

- (void)discoverCharacteristics:(nullable NSArray<CBUUID *> *)characteristicUUIDs forService:(CBService *)service;//[peripheral discoverCharacteristics:nil forService:service];

触发代理

  • (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error

可以获取到外设的服务特性service的Characteristic特征的值,然后

 for (CBCharacteristic *characteristic in service.characteristics)

    {

//        NSLog(@"扫描到服务:%@ 的特征:%@", service.UUID, characteristic.UUID);

        //读取获取Characteristic特征的值

        [peripheral readValueForCharacteristic:characteristic];

        // 搜索特征的 Descriptors

        //会触发代理  peripheral:didDiscoverDescriptorsForCharacteristic:error:发现特征描述符

        [peripheral discoverDescriptorsForCharacteristic:characteristic];

    }

 

5)写入数据

通过触发的深度扫描的代理方法,扫描获取数据信息,对信息进行处理,然后在外设上写入自己想要写入的信息

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error

{

   //订阅实时通知,触发didUpdateNotificationStateForCharacteristic代理方法

     [peripheral setNotifyValue:YES forCharacteristic:characteristic];

    

    for (CBDescriptor *descriptor in characteristic.descriptors) {

       // 获取到 描述符Descriptors 的值,将会触发代理didUpdateValueForDescriptor

        [peripheral readValueForDescriptor:descriptor];

        if (characteristic.properties & CBCharacteristicPropertyWrite ||

            characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse) {

            // 写数据

          [peripheral writeValue:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://www.baidu.com"]] forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

       } else {

            NSLog(@"该字段不可写!");

        }

 

    }

}

 

posted on 2017-02-08 14:44  ~飞想~  阅读(278)  评论(0)    收藏  举报

导航