EB_Bluetooth.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /***************************************************
  2. This is a library for the EB-Bluetooth
  3. The EB-Bluetooth uses software seiral and one extra pin for checking state.
  4. MakerStudio invests time and resources providing this open source code,
  5. please support MakerStudio and open-source hardware by purchasing
  6. products from MakerStudio!
  7. Written by Steve for MakerStudio.
  8. BSD license, all text above must be included in any redistribution
  9. ****************************************************/
  10. #include "EB_Bluetooth.h"
  11. EB_Bluetooth::EB_Bluetooth(uint8_t EB_Rx, uint8_t EB_Tx):swSerial(EB_Tx, EB_Rx)
  12. {
  13. EB_rxPin = EB_Rx;
  14. EB_txPin = EB_Tx;
  15. }
  16. EB_Bluetooth::EB_Bluetooth(uint8_t EB_Rx, uint8_t EB_Tx, uint8_t EB_INT):swSerial(EB_Tx, EB_Rx)
  17. {
  18. EB_rxPin = EB_Rx;
  19. EB_txPin = EB_Tx;
  20. EB_statePin = EB_INT;
  21. }
  22. //initialization
  23. void EB_Bluetooth::begin()
  24. {
  25. pinMode(EB_rxPin, OUTPUT);
  26. pinMode(EB_txPin, INPUT);
  27. pinMode(EB_statePin, INPUT);
  28. bluetoothState = 0;
  29. swSerial.begin(9600);// the default baudrate is 9600
  30. }
  31. int EB_Bluetooth::setName(String name)
  32. {
  33. int isSuccess = 0;
  34. String recvBuf;
  35. char recvChar;
  36. String cmd("AT+NAME");
  37. cmd += name;
  38. cmd += '\r';
  39. cmd += '\n';
  40. int cmdLen = cmd.length();
  41. for(int i = 0; i < cmdLen; i++){
  42. swSerial.write(cmd[i]);
  43. }
  44. delay(200);
  45. int len = 0;
  46. if(len = available()){
  47. for(int i = 0; i < len; i++){
  48. recvChar = read();
  49. Serial.print(recvChar);
  50. recvBuf += recvChar;
  51. }
  52. if(recvBuf.indexOf(name) != -1){
  53. isSuccess= 1;
  54. }
  55. }
  56. return isSuccess;
  57. }
  58. int EB_Bluetooth::print(uint8_t byte)
  59. {
  60. return(swSerial.write(byte));
  61. }
  62. int EB_Bluetooth::read()
  63. {
  64. return(swSerial.read());
  65. }
  66. int EB_Bluetooth::available()
  67. {
  68. return(swSerial.available());
  69. }
  70. //make sure the state pin is connected to the Arduino. return 1 - connected, 0 - unconnected
  71. int EB_Bluetooth::connected()
  72. {
  73. /*String conSymb = "+CONNECTED";//if connected, "+CONNECTED" will be sent back from the Bluetooth
  74. String parSymb = "+PAIRABLE";//if pairable, "+PAIRABLE" will be sent back from the Bluetooth
  75. String recvBuf;
  76. char recvChar;
  77. if(available()){
  78. recvChar = read();
  79. recvBuf += recvChar;
  80. if(recvBuf.indexOf(conSymb) != -1){
  81. bluetoothState = BLUETOOTH_CONNECTED;
  82. }
  83. if(recvBuf.indexOf(parSymb) != -1){
  84. bluetoothState = BLUETOOTH_PAIRABLE;
  85. }
  86. }*/
  87. int readState = digitalRead(EB_statePin);
  88. if(readState != bluetoothState){
  89. bluetoothState = readState;
  90. delay(1);
  91. swSerial.flush();
  92. }
  93. return bluetoothState;
  94. }