问题描述
我正在尝试学习如何将一些传感器插入 Arduino 板,以便通过蓝牙与 Red Bear Labs 迷你板与 iPhone 通话,但遇到了砖墙.
I am trying to learn how to get some sensors plugged into an Arduino board to talk to an iPhone over Bluetooth with a Red Bear Labs mini board but have hit a brick wall.
传感器获得读数,并通过 BLE 发送到手机.到目前为止,我已连接到该设备,并取回了看似数据的内容,但我无法理解.
The sensors get a reading and this is sent to the phone over BLE. So far I've connected to the device and I get back what appears to be data but I can't make sense of it.
我写了一个看起来像这样的小草图来模拟传感器数据.
I've written a little sketch that looks like this, to simulate the sensor data.
#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 中,我发送一个调用来读取数据,然后数据被一段代码接收,如下所示:
In iOS I send a call to read the data and then the data is received by a piece of code that looks something like:
- (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;
}
}
}...
}
但是当我查看返回的数据时,它对我来说完全没有意义..(我会尽快找出一个例子).
But when I look at the data that comes back it just makes no sense to me at all.. (I'll dig out an example as soon as I can).
有谁知道我遗漏了一个简单的步骤,或者我可以查看一个很好的例子来尝试更好地理解这一点?
Does anyone know a simple step I'm missing or a good example I could look at to try and better understand this?
推荐答案
我终于意识到数据是正确的,我不得不通过位移位来拉出"数据.
I finally realised that the data was correct, I had to 'pull out' the data by bit shifting it.
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);
}
这篇关于如何在 ios 中通过蓝牙接收简单的整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!