目标: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.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