1.在info.plist中添加
Privacy - Location Always Usage Description 显示给用户看的信息 例如:将获取您的位置信息
Privacy - Location When In Use Usage Description 显示给用户看的信息 例如:将获取您的位置信息
2.添加以下头文件:
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>
3.创建2个属性
@property(nonatomic,strong)CBCentralManager *centralManager;
@property(nonatomic,strong)CBPeripheral *peripheral;
4.实例化对象并检测蓝牙是否开启
//只要delegate设置为self就开始检测蓝牙是否开启
_centralManager= [[CBCentralManageralloc]initWithDelegate:self queue:nil];
5.开始扫描
- (void)centralManagerDidUpdateState:(CBCentralManager*)central {
switch(central.state) {
case CBManagerStatePoweredOn:
注意:此处的ServiceUUID(定义的宏)是和嵌入式确定好,可以过滤掉不是自己的蓝牙产品,如果不设置就扫描所有蓝牙产品
[_centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:ServiceUUID]]options:nil];
break;
default:
break;
}
}
6.蓝牙信息获取
- (void)centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber*)RSSI {
(1)peripheral:外设(即蓝牙设备),连接,写入数据的时候需要用到。
(2)advertisementData:蓝牙传输过来的数据,包括:设备名称,自定义数据等。 自定义数据有长度限制,自己想要什么数据要和嵌入式商量好,例如MAC地址,传感器数据等
(3)RSSI:侧面反映距离的远近
}
7.发起连接请求
[_centralManager connectPeripheral:_peripheral options:nil];
8.连接成功后回调方法(此时应停止蓝牙扫描,然后设置代理,发现服务)
- (void)centralManager:(CBCentralManager*)central didConnectPeripheral:(CBPeripheral*)peripheral {
[central stopScan];
peripheral.delegate=self;
[peripheral discoverServices:nil];
}
9.连接失败回调方法
- (void)centralManager:(CBCentralManager*)central didFailToConnectPeripheral:(CBPeripheral*)peripheral error:(NSError*)error {
}
10.蓝牙发现的服务(发现服务的每个特征)
- (void)peripheral:(CBPeripheral*)peripheral didDiscoverServices:(NSError*)error {
for(CBService *service in [peripheral services]) {
[peripheral discoverCharacteristics:nil forService:service];
}
}
11.读取每个特征值
- (void)peripheral:(CBPeripheral*)peripheral didDiscoverCharacteristicsForService:(CBService*)service error:(NSError*)error {
for(inti =0; i < service.characteristics.count; i++) {
CBCharacteristic *c = [service.characteristics objectAtIndex:i];
//此处的UUID是根据嵌入式定义的。可以指定读取。但特征有分多种类型,例如读,写,通知等。
//以下的是读的过程,如果是通知则用 [peripheral setNotifyValue:YES forCharacteristic:c];
if([[[c UUID] UUIDString] isEqual:@"6C290D2E-1C03-ACA1-AB48-A9B908BAE79E"])
{
[peripheral readValueForCharacteristic:c];
}
}
}
12.读取指定特征的特征值
- (void)peripheral:(CBPeripheral*)peripheral didUpdateValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError*)error {
if([[[c UUID] UUIDString] isEqual:@"6C290D2E-1C03-ACA1-AB48-A9B908BAE79E"])
{
characteristic.value 即为特征值,这个也是嵌入式自定义的格式
}
}
13.向指定的特征写入数据
[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
14.写入数据成功后回调
- (void)peripheral:(CBPeripheral*)peripheral didWriteValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError*)error {
注意:此时已经写入数据成功了,但直接在这里显示characteristic.value是发现没有修改过,所以此时必须再读取一下特征即:[peripheral readValueForCharacteristic:c]; c为特征,然后回到步骤12
}
15.蓝牙断开连接后回调
- (void)centralManager:(CBCentralManager*)central didDisconnectPeripheral:(CBPeripheral*)peripheral error:(NSError*)error {
这里可以根据需求做处理,例如自动重连等操作,步骤7
}
16.主动断开连接
[centralManager cancelPeripheralConnection:peripheral];
网友评论