iOS - 蓝牙 BLE

1.导入CoreBluetooth.framework 框架。


2.在需要实现查找蓝牙的类导入#import<CoreBluetooth/CoreBluetooth.h> 头文件。


3.遵循 <CBCentralManagerDelegate,CBPeripheralDelegate>协议。


4.初始化CBCentralManager类 self.bleCentralManager = [[CBCentralManager alloc]initWithDelegate:self queue:centralQueue];(先初始化再查找)

初始化后会实现CBCentralManagerDelegate代理检查准备工作。

#pragma mark -

#pragma mark - CBCentralManagerDelegate

- (void)centralManagerDidUpdateState:(CBCentralManager*)central

{

    if(central.state==CBCentralManagerStatePoweredOn) {

        NSLog(@"蓝牙已就绪!..");

    }else

    {

       NSLog(@"蓝牙未准备好!..");

    }

}


5.查找蓝牙设备

 [self.bleCentralManagerscanForPeripheralsWithServices:nil options:nil];


6.如果查找到蓝牙设备会进入

//扫描到设备会进入方法

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

在此方法内可以找到自己需要的蓝牙,然后从中心连接脸呀外设。也可以保存需要的蓝牙外设在需要的地方进行连接。


7.从中心连接蓝牙外设

       [self.bleCentralManager connectPeripheral:peripheral options:nil];


8.连接外设失败进入到

//连接外设失败

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

{}

连接失败可在此再次进行连接


9.连接蓝牙外设成功进入

#pragma mark 设备扫描与连接的代理

//连接到Peripherals-成功

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

{   

   [peripheral setDelegate:self];

[peripheraldiscoverServices:nil];

}


10.在调用上面括号内 [peripheralsetDelegate:self]; [peripheral discoverServices:nil];两个方法后进入下一步。

 查看蓝牙外设有哪些服务

//扫描到服务 查询蓝牙服务

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

   if (!error) {

       NSLog(@"扫描到的外设服务:%@ ->%@",peripheral.name,peripheral.services);

       for (CBService *service in peripheral.services) {

           //查询服务所带的特征值           

           [peripheraldiscoverCharacteristics:nil forService:service];

 

       }

   }   

}


11.在查询服务所带的特征值后进入下一个方法,进行特征的

//扫描到特征

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

    [self.bleTimerinvalidate];

if(!error) {

//获取Characteristic的值

        for (CBCharacteristic *characteristicin service.characteristics){

if([characteristic.UUID.UUIDString isEqualToString:@"FF01"]) {

                [self.blePeripheralreadValueForCharacteristic:characteristic];

            }

}}}

此处根据需要有两种实现方法                         

[per setNotifyValue:YESforCharacteristic:characteristic];

[self.blePeripheralreadValueForCharacteristic:characteristic];


12.获得特征值后订阅有两种方法,我们来说 [self.blePeripheral readValueForCharacteristic:characteristic];这种方法。会进入下面方法处理

#pragma mark 设备信息处理

//扫描到具体的值 8处理蓝牙发过来的数据

- (void)peripheral:(CBPeripheral*)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristicerror:(nullable NSError *)error{

   if (error) {

       NSLog(@"扫描外设的特征失败!%@-> %@->%ld-> %@",peripheral.name, peripheral.RSSI,(long)peripheral.state,[error localizedDescription]);

       return;

    }

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

       if (bytes[0] == 0x14 && bytes[1] == 0x44){

           int Temperature = ((bytes[3] << 8) + bytes[2]) / 100;

           NSLog(@"温度 %d", Temperature);

           

       }

}

 

这是一个简单的流程。具体还需要自己根据设备定制。仅供参考与自己回忆。如果同时连接多个蓝牙,可以在扫描的时候把扫描到的蓝牙放进一个数组然后进行连接。

你可能感兴趣的:(ios,蓝牙,BLE)