我怎样才能通过蓝牙接收简单的整数值在ios中

我试图学习如何让一些传感器插入到Arduino板上,通过蓝牙与红熊实验室迷你板交谈,但却碰到了砖墙。

传感器读取数据,并通过BLE发送到手机。 到目前为止,我已经连接到设备,我回来似乎是数据,但我无法理解它。

我写了一个看起来像这样的小图,来模拟传感器数据。

#include <SoftwareSerial.h> SoftwareSerial bluetooth(5, 6); void setup() { bluetooth.begin(57600); } void loop() { //int reading = analogRead(2); int reading = 123; // fake reading byte lowerByte = (byte) reading & 0xFF; byte upperByte = (byte) (reading >> 8) & 0xFF; bluetooth.write(reading); bluetooth.write(upperByte); bluetooth.write(lowerByte); delay(1000); } 

在iOS中,我发送一个调用来读取数据,然后通过一段代码来接收数据:

 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { Byte data[20]; static unsigned char buf[512]; static int len = 0; NSInteger data_len; if (!error && [characteristic.UUID isEqual:[CBUUID UUIDWithString:@RBL_CHAR_TX_UUID]]){ data_len = characteristic.value.length; [characteristic.value getBytes:data length:data_len]; if (data_len == 20){ memcpy(&buf[len], data, 20); len += data_len; if (len >= 64){ [[self delegate] bleDidReceiveData:buf length:len]; len = 0; } } else if (data_len < 20) { memcpy(&buf[len], data, data_len); len += data_len; [[self delegate] bleDidReceiveData:buf length:len]; len = 0; } } }... } 

但是当我看到返回的数据对我来说根本没有意义..(我会尽快挖出一个例子)。

有谁知道我错过了一个简单的步骤,或者我可以看看一个很好的例子,试图更好地理解这一点?

我终于意识到数据是正确的,我不得不通过位移来“拉出”数据。

 UInt16 value; UInt16 pin; for (int i = 0; i < length; i+=3) { pin = data[i]; value = data[i+2] | data[i+1] << 8; NSLog(@"Pin: %d", pin); NSLog(@"Value %d",value); }