1

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

1-蓝牙 (BlueCreation BC127A) 2-微控制器附有一些命令,如 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]; }

谢谢。

4

1 回答 1

0

认为蓝牙型号尚未完成配置。

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

于 2017-03-14T01:36:31.793 回答