我需要从中央管理器编辑蓝牙外设的广告数据。
我试了很多..。
以下代码提供了详细信息:
1.外设连接后:
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Connection successfull to peripheral: %@",peripheral);
peripheral.delegate = self;
[peripheral discoverServices:nil];
//Do somenthing after successfull connection.
}2.发现服务:
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
for (CBService *service in peripheral.services) {
NSLog(@"Discovering characteristics for service %@", service);
[peripheral discoverCharacteristics:nil forService:service];
}
}3.从服务中发现特征:
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"B0702880-A295-A8AB-F734-031A98A512DE"]]) {
[peripheral readValueForCharacteristic:characteristic];
NSLog(@"Reading value for characteristic %@", characteristic);
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
}4.更新通知状态:
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(@"characteristic.properties--------------------->%lu",(unsigned long)characteristic.properties);
if (error) {
NSLog(@"Error changing notification state: %@",[error localizedDescription]);
}
// Notification has started
if (characteristic.isNotifying) {
NSLog(@"Notification began on %@", characteristic);
}
NSString* decodeString = @"teststring";
NSData *encodeData = [decodeString dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"to write----- %@",encodeData);
if ((characteristic.properties & CBCharacteristicPropertyWrite) ||
(characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse))
{
[peripheral writeValue:encodeData forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}
else
{
NSLog(@"Not permit to write");
}
}5.更新外围设备中的写入值:
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (error) {
NSLog(@"Error writing characteristic value: %@",[error localizedDescription]);
}
NSData *data = characteristic.value;
NSLog(@"FinalData:%@",data);
}我是IOS.Helps的新手,非常感谢
提前谢谢..
发布于 2016-08-23 21:25:07
没有从中央在外围设备上设置广告数据的一般方法。如果您想做这样的事情,您要么必须在外围设备上实现该功能(通过专门制作的GATT服务),要么该功能是由产品以某种方式提供的。
还请注意,广告是链路层(LL)功能,而这些功能通常不会被iOS公开。BLE的iOS API是GAP/关贸总协定级别的。
https://stackoverflow.com/questions/39077395
复制相似问题