IOS:将命令值写入corebluetooth外围设备以获得响应

时间:2014-12-19 14:04:16

标签: ios objective-c core-bluetooth

开发corebluetooth应用程序,并且与外围设备的通信等方面很糟糕。我很困惑地编写命令来观察以便从中检索信息。请在下面找到我的代码

  int sendcommand[6];
  sendcommand[0]=0x01;
  sendcommand[1]=6;
  sendcommand[2]=0x70;
  sendcommand[3]=0x00;
  sendcommand[4]=crcData[0];// value is -100
  sendcommand[5]=crcData[1];// value is -31

  NSMutableArray *arr=[[NSMutableArray  alloc]initWithCapacity:6];
  for (int i = 0; i < 6; i++) {
    NSNumber *number = [NSNumber numberWithFloat:sendcommand[i]];
    [arr addObject:number];
   }
   NSLog(@"mutable arr is %@",arr);

  NSString *error1;
   NSData *dataarr = [NSPropertyListSerialization dataFromPropertyList:arr    
   format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error1];


CBMutableCharacteristic *testCharacteristic = [[CBMutableCharacteristic alloc]  
initWithType:characteristicUUID 
properties:CBCharacteristicPropertyRead|CBCharacteristicPropertyWrite value:dataarr 
 permissions:CBAttributePermissionsReadable|CBAttributePermissionsWriteable];
NSLog(@"Read or Write %@ ",testCharacteristic);

[peripheral writeValue:dataarr forCharacteristic:testCharacteristic 
type:CBCharacteristicWriteWithoutResponse];

我的问题是通过使用上面的六个命令创建一个打包器并将数据发送到外围设备,并响应它返回更新值的一些信息。获取控制台值为

  2014-12-19 19:26:51.068 Bluetooth_iph[1267:180180]    

   didUpdateNotificationStateForCharacteristic
   2014-12-19 19:26:51.069 Bluetooth_iph[1267:180180] characteristic.UUID : 8881
   2014-12-19 19:26:51.069 Bluetooth_iph[1267:180180] characteristic.value : (null)
   2014-12-19 19:26:51.069 Bluetooth_iph[1267:180180]    
    didUpdateNotificationStateForCharacteristic
    2014-12-19 19:26:51.070 Bluetooth_iph[1267:180180] characteristic.UUID : 8882
    2014-12-19 19:26:51.070 Bluetooth_iph[1267:180180] characteristic.value : (null)

请帮我创建上述命令的数据包,从蓝色外设获得响应。

1 个答案:

答案 0 :(得分:1)

您不应手动创建CBC特性。您应该使用CBCentralManager在方法中返回的那些:

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

您可以从以下位置访问这些特征:

service.characteristics