BlackBerry 10 BlackBerry OS 7 1 手机通过蓝牙串口读取Arduino 蓝牙传过来的温度

               

目标:BlackBerry手机通过蓝牙串口读取Arduino 蓝牙传过来的温度 湿度信息

硬件:Arduino主板 + DHT11温湿度传感器 + 蓝牙模块,开发工具C语言

手机:BlackBerry 9360手机,OS 7.1,开发工具Java

手机:BlackBerry 10 Alpha手机,OS  10.0.xxx,开发工具BlackBerry 10 NDK

通讯:手机和Arduino通过蓝牙连接




BlackBerry 9360手机上面的显示:湿度41,温度11(哈冷啊。。。。。。。。。。。。。。。。)


Arduino代码参考这里 http://playground.arduino.cc/Main/DHTLib

代码如下:

// //   FILE:  dht_test.pde// PURPOSE: DHT library test sketch for Arduino//#include <dht.h>dht DHT;#define DHT11_PIN 8#define DHT22_PIN 5void setup(){  Serial.begin(9600);  Serial.println("DHT TEST PROGRAM ");  Serial.print("LIBRARY VERSION: ");  Serial.println(DHT_LIB_VERSION);  Serial.println();  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");}void loop()// READ DATA  int chk;   // READ DATA  Serial.print("DHT11, \t");  chk = DHT.read11(DHT11_PIN);  switch (chk)  {    case DHTLIB_OK:    Serial.print("OK,\t");   break;    case DHTLIB_ERROR_CHECKSUM:   Serial.print("Checksum error,\t");   break;    case DHTLIB_ERROR_TIMEOUT:   Serial.print("Time out error,\t");   break;    default:   Serial.print("Unknown error,\t");   break;  } // DISPLAT DATA  Serial.print(DHT.humidity,1);  Serial.print(",\t");  Serial.println(DHT.temperature,1);  delay(1000);}//// END OF FILE//



BlackBerry  OS 7.1

BlackBerry Java API封装了串口通讯,所以用起来就很方便了。

开发工具eJDE中导入sample程序BluetoothSerialPortDemo ,修改代码接收蓝牙数据并显示在文本框中。

                

                        // Read in the contents and get the event lock for this                        // application so we can update the info field.                     Util.log("Read bluetooth data...");                     int i = 0;                     while(true){                      i++;                            String cmd = "";                            byte b;                            while ((( b = _din.readByte()) > 0) &&( (char)b != ((char)0x0a)) ){                             cmd = cmd + (char)b;                            }                                                         Util.log("Received " + cmd);                            _infoField.setText(i + ": " + cmd);                        }                      

注:

Arduino中,Serial.println() 出来的数据是0x0d, 0x0a。在Java中,我只检测回车字符  (char)0x0a作为一条数据的分隔符即可


BlackBerry  OS 10

BlackBerry NDK C/C++ API封装了蓝牙SPP API,用起来稍微复杂一点(和Android API类似),连接蓝牙设备的时候,bt_spp_open()方法需要对方的服务UUID,mac地址。

下载BlackBerry 蓝牙例子代码bluetoothsppchat,导入到NDK中,build,运行即可。

注意其中的UUID的定义:

#define SPP_SERVICE_UUID "00001101-0000-1000-8000-00805F9B34FB"

//! [7]void ChatManager::connectToSPPService(){    m_chatHistory.clear();    const int fd = bt_spp_open(m_remoteAddress.toAscii().data(), (char *) SPP_SERVICE_UUID, false);    if (fd >= 0) {        updateChatWindow("SPP Client\n\n");        setSPPClient(fd);    } else {        showDialog("spp_open fail", "errno = " + QString::number(errno) );    }}//! [7]
BB 10运行效果如图



参考:

蓝牙串口服务  UUID   '{00001101-0000-1000-8000-00805F9B34FB}' 

https://bluetooth.org/Technical/AssignedNumbers/service_discovery.htm


http://www.douban.com/group/topic/20009323/

http://www.cnblogs.com/lyout/archive/2011/04/11/2012175.html


BlackBerry 10 NDK Bluetooth API
http://developer.blackberry.com/native/reference/bb10/com.qnx.doc.bluetooth/topic/manual/btspp.h_functions_overview.html


BlackBerry 10 NDK蓝牙例子(Bluetooth SPP Chat Example)说明
http://blackberry.github.com/Cascades-Samples/bluetoothsppchat.html


BlackBerry 10 NDK例子代码(包括蓝牙)下载
https://github.com/blackberry/Cascades-Samples/

安卓手机和Arduino蓝牙通讯例子 Data transfer between Android and Arduino via Bluetooth
http://english.cxem.net/arduino/arduino5.php

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

要在Arduino环境中使用ESP32通过蓝牙模拟串口和透传Serial数据,你可以使用ESP32的蓝牙功能和`SoftwareSerial`库来模拟串口通信,并使用`Serial`对象来透传数据。以下是一个示例代码: 首先,确保你已经安装了`SoftwareSerial`库。在Arduino IDE中,选择 "工具" -> "管理库",然后搜索并安装 "SoftwareSerial" 库。 然后,使用以下示例代码: ```cpp #include <SoftwareSerial.h> SoftwareSerial bluetoothSerial(10, 11); // RX, TX (使用不同的引脚号,例如10和11) void setup() { Serial.begin(115200); bluetoothSerial.begin(9600); // 设置蓝牙模块的波特率 Serial.println("Bluetooth Serial started"); } void loop() { if (bluetoothSerial.available()) { char data = bluetoothSerial.read(); Serial.print("Received data from Bluetooth: "); Serial.println(data); // 发送数据给串口 Serial.print("Sending data to Serial: "); Serial.println(data); } if (Serial.available()) { char data = Serial.read(); Serial.print("Received data from Serial: "); Serial.println(data); // 发送数据给蓝牙模块 bluetoothSerial.print("Sending data to Bluetooth: "); bluetoothSerial.print(data); bluetoothSerial.println(); } } ``` 在此代码中,我们使用了`SoftwareSerial`库创建了一个虚拟的串口对象`bluetoothSerial`,用于与蓝牙模块进行通信。 在`setup()`函数中,我们初始化串口蓝牙模块的通信。在这个示例中,我们将蓝牙模块的波特率设置为9600bps。 在`loop()`函数中,我们检查蓝牙串口是否有可用数据。如果有可用数据,我们将接收到的数据打印到串口,并通过调用`Serial.print()`和`Serial.println()`将数据透传给串口。 同时,我们还检查串口是否有可用数据。如果有可用数据,我们将接收到的数据打印到串口,并通过调用`bluetoothSerial.print()`和`bluetoothSerial.println()`将数据透传给蓝牙模块。 请注意,你需要根据你所使用的蓝牙模块的具体配置和波特率来调整代码。另外,确保你已正确连接蓝牙模块的RX和TX引脚,并使用正确的引脚号初始化`SoftwareSerial`对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值