ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

蓝牙相关

2021-01-08 10:01:59  阅读:219  来源: 互联网

标签:central characteristic peripheral 蓝牙 error 相关 CBPeripheral void


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.

 

  • ●  HFP using eSCO channel

  • ●  A2DP using ACL channel

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有