如何编辑广告数据? [英] How to edit the advertisement data?

查看:277
本文介绍了如何编辑广告数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从中央管理器编辑蓝牙外围设备的广告数据。

I need to edit the advertisement data of bluetooth peripheral from central manager.

我尝试了很多。

以下代码提供了详细信息:

The following code provides the details :

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.从服务中发现特征:

3.Discovering characteristics from service:

- (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。更新通知状态:

4.Updating Notification State:

- (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.Update在外围设备中写入值:

5.Update Write value in Peripheral:

- (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的新手。感谢帮助

i am new to IOS.Helps are appreciated

预先感谢。.

推荐答案

没有在外围设备上设置广告数据的通用方法从中央。如果您想执行此类操作,则必须在外围设备上实现此功能(通过特制的GATT服务),或者该产品以某种方式提供此功能。

There is no general way of setting advertising data on the peripheral from the central. If you want to do something like this you either have to implement the feature on the peripheral (through a purpose made GATT Service), or this feature is offered by the product somehow.

还要注意,广告是链接层(LL)功能,iOS通常不会公开这些功能。 BLE的iOS API为GAP / GATT级别。

Also note that advertising is a link layer (LL) feature, and those are normally not exposed by iOS. The iOS APIs for BLE is GAP/GATT level.

这篇关于如何编辑广告数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆