其他分享
首页 > 其他分享> > 蓝牙相关

蓝牙相关

作者:互联网

central(中心) 和 peripheral(外设)
iOS 设备既可以作为 central,也可以作为 peripheral,这主要取决于通信需求。
区分的方式即是这两个角色的重要特点:提供数据的是谁,谁就是 peripheral;需要数据的是谁,谁就是 central。就像是 client 和 server 之间的关系一样。
 

在 BLE 中,最常见的就是广播。实际上,peripheral 在不停的发送广播,希望被 central 找到。广播的信息中包含它的名字等信息。central 的作用则是去 scan,找到需要连接的 peripheral,连接后便可进行通信了。

 

[[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];

搜索当前可用的 peripheral

[myCentralManager scanForPeripheralsWithServices:nil options:nil];
 
- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI {

NSLog(@"Discovered %@", peripheral.name);
}
[myCentralManager stopScan];

 

连接 peripheral

[myCentralManager connectPeripheral:peripheral options:nil];
- (void)centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral {

NSLog(@"Peripheral connected");
}

搜索 peripheral 的 service

  [peripheral discoverServices:nil];

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

for (CBService *service in peripheral.services) {
NSLog(@"Discovered service %@", service);
}
}

搜索 service 的 characteristic

[peripheral discoverCharacteristics:nil forService:interestingService];
- (void)peripheral:(CBPeripheral *)peripheral
didDiscoverCharacteristicsForService:(CBService *)service
error:(NSError *)error {

for (CBCharacteristic *characteristic in service.characteristics) {
NSLog(@"Discovered characteristic %@", characteristic);
}
}

读取 characteristic 数据

  1、
NSLog(@"Reading value for characteristic %@", interestingCharacteristic);
[peripheral readValueForCharacteristic:interestingCharacteristic];
 
2、
- (void)peripheral:(CBPeripheral *)peripheral
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError *)error {

NSData *data = characteristic.value;
// parse the data as needed
}
订阅 Characteristic 数据
 
[peripheral setNotifyValue:YES forCharacteristic:interestingCharacteristic];
 
- (void)peripheral:(CBPeripheral *)peripheral
didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError *)error {

if (error) {
NSLog(@"Error changing notification state: %@", [error localizedDescription]);
}
}
 
当订阅成功以后,那数据便会实时的传回了,数据的回调依然和之前读取 characteristic 的回调相同(注意,不是订阅的那个回调)peripheral:didUpdateValueForCharacteristic:error:

向 characteristic 写数据

[peripheral writeValue:dataToWrite forCharacteristic:interestingCharacteristic type:CBCharacteristicWriteWithResponse];
 
- (void)peripheral:(CBPeripheral *)peripheral
didWriteValueForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError *)error {

if (error) {
NSLog(@"Error writing characteristic value: %@", [error localizedDescription]);
}
}
 

检查设备是否能作为 central

centralManagerDidUpdateState
typedef NS_ENUM(NSInteger, CBCentralManagerState) {
CBCentralManagerStateUnknown = 0,
CBCentralManagerStateResetting,
CBCentralManagerStateUnsupported,
CBCentralManagerStateUnauthorized,
CBCentralManagerStatePoweredOff,
CBCentralManagerStatePoweredOn,
};
 

检查 characteristic 访问权限

CBCharacteristicproperties
typedef NS_OPTIONS(NSUInteger, CBCharacteristicProperties) {
CBCharacteristicPropertyBroadcast = 0x01,
CBCharacteristicPropertyRead = 0x02,
CBCharacteristicPropertyWriteWithoutResponse = 0x04,
CBCharacteristicPropertyWrite = 0x08,
CBCharacteristicPropertyNotify = 0x10,
CBCharacteristicPropertyIndicate = 0x20,
CBCharacteristicPropertyAuthenticatedSignedWrites = 0x40,
CBCharacteristicPropertyExtendedProperties = 0x80,
CBCharacteristicPropertyNotifyEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0) = 0x100,
CBCharacteristicPropertyIndicateEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0) = 0x200
};
多个权限可以通过|&来判断是否支持,比如判断是否支持读或写:
BOOL isSupport = characteristic.properties & (CBCharacteristicPropertyRead | CBCharacteristicPropertyWrite)
 
 

写入后是否回调

[self.connectedPeripheral writeValue:data forCharacteristic:connectedCharacteristic type:CBCharacteristicWriteWithResponse];
typedef NS_ENUM(NSInteger, CBCharacteristicWriteType) {
CBCharacteristicWriteWithResponse = 0,
CBCharacteristicWriteWithoutResponse,
};
 
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

 

 

CBCharacteristicPropertyNotify
Notifications of the characteristic’s value are permitted, without a response from the central to indicate that the notification was received.

Available in iOS 5.0 and later.
CBCharacteristicPropertyIndicate
Indications of the characteristic’s value are permitted, with a response from the central to indicate that the indication was received.

 

标签:central,characteristic,peripheral,蓝牙,error,相关,CBPeripheral,void
来源: https://www.cnblogs.com/zlaizp/p/14250086.html