我怎么能接受简单的整数值通过蓝牙的ios [英] How can I receive simple integer values over bluetooth in ios
问题描述
我想了解如何获得插入一个Arduino板一些传感器谈论到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的我送一个电话来读取数据,然后将数据由一块code的,看起来像接受:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!