如何在 CBPeripheral 设备上编写命令?

Posted

技术标签:

【中文标题】如何在 CBPeripheral 设备上编写命令?【英文标题】:How to write command on CBPeripheral device? 【发布时间】:2015-07-03 07:02:25 【问题描述】:

我有一个有两个模块的 BLE 设备

1-蓝牙 (BlueCreation BC127A) 2-MicroController 附有一些命令,如 LOGIN RESET VERSION 等。

我想向与 uC 通信的 BLE 写入命令。

按照我的规范/文档,数据包结构应类似于:

当我在 BLE 上编写确切的命令时,出现错误:“值的长度无效。”

根据文档,它的数据包长度是 17,我认为这是错误的,因为当我得到它时 sizeof(byte)/sizeof(char);我得到 4。我使用了这两个值,但我无法得到积极的回应。

我需要帮助通过成功编写命令来克服这个问题。这是我的第一个蓝牙集成项目。

我的代码 按照数据包结构,我的命令 LOGIN 变为:

    NSString *text = @"02 00 0B 00 00 00 01 10 82 10 83 04 05 06 00 00 03";

    const char* byte = [text cStringUsingEncoding:[NSString defaultCStringEncoding]];//(char)[text UTF8String];
    length = sizeof(byte)/sizeof(char);
    //NSData *data = [NSData dataWithBytes:&byte length:length];
    NSData *data = [self dataWithStringHex:text];
    if (self.bleShield.activePeripheral.state == CBPeripheralStateConnected) 
    CBUUID *uuid_service = [CBUUID UUIDWithString:@"67D13B00-89B8-11E3-9DE5-0002A5D5C51B"];
    CBUUID *uuid_char = [CBUUID UUIDWithString:@"892778A0-89B8-11E3-8821-0002A5D5C51B"];
    CBService *service = [self findServiceFromUUID:serviceUUID p:p];

if (!service)

    NSLog(@"Could not find service with UUID %@ on peripheral with UUID %@",
          [self CBUUIDToString:serviceUUID],
          p.identifier.UUIDString);

    return;
 
CBCharacteristic *characteristic = [self findCharacteristicFromUUID:characteristicUUID service:service];

if (!characteristic)

    NSLog(@"Could not find characteristic with UUID %@ on service with UUID %@ on peripheral with UUID %@",
          [self CBUUIDToString:characteristicUUID],
          [self CBUUIDToString:serviceUUID],
          p.identifier.UUIDString);

    return;


[p writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];



-(CBService *) findServiceFromUUID:(CBUUID *)UUID p:(CBPeripheral *)p 
for(int i = 0; i < p.services.count; i++)

    CBService *s = [p.services objectAtIndex:i];
    if ([self compareCBUUID:s.UUID UUID2:UUID])
        return s;


return nil; //Service not found on this peripheral 

-(CBCharacteristic *) findCharacteristicFromUUID:(CBUUID *)UUID service:(CBService*)service 
for(int i=0; i < service.characteristics.count; i++)

    CBCharacteristic *c = [service.characteristics objectAtIndex:i];
    if ([self compareCBUUID:c.UUID UUID2:UUID]) return c;


return nil; //Characteristic not found on this service 

- (NSData *)dataWithStringHex:(NSString *)string 
NSString *cleanString;
cleanString = [string stringByReplacingOccurrencesOfString:@"<" withString:@""];
cleanString = [cleanString stringByReplacingOccurrencesOfString:@">" withString:@""];
cleanString = [cleanString stringByReplacingOccurrencesOfString:@" " withString:@""];

NSInteger length = [cleanString length];
uint8_t buffer[length/2];
for (NSInteger i = 0; i < length; i+=2)

    unsigned result = 0;
    NSScanner *scanner = [NSScanner scannerWithString:[cleanString substringWithRange:NSMakeRange(i, 2)]];
    [scanner scanHexInt:&result];
    buffer[i/2] = result;

return  [[NSMutableData alloc] initWithBytes:&buffer   length:length/2]; 

谢谢。

【问题讨论】:

好的,显示你如何读取值的代码,也给我们它的值(如 NSData,CBCharacteristic.value),以及你尝试编写它的时间。 @Larme mate 我添加了代码。我添加的 UUID 的特征是写入类型。 能否在读取值并记录值时也显示代码? 我猜你应该像这样创建NSData:***.com/a/26984127/1801544 是的,正如@Larme 所说,您需要将 NsData 创建为多个字节,而不是字符。 【参考方案1】:

认为蓝牙机型未完成配置。

请检查您的 BLE 模块的虚拟机应用软件。 确认系统是否添加了接收数据时的响应功能。

【讨论】:

以上是关于如何在 CBPeripheral 设备上编写命令?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 CoreBluetooth 中测试两个 CBPeripheral 的相等性?

如何自动连接 CBPeripheral(带有 iBeacon + 蓝牙芯片的 BLE 设备)并且应用程序未运行

同一设备的多个 CBPeripheral

iOS CBPeripheral 连接问题

修改BLE设备名称

在连接之前识别 CBPeripheral 的类型