我正在使用 RedBear BLE Shield将数据连续流式传输到 iOS。
我正在使用SDK中提供的聊天 iOS 应用程序来调试以下 arduino 代码:
#include <Arduino.h>
#include <SPI.h>
#include "ble.h"
unsigned int reading = 0;
void setup()
{
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(LSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV16);
SPI.begin();
ble_begin();
Serial.begin(57600);
}
unsigned char buf[16] = {0};
unsigned char len = 0;
void loop()
{
ble_do_events();
if(ble_connected()){
reading = 10; //this is fake data for testing purposes.
buf[0] = reading;
reading = reading << 8;
reading |= 11; //again same fake data
buf[1] = reading & 0xFF;
len = 2;
Serial.print(reading); Serial.print(" -> LSB:"); Serial.print(buf[1], HEX); Serial.print(" MSB:"); Serial.println(buf[0], HEX);
len = 2;
for (int i = 0; i < len; i++)
ble_write(buf[i]);
ble_write(0x0D);
len = 0;
ble_do_events();
delay(20);
}
}
我遇到了以下问题:我第一次尝试连接到 BLE Shield 时,它只执行一次循环并且卡住了。然后,如果我断开连接并重新连接,数据就会开始按我的意愿传输。
有人试过这个吗?