2014-12-19 92 views
0

開發corebluetooth應用程序,並且遇到了一些方面的問題,比如與外圍設備的通信.Iam變得困惑,爲了從中檢索信息而編寫命令。我下面IOS:寫命令值到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]; 

我的問題的代碼是通過使用上述六個命令來創建一個封隔器和數據發送到外設和響應,這給一些資料回來更新values.am取得主控臺值

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) 

請幫我爲上面的命令創建數據包,從藍色的外設得到響應。

+0

你肯定不希望你的編碼數據作爲'NSPropertyListSerialization' - ' sendCommand'應該是'char'數組,你應該使用'NSData dataWithBytes:length' – Paulw11

+0

嘗試過,但是同樣輸出我得到 – iosdeveloper

回答

1

您不應該手動創建CBCharacteristics。您應該使用的CBCentralManager返回該方法的那些:

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

您可以訪問特性:

service.characteristics 
+0

你對CBMutablecharacterstics – iosdeveloper

+0

是說,當你處於中央模式時,你不應該創建它們。 – Aris

+0

是否有任何示例代碼片段? – iosdeveloper

相關問題