使用蓝牙将文本从Arduino发送到iphone,我无法获取character.value

时间:2015-09-24 07:34:21

标签: iphone bluetooth arduino

目的:Arduino向iphone应用程序发送传感器值。

问题: 我制作了一个可以与Arduino连接的BlueTooth应用程序。 我有时像[图4]那样成功。 但我无法从arduino中取出一根弦。 我可以得到“< 00>”或清空。

我需要修改我的申请? 我必须先改变Arduino吗?或者我的ios源代码?

[图1]是Arduino蓝牙源代码=> (蓝牙是蓝牙4.0)

#include <SoftwareSerial.h>

int blueTx = 6; 
int blueRx = 7; 

SoftwareSerial BTSerial(blueRx,blueTx); //connect HC-10 TX, RX

void setup() {
  Serial.begin(9600);
  Serial.println("hello world");      
  BTSerial.begin(9600);
  Serial.println("BTSerial begin");
}

void loop() {
  while(BTSerial.available())
  {

    BTSerial.println("hello world");
    BTSerial.write("BTSerial is open!");
  }
}

[图2]是Xcode Log

2015-09-24 15:58:52.822 Arduino_BLE test1[1727:734383] STATE - Power On
2015-09-24 15:58:52.822 Arduino_BLE test1[1727:734383] SCAN - Scanning
2015-09-24 15:58:52.873 Arduino_BLE test1[1727:734383] DISCOVER - Discover : <CBPeripheral: 0x135537f00, identifier = B4A88537-AF01-B403-AA32-F2C84464CFE6, name = HMSoft, state = disconnected>
2015-09-24 15:58:52.873 Arduino_BLE test1[1727:734383] DISCOVER - Connecting to peripheral <CBPeripheral: 0x135537f00, identifier = B4A88537-AF01-B403-AA32-F2C84464CFE6, name = HMSoft, state = connecting>
2015-09-24 15:58:53.015 Arduino_BLE test1[1727:734383] CONNECT - To HMSoft
2015-09-24 15:58:53.016 Arduino_BLE test1[1727:734383] CONNECT - Scanning stoped
2015-09-24 15:58:53.301 Arduino_BLE test1[1727:734383] DISCOVER_SERVICE - Service : <CBService: 0x13556ae90, isPrimary = YES, UUID = FFE0>
2015-09-24 15:58:53.421 Arduino_BLE test1[1727:734383] DISCOVER_CHAR - Characteristic : <CBCharacteristic: 0x135673ce0, UUID = FFE1, properties = 0x16, value = (null), notifying = NO>
2015-09-24 15:58:53.481 Arduino_BLE test1[1727:734383] UPDATE_VALUE - start
2015-09-24 15:58:53.481 Arduino_BLE test1[1727:734383] UPDATA_VALUE - stringFromData : 
2015-09-24 15:58:53.481 Arduino_BLE test1[1727:734383] UPDATA_VALUE - data : <00>
2015-09-24 15:58:53.691 Arduino_BLE test1[1727:734383] NOTIFY - Notification start
2015-09-24 15:58:53.691 Arduino_BLE test1[1727:734383] NOTIFY - Notification began on <CBCharacteristic: 0x135673ce0, UUID = FFE1, properties = 0x16, value = <00>, notifying = YES>
2015-09-24 15:58:53.691 Arduino_BLE test1[1727:734383] NOTIFY - stringFromData : 

[图3]是我为蓝牙网络编写的源代码。

//DISCOVER CHAR
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    if (error) {NSLog(@"DISCOVER_CHAR - Error");return;}

    for (CBCharacteristic * characteristic in service.characteristics) {
        NSLog(@"DISCOVER_CHAR - Characteristic : %@",characteristic);
        [peripheral readValueForCharacteristic:characteristic];
        [peripheral setNotifyValue:YES forCharacteristic:characteristic];
    }
}

//INTERACT
//READ
-(void)peripheral:(CBPeripheral *)peripheral
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
            error:(NSError *)error
{
    NSLog(@"UPDATE_VALUE - start");
    NSString *stringFromData = [[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding];
    NSLog(@"UPDATA_VALUE - stringFromData : %@", stringFromData);

    [_data appendData:characteristic.value];
    NSLog(@"UPDATA_VALUE - data : %@",_data);
}

[图4]是我之前成功的。但是......我不能再做了

UPDATA_VALUE - start
UPDATA_VALUE - stringFromData :
UPDATA_VALUE - stringFromData :<00>
UPDATA_VALUE - start    
UPDATA_VALUE - stringFromData :hello world

BTSeria
UPDATA_VALUE - stringFromData:<0068656c 6c6f2077 6f726c64 0d0a4254 53657269 61>
UPDATA_VALUE - start
UPDATA_VALUE - stringFromData :l begin

UPDATA_VALUE - stringFromData :<0068656c 6c6f2077 6f726c64 0d0a4254 53657269 616c2062 6567696e 0d0a>

2 个答案:

答案 0 :(得分:0)

当前代码中的语法:

BTSerial.begin(9600);

此语法表明您使用的是9600波特率。首先,您需要弄清楚设备接受数据的波特率是多少。 如果您不知道波特率,您可以尝试使用不同的标准波特率,如2400,4800,9600,19200,38400等。 您可以参考蓝牙模块的数据表获取此信息。

答案 1 :(得分:0)

认识德赛。!!谢谢。我一个接一个地改变了。最后,我收到了来自arduino的消息。问题是Arduino中sensorValue的“值格式”。我刚刚将sensorValue的值格式从“int”更改为“byte”。那是工作!我解决了!!!!再次感谢您的关注。