作者:刘国建
BLE简介
CBCentralManager *manager = [[CBCentralManageralloc]init];
self.manager = manager;
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;
[manager scanForPeripheralsWithServices:niloptions:nil];
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI;
if ([peripheral.namehasPrefix:@"A"] ) {
//连接设备
[manager connectPeripheral:peripheraloptions:nil];
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;(连接成功)
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullableNSError *)error;(连接失败)
[peripheral discoverServices:nil];
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error;
[peripheral discoverCharacteristics:nilforService:service];
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error;
if ([characteristics.UUID.UUIDStringisEqualToString:@"你需要的特征的UUID"]){
//读取特征数据
[peripheral readValueForCharacteristic:characteristics];
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;
BLE速度提升
case CYBLE_EVT_GATTS_XCNHG_MTU_REQ:
{
uint16 cyBle_mtuSize;
if(CYBLE_GATT_MTU > ((CYBLE_GATT_XCHG_MTU_PARAM_T *)eventParam)->mtu)
{
cyBle_mtuSize = ((CYBLE_GATT_XCHG_MTU_PARAM_T *)eventParam)->mtu;
}
else
{
cyBle_mtuSize = CYBLE_GATT_MTU;
}
(void)CyBle_GattsExchangeMtuRsp(((CYBLE_GATT_XCHG_MTU_PARAM_T *)eventParam)->connHandle, cyBle_mtuSize);
}
break;
对于不同的手机,测试如下:连接间隔20-30ms,由于是with response,所以一般一包数据需要两个连接间隔。
网易云大礼包:https://www.163yun.com/gift
本文来自网易实践者社区,经作者刘国建授权发布