美文网首页
iOS BLE 蓝牙技术

iOS BLE 蓝牙技术

作者: 隐身人 | 来源:发表于2018-03-11 21:56 被阅读31次

今天我所记录的是CoreBlueTooth

简介:

*可用于第三方蓝牙设备交互,设备必须支持蓝牙4.0

*iPhone的设备必须是4S或者更新

*iPad设备必须是iPad mini或者更新

*iOS的系统必须是iOS 6或者更新

*蓝牙4.0以`低功耗`著称,所以一般被称为BLE(bluetooth low energy)

核心:

*CBCentralManager:中心设备(用来连接到外部设备的管家)

*CBPeripheralManager:外部设备(第三方的蓝牙4.0设备)

开发步骤:

*建立中心管家

```

// 1. 创建中心管家,并且设置代理

self.cmgr = [[CBCentralManager alloc]initWithDelegate:self queue:nil];

```

*扫描外设(discover)

```

// 2. 在代理方法中扫描外部设备

 /**

  *  scanForPeripheralsWithServices :如果传入指定的数组,那么就只会扫描数组中对应ID的设备

  *                                  如果传入nil,那么就是扫描所有可以发现的设备

  *  扫描完外部设备就会通知CBCentralManager的代理

  */

 - (void)centralManagerDidUpdateState:(CBCentralManager *)central

{

    if ([central state] == CBCentralManagerStatePoweredOn) {

        [self.cmgr scanForPeripheralsWithServices:nil options:nil];

    }

}

```

```

/**

 *  发现外部设备,每发现一个就会调用这个方法

 *  所以可以使用一个数组来存储每次扫描完成的数组

 */

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

{

    // 有可能会导致重复添加扫描到的外设

    // 所以需要先判断数组中是否包含这个外设

    if(![self.peripherals containsObject:peripheral]){

        [self.peripherals addObject:peripheral];

    }

}

```

*连接外设

```

/**

 *  模拟开始连接方法

 */

- (void)start

{

    // 3. 连接外设

    for (CBPeripheral *ppl in self.peripherals) {

        // 扫描外设的服务

        // 这个操作应该交给外设的代理方法来做

        // 设置代理

        ppl.delegate = self;

        [self.cmgr connectPeripheral:ppl options:nil];

    }

}

```

*扫描外设中的服务和特征

-服务和特征的关系

`每个蓝牙4.0的设备都是通过服务和特征来展示自己的,一个设备必然包含一个或多个服务,每个服务下面又包含若干个特征。`

```

/**

 *  连接外设成功调用

 */

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

{

    // 查找外设服务

    [peripheral discoverServices:nil];

}

```

```

/**

 *  发现服务就会调用代理方法

 *

 *  @param peripheral 外设

 */

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

{

    // 扫描到设备的所有服务

    NSArray *services = peripheral.services;

    // 根据服务再次扫描每个服务对应的特征

    for (CBService *ses in services) {

        [peripheral discoverCharacteristics:nil forService:ses];

    }

}

```

*与外设做数据交互

-在指定的特征下做相应的操作

```

/**

 *  发现服务对应的特征

 */

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

{

    // 服务对应的特征

    NSArray *ctcs = service.characteristics;

    // 遍历所有的特征

    for (CBCharacteristic *character in ctcs) {

        // 根据特征的唯一标示过滤

        if ([character.UUID.UUIDString isEqualToString:@"根据通讯协议里面选择具备读写权限的UUID"]) {

        //进行写入数据到蓝牙设备

         Bytedata[] = {0xF0,0xE0,0x06,0x00};//一般是16进制数据写入

         NSData * dataD = [[NSData alloc] initWithBytes:data length:4];//NSData 是 <f0e00600>

         [peripheralwriteValue:dataD forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];//这个是写入数据到蓝牙设备的方法

        }

    }

}

```

*获取蓝牙返回数据

/**

 *  获取蓝牙返回数据

 */

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

{

    if(error) {

        NSLog(@"-- error -- ");

    }

    @synchronized(self) {

        NSLog(@"didUpdateValueForCharacteristic: %@",characteristic.value);

        if(characteristic.value.length==0){

            return;}

    }

//下面这个是测试接受数据解析操作,具体看各自的蓝牙协议。

    Byte *testByte = (Byte *)[characteristic.value bytes];

    if (testByte[2] == 0x02) {

        if (testByte[3] == 0x01) {

            testdata = testByte[4];

            NSLog(@"返回数据 === %d ===",testdata);

        }

    }

}

*断开连接

```

/**

 *  断开连接

 */

- (void)stop

{

    // 断开所有连接上的外设

    for (CBPeripheral *per in self.peripherals) {

        [self.cmgr cancelPeripheralConnection:per];

    }

  //注意断开蓝牙的时候一定要设备连接上蓝牙再才断,我有过未连接上直接断开会崩溃,闪退的经历。

}

```

相关文章

  • iOS-蓝牙开发

    iOS个版本蓝牙比较 随着蓝牙低功耗技术BLE(Bluetooth Low Energy)的发展,蓝牙技术正在一步...

  • BLE蓝牙ios简介(一)

    what‘s the BLE ? 随着蓝牙低功耗技术BLE(Bluetooth Low Energy)的发展,蓝牙...

  • iOS BLE 蓝牙技术

    今天我所记录的是CoreBlueTooth 简介: *可用于第三方蓝牙设备交互,设备必须支持蓝牙4.0 *iPho...

  • CoreBluetooth

    iOS-BLE蓝牙开发持续更新 - 简书 蓝牙打印小票 一个第三方 IOS BLE4.0蓝牙和外设连接和收发数据的...

  • iOS之蓝牙开发

    前言# 什么是蓝牙?#### 随着蓝牙低功耗技术BLE(Bluetooth Low Energy)的发展,蓝牙技术...

  • Bluetooth LE(低功耗蓝牙)特点

    BLE(Bluetooth Low Energy,低功耗蓝牙)是对传统蓝牙BR/EDR技术的补充。尽管BLE和传统...

  • ios蓝牙参考

    参考文章:iOS中的蓝牙开发iOS-BLE蓝牙开发demo 官网 转载 CenteralManager学习笔记

  • 12月第三周

    iOS-BLE蓝牙开发持续更新 - IOS - 伯乐在线 ReactiveCocoa 中 RACSignal 所有...

  • iOS 蓝牙 BlueTooth BLE

    iOS 蓝牙 BlueTooth BLE 因为工作的需要, 研究了下iOS端蓝牙方面的知识. 研究的并不深入....

  • iOS-蓝牙开发Mark

    iOS蓝牙的开发专题[http://liuyanwei.jumppo.com/2015/07/17/ios-BLE...

网友评论

      本文标题:iOS BLE 蓝牙技术

      本文链接:https://www.haomeiwen.com/subject/uyxgfftx.html