蓝牙-b

最近智能家居比较火,好多公司开始开发通过蓝牙对智能家居进行连接控制!下面,我就把自己总结的蓝牙方面的知识分享一下!求吐槽!!!!O(∩_∩)O。。。

1.导入头文件#import <CoreBluetooth/CoreBluetooth.h>

2.设置中心及外设的属性

@property(nonatomic,strong)CBCentralManager*cbCentralMgr;//中心(发起连接)

@property(nonatomic,strong)CBPeripheral*myPeripheral;//外部设备(被动连接)

3.继承代理方法<CBCentralManagerDelegate,CBPeripheralDelegate>

4.创建中心设备的实例并设置代理

self.cbCentralMgr= [[CBCentralManageralloc]initWithDelegate:selfqueue:nil];

5.中心设备设置delegate后会自动调用本机蓝牙状态的方法

- (void)centralManagerDidUpdateState:(CBCentralManager*)central

{

    switch(central.state)

   {

        caseCBCentralManagerStateUnsupported:

         NSLog(@"The platform/hardware doesn't support Bluetooth Low Energy.");

         break;

         caseCBCentralManagerStateUnauthorized:

         NSLog(@"The app is not authorized to use Bluetooth Low Energy.");

         break;

         caseCBCentralManagerStatePoweredOff:

         NSLog(@"Bluetooth is currently powered off.");

         break;

        caseCBCentralManagerStatePoweredOn:

       {

       NSLog(@"CBCentralManagerStatePoweredOn");

        }

       break;

      caseCBCentralManagerStateUnknown:

      default:

      break;

      }

}

6.在状态为CBCentralManagerStatePoweredOn的时候可以通过中心设备扫描周边的设备

[self.cbCentralMgr    scanForPeripheralsWithServices:niloptions:@{CBCentralManagerScanOptionAllowDuplicatesKey: [NSNumbernumberWithBool:YES]}];

7.一旦扫描到设备就会自动调用代理方法

//发现蓝牙设备,可能发现不止一个蓝牙设备,所以该方法可能被调用多次

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

8.在扫描到一个设备时,我们可以进行外部设备和中心的连接

//连接某个蓝牙,一个中心设备可以连接多个周围的蓝牙设备,苹果最多连接10个外设

[self.cbCentralMgr connectPeripheral:peripheral options:[NSDictionary dictionaryWithObject:[NSNumbernumberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];

9.在连接的时候同时可以关闭中心的扫描

[self.cbCentralMgrstopScan];

10.在中心和外部设备的连接过程中,可能会调用以下几个方法:

a.当连接成功时调用

//当连接上某个蓝牙之后,CBCentralManager会通知代理处理

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

b.当连接失败时调用

//当连接蓝牙失败的时候会调用

- (void)centralManager:(CBCentralManager*)central didFailToConnectPeripheral:(CBPeripheral*)peripheral error:(NSError*)error{}

c.当连接断开时调用

//当中心主动与外设断开或外设主动和中心断开成功时调用

- (void)centralManager:(CBCentralManager*)central didDisconnectPeripheral:(CBPeripheral*)peripheral error:(nullableNSError*)error{}

11.中心主动断开与外设的连接

[self.cbCentralMgrcancelPeripheralConnection:peripheral];

12.连接成功后,需要设置外设代理并且查询所有蓝牙服务或蓝牙某一个服务

//因为在后面我们要从外设蓝牙那边再获取一些信息并与之通讯,这些过程会有一些事件可能要处理,所以要给这个外设设置代理

peripheral.delegate=self;

//查询所有蓝牙服务,或查询蓝牙外设上的指定服务

[self.myperipheraldiscoverServices:nil];

13.查询所有服务时会调用外设的方法

//返回的蓝牙服务通知通过代理实现

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

{

  for(CBService* serviceinperipheral.services){

  [peripheraldiscoverCharacteristics:nilforService:service];

  }

}

14.在这个方法里会进行扫描所有特征值,进而调用外设查询特征值的代理方法

//返回的蓝牙特征值通知通过代理实现

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

}

15.在上面的方法中可以获取到已经与中心连接的外设内有哪些特征值,并且可以通过打开通知来读取特征值内数据的变化

[peripheralsetNotifyValue:YESforCharacteristic:characterstic];

16.特征值内数据变化时会调用外设的代理方法

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

17.在该方法里面读取的特征值内容是NSData类型,我们也可以转换为字节数组进行判断

NSData*data = characteristic.value;

//data转byte数组

Byte*testByte = (Byte*)[databytes];

for(inti=0;i<[datalength]; i++){

if((testByte[1] ==2)&&(testByte[0] ==2)) {

  //做简单的演示判断

  }

}

18.与此同时,我们也可以向某一个特征发送信息,只需要调用下面的方法

//根据蓝牙对象和特性发送数据

-(void)sendDatawithperipheral:(CBPeripheral*)peripheral characteristic:(NSString*)characteristicStr service:(NSString*) service data:(NSData*)data {

for(int i=0; i<service.characteristics; i++)

  if([peripheral.services[i].UUIDisEqual:[CBUUIDUUIDWithString:service]]) {

 for(CBCharacteristic*characteristicin[[peripheral.servicesobjectAtIndex:i]characteristics]){

//找到通信的的特性

 if([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:characteristicStr]]){

 [peripheralwriteValue:dataforCharacteristic:characteristictype:CBCharacteristicWriteWithoutResponse];

  }

 }

}

}

}

19.那么,我们该如何读取中心和外部设备的信号值?信号值越小说明信号越弱

a.当未连接时,我们可以通过扫描外设获取- (void)centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber*)RSSI{}方法里的RSSI值

b.当连接时,我们可以通过 [peripheralreadRSSI]来调用 - (void)peripheral:(CBPeripheral*)peripheral didReadRSSI:(NSNumber*)RSSI error:(NSError*)error{}方法获取RSSI值



文/爱吃苹果的兔子(简书作者)
原文链接:http://www.jianshu.com/p/1461ebb498ab
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
原文地址:https://www.cnblogs.com/isItOk/p/5557128.html