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//dht DHT;void 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
總結
以上是生活随笔為你收集整理的BlackBerry 10 BlackBerry OS 7 1 手机通过蓝牙串口读取Arduino 蓝牙传过来的温度的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微机原理实验报告
- 下一篇: iOS开发之获取运营商和WIFI