美文网首页
CoreBluetooth蓝牙开发

CoreBluetooth蓝牙开发

作者: 5579347b9854 | 来源:发表于2017-07-03 15:48 被阅读0次

导言:智能家居近年来的兴起,蓝牙的重要性就显得越来越突出,刚好公司最近需要做了一个关于蓝牙饮水机项目。

CoreBluetooth框架,是苹果iOS5之后出的一个针对蓝牙4.0出的交互的桥梁,我现在先看下头文件

这个主头文件

我们使用时后只需要导入#import#<CoreBluetooth/CoreBluetooth.h>头文件就可使用里面首先你要初始化中心设备并设置代理

CBCentralManager *center = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];

创建时候最好就是用单利来创建中心设备保证center唯一,

《中心设备:中心设备可以理解为是处理数据的iOS设备,比如你的 iPhone、iPad 等。

外设:外设顾名思义,就是产生数据的外部设备。这个外部设备可以是单片机、嵌入式设备甚至是另一个iOS设备等等。外设可以通过其传感器等产生有用数据,数据后通过蓝牙传给中心设备使用。

在建立连接的之前,外设向外发出广播数据(advertisementData,官方描述“A

dictionary containing any advertisement and scan response

data.”),广播数据是一个字典类数据,中心设备可以获取一定范围内的外设发出的广播数据。》参考talisk斯温的技术博客,iOS蓝牙开发CoreBluetooth快速入门,

扫描外设设备代理方面里有返回外设是否可以连接//检查App的设备BLE是否可用

- (void)centralManagerDidUpdateState:(CBCentralManager *)central

其中state返回值为CBCentralManagerStatePoweredOn,代表外设蓝牙设备可以连接连接设备很简单

.扫描外设

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

接收外设返回设备信息,根据设备信息是否进行连接

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI    “这个代理方法在中心没有停止扫描之前会不停进入其中”

中心连接外设很简单

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

连接成功会进入

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

在连接到外设时候可以停止扫描

[self.center stopScan];

连接失败会进入代理方法可以进行重新连接

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

收到外设特征需要在代理方法里面设置读取和写入数据特征 "service.characteristics"

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

for (CBCharacteristic *Characteristic in service.characteristics)

{

if ([Characteristic.UUID isEqual:[CBUUID UUIDWithString:UUIDNT]]) {//监听外设数据特征

[peripheral setNotifyValue:YES forCharacteristic:Characteristic];

[peripheral readValueForCharacteristic:Characteristic];

self.Characteristic = Characteristic;

peripheral.delegate =self;

}

if ([Characteristic.UUID isEqual:[CBUUID UUIDWithString:UUIDWR]]) {//写入数据特征

[peripheral setNotifyValue:YES forCharacteristic:Characteristic];

peripheral.delegate = self;

[peripheral readValueForCharacteristic:Characteristic];

self.writeCharacteristic = Characteristic;

}

}

当中心接收到外设返回的数据时候会进入这个代理方法

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

遍历外设返回数据并转化为nsstring

for(int i=0;i<[characteristic.value length];i++)

{

NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数

if([newHexStr length]==1)

hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];

else

hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];

}

NSRange rang = NSMakeRange(10, 2);

NSString *string = [hexStr substringWithRange:rang];

写入数据传到外设 "蓝牙4.0一般是16进制数据"

[self.discoveredPeripheral writeValue:data2 forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];

这是一组16进制数据格式

Byte dataArr2[8];

dataArr2[0]= 0x0D;

dataArr2[1] = 0x0A;

dataArr2[2]= 0x0D;

dataArr2[3] = 0x0A;

dataArr2[4]= 0x4F;

dataArr2[5] = 0x4B;

dataArr2[6]= 0x0D;

dataArr2[7] = 0x0D;

NSData *data2 = [NSData dataWithBytes:dataArr2 length:8];

结语,上面只是个人的一些开发经验仅供参考,有什么不对的请在下面指教。

相关文章

  • iOS 关于蓝牙开发

    蓝牙库: 当前iOS中的蓝牙开发使用的都是系统自带的蓝牙库

  • iOS 蓝牙

    1.蓝牙的基础知识 1. iOS中开发蓝牙常用的系统库是

  • 蓝牙开发

    iOS蓝牙开发 Bluetooth蓝牙CoreBluetooth 蓝牙中心设备的实现 蓝牙外设的实现 有Demo ...

  • iOS蓝牙开发 Bluetooth蓝牙CoreBluetooth

    iOS蓝牙开发 Bluetooth蓝牙CoreBluetooth 蓝牙中心设备的实现 蓝牙外设的实现 有Demo ...

  • iOS蓝牙4.0,收发数据设计

    iOS蓝牙开发,现在常规使用的是CoreBlueTooth.framework,即蓝牙4.0开发框架。 1.CBC...

  • iOS蓝牙开发

    蓝牙基础知识 蓝牙库 当前iOS中的蓝牙开发使用的都是系统自带的蓝牙库

  • iOS蓝牙开发

    蓝牙基础知识 蓝牙库 当前iOS中的蓝牙开发使用的都是系统自带的蓝牙库

  • CoreBluetooth蓝牙开发

    导言:智能家居近年来的兴起,蓝牙的重要性就显得越来越突出,刚好公司最近需要做了一个关于蓝牙饮水机项目。 CoreB...

  • iOS蓝牙开发

    本文主要介绍基于蓝牙4.0的智能硬件开发。蓝牙4.0主要基于

  • iOS 蓝牙BLE4.0开发

    蓝牙开发,现在普遍的都是BLE4.0低功耗蓝牙,CoreBluetooth是iOS 开发I比较推荐的一种开发方法...

网友评论

      本文标题:CoreBluetooth蓝牙开发

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