项目作者: KipCrossing

项目描述 :
Micropython code for the HC05 Bluetooth adaptor and an example application for android devices made specifically for the HC05.
高级语言: Java
项目地址: git://github.com/KipCrossing/PyBoard-HC05-Android.git
创建时间: 2017-09-13T07:21:41Z
项目社区:https://github.com/KipCrossing/PyBoard-HC05-Android

开源协议:Apache License 2.0

下载


PyBoard-HC05-Android

Micropython code for the HC05 Bluetooth adaptor and an example application for android devices made specifically for the HC05.

main.py

  1. # code by Kipling
  2. print("(Main program started)")
  3. import pyb
  4. # HC05 connection with the PyBoard
  5. #
  6. # HC06 - PyBoard
  7. # --------------
  8. # GND - GND
  9. # VCC - VCC
  10. # RXD - X3 (TX)
  11. # TXD - x4 (RX)
  12. blue_uart = pyb.UART(2, 9600)
  13. blue_uart.init(9600, bits=8, stop=1, parity=None)
  14. #pyb.repl_uart(blue_uart)
  15. while True:
  16. if blue_uart.any():
  17. line = blue_uart.readline()
  18. line = str(line,'utf-8')
  19. if line[-5:-1] == "BTM-":
  20. if line[-5:] == "BTM-U":
  21. print("UP")
  22. blue_uart.write("GO UP")
  23. # Write you code here
  24. elif line[-5:] == "BTM-D":
  25. print("DOWN")
  26. blue_uart.write("GO DOWN")
  27. # Write you code here
  28. elif line[-5:] == "BTM-L":
  29. print("LEFT")
  30. blue_uart.write("GO LEFT")
  31. # Write you code here
  32. elif line[-5:] == "BTM-R":
  33. print("RIGHT")
  34. blue_uart.write("GO RIGHT")
  35. # Write you code here
  36. else:
  37. print(line)
  38. blue_uart.write("You sent: " + line)
  39. # Write you code here

MainActivity.java

  1. package com.example.kipling.bletooth;
  2. import android.app.Activity;
  3. import android.bluetooth.BluetoothAdapter;
  4. import android.bluetooth.BluetoothDevice;
  5. import android.bluetooth.BluetoothSocket;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.os.Handler;
  9. import android.os.SystemClock;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.io.OutputStream;
  18. import java.util.Set;
  19. import java.util.UUID;
  20. import static android.content.ContentValues.TAG;
  21. public class MainActivity extends Activity {
  22. EditText bluetoothSend;
  23. private BluetoothAdapter bluetoothAdapter;
  24. private Set<BluetoothDevice>pairedDevices;
  25. private static UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  26. private OutputStream mmOutStream;
  27. private InputStream mmInStream;
  28. private BluetoothSocket mmSocket;
  29. private byte[] mmBuffer; // mmBuffer store for the stream
  30. private TextView textView;
  31. private Handler mHandler; // handler that gets info from Bluetooth service
  32. @Override
  33. protected void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.activity_main);
  36. bluetoothSend = (EditText) findViewById(R.id.bluetooth_word);
  37. textView = (TextView) findViewById(R.id.textView2);
  38. bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  39. mmSocket = null;
  40. on();
  41. connector();
  42. th.start();
  43. }
  44. // This is to turn on the bluetooth adapter if it is not already on
  45. public void on(){
  46. if (!bluetoothAdapter.isEnabled()) {
  47. Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  48. startActivityForResult(turnOn, 0);
  49. Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show();
  50. } else {
  51. Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();
  52. }
  53. }
  54. // Call this to turn off the bluetooth adapter (not used)
  55. public void off(View v){
  56. bluetoothAdapter.disable();
  57. Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show();
  58. }
  59. // If connection is not established on app startup (onCreate) try again with this method
  60. public void connect(View v){
  61. try{
  62. String name = "CONNECTED";
  63. byte[] bytes = name.getBytes();
  64. mmOutStream.write(bytes);
  65. }catch (IOException e){
  66. Toast.makeText(getApplicationContext(), "Connecting..." ,Toast.LENGTH_LONG).show();
  67. connector();
  68. }
  69. }
  70. public void connector(){
  71. OutputStream tmpOut = null;
  72. InputStream tmpIn = null;
  73. // Get list of paired devices
  74. BluetoothSocket tmp = null;
  75. String dname;
  76. pairedDevices = bluetoothAdapter.getBondedDevices();
  77. BluetoothDevice device = null;
  78. if(pairedDevices.size() >0) {
  79. for (BluetoothDevice bt : pairedDevices) {
  80. Log.d("TAG", bt.getName());
  81. dname = bt.getName();
  82. if (dname.equals("HC-05")) {
  83. device = bt;
  84. Log.d("TAG", "HC-05 PARED!!!");
  85. //Toast.makeText(getApplicationContext(), device.getName(), Toast.LENGTH_LONG).show();
  86. } else {
  87. Log.d("TAG", "Not HC-05");
  88. }
  89. }
  90. try {
  91. // MY_UUID is the app's UUID string, also used by the client code.
  92. tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
  93. } catch (IOException e) {
  94. Log.d("TAG", "Socket's listen() method failed", e);
  95. Toast.makeText(getApplicationContext(), "Error 1" ,Toast.LENGTH_LONG).show();
  96. }
  97. mmSocket = tmp;
  98. bluetoothAdapter.cancelDiscovery();
  99. try {
  100. // Connect to the remote device through the socket. This call blocks
  101. // until it succeeds or throws an exception.
  102. mmSocket.connect();
  103. Log.d("TAG", "Socket connected!!!!!");
  104. Toast.makeText(getApplicationContext(), "Connected" ,Toast.LENGTH_LONG).show();
  105. } catch (IOException connectException) {}
  106. try {
  107. tmpIn = mmSocket.getInputStream();
  108. } catch (IOException e) {
  109. Log.e(TAG, "Error occurred when creating input stream", e);
  110. }
  111. try {
  112. tmpOut = mmSocket.getOutputStream();
  113. } catch (IOException e) {
  114. Log.e(TAG, "Error occurred when creating output stream", e);
  115. Toast.makeText(getApplicationContext(), "Error 2" ,Toast.LENGTH_LONG).show();
  116. }
  117. mmOutStream = tmpOut;
  118. mmInStream = tmpIn;
  119. }else{
  120. Log.d("TAG", "No devices");
  121. Toast.makeText(getApplicationContext(), "HC-05 is not pared", Toast.LENGTH_LONG).show();
  122. }
  123. }
  124. // thread to listen to the input data from HC05 (not perfect)
  125. Thread th = new Thread(new Runnable() {
  126. public void run() {
  127. mmBuffer = new byte[4096];
  128. int numBytes; // bytes returned from read()
  129. // Keep listening to the InputStream until an exception occurs.
  130. while (true) {
  131. try {
  132. if(mmInStream.available()>2) {
  133. Log.d("TAG","mmInStream.available()>2");
  134. // Read from the InputStream.
  135. numBytes = mmInStream.read(mmBuffer);
  136. final String readMessage = new String(mmBuffer, 0, numBytes);
  137. runOnUiThread(new Runnable() {
  138. @Override
  139. public void run() {
  140. textView.setText(readMessage);
  141. }
  142. });
  143. Log.d("TAG", readMessage);
  144. }else{
  145. SystemClock.sleep(100);
  146. Log.d("TAG", "No Data");
  147. }
  148. } catch (IOException e) {
  149. Log.d("TAG", "Input stream was disconnected", e);
  150. break;
  151. }
  152. }
  153. }
  154. });
  155. // Receives commands from the UI to send to HC05
  156. public void write(View v) {
  157. String name = bluetoothSend.getText().toString();
  158. byte[] bytes = name.getBytes();
  159. Log.d("TAG","Pressed: "+name);
  160. try {
  161. mmOutStream.write(bytes);
  162. } catch (IOException e) {
  163. e.printStackTrace();
  164. Log.d("TAG","");
  165. Toast.makeText(getApplicationContext(), "Send failed" ,Toast.LENGTH_LONG).show();
  166. }
  167. }
  168. public void up(View v){
  169. String name = "BTM-U";
  170. byte[] bytes = name.getBytes();
  171. Log.d("TAG","Pressed: "+name);
  172. try {
  173. mmOutStream.write(bytes);
  174. } catch (IOException e) {
  175. e.printStackTrace();
  176. Log.d("TAG",""+e);
  177. Toast.makeText(getApplicationContext(), "Send failed" ,Toast.LENGTH_LONG).show();
  178. }
  179. }
  180. public void down(View v){
  181. String name = "BTM-D";
  182. byte[] bytes = name.getBytes();
  183. Log.d("TAG","Pressed: "+name);
  184. try {
  185. mmOutStream.write(bytes);
  186. } catch (IOException e) {
  187. e.printStackTrace();
  188. Log.d("TAG",""+e);
  189. Toast.makeText(getApplicationContext(), "Send failed" ,Toast.LENGTH_LONG).show();
  190. }
  191. }
  192. public void left(View v){
  193. String name = "BTM-L";
  194. byte[] bytes = name.getBytes();
  195. Log.d("TAG","Pressed: "+name);
  196. try {
  197. mmOutStream.write(bytes);
  198. } catch (IOException e) {
  199. e.printStackTrace();
  200. Log.d("TAG",""+e);
  201. Toast.makeText(getApplicationContext(), "Send failed" ,Toast.LENGTH_LONG).show();
  202. }
  203. }
  204. public void right(View v){
  205. String name = "BTM-R";
  206. byte[] bytes = name.getBytes();
  207. Log.d("TAG","Pressed: "+name);
  208. try {
  209. mmOutStream.write(bytes);
  210. } catch (IOException e) {
  211. e.printStackTrace();
  212. Log.d("TAG",""+e);
  213. Toast.makeText(getApplicationContext(), "Send failed" ,Toast.LENGTH_LONG).show();
  214. }
  215. }
  216. }

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity"
  8. android:transitionGroup="true">
  9. <TextView android:text="HC05"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:id="@+id/textview"
  13. android:textSize="35dp"
  14. android:layout_alignParentTop="true"
  15. android:layout_centerHorizontal="true" ></TextView>
  16. <ImageView
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:id="@+id/imageView"
  20. android:layout_below="@+id/textView"
  21. android:layout_centerHorizontal="true"
  22. android:theme="@style/Base.TextAppearance.AppCompat" ></ImageView>
  23. <Button
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="Connect"
  27. android:onClick="connect"
  28. android:id="@+id/button3"
  29. android:layout_below="@+id/textview"
  30. android:layout_alignLeft="@+id/buttonLEFT"
  31. android:layout_alignStart="@+id/buttonLEFT" ></Button>
  32. <Button
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="UP"
  36. android:onClick="up"
  37. android:id="@+id/buttonUP"
  38. android:layout_marginTop="11dp"
  39. android:layout_below="@+id/textview"
  40. android:layout_alignLeft="@+id/buttonDOWN"
  41. android:layout_alignStart="@+id/buttonDOWN" ></Button>
  42. <Button
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="DOWN"
  46. android:onClick="down"
  47. android:id="@+id/buttonDOWN"
  48. android:layout_below="@+id/buttonLEFT"
  49. android:layout_centerHorizontal="true" ></Button>
  50. <Button
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:text="LEFT"
  54. android:onClick="left"
  55. android:id="@+id/buttonLEFT"
  56. android:layout_below="@+id/buttonUP"
  57. android:layout_alignParentLeft="true"
  58. android:layout_alignParentStart="true"
  59. android:layout_marginLeft="11dp"
  60. android:layout_marginStart="11dp" ></Button>
  61. <Button
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:text="RIGHT"
  65. android:onClick="right"
  66. android:id="@+id/buttonRIGHT"
  67. android:layout_below="@+id/buttonUP"
  68. android:layout_alignParentRight="true"
  69. android:layout_alignParentEnd="true"
  70. android:layout_marginRight="13dp"
  71. android:layout_marginEnd="13dp" ></Button>
  72. <EditText
  73. android:layout_width="match_parent"
  74. android:layout_height="wrap_content"
  75. android:id="@+id/bluetooth_word"
  76. android:text="Input text"
  77. android:layout_centerVertical="true"
  78. android:layout_alignParentLeft="true"
  79. android:layout_alignParentStart="true"
  80. android:layout_toLeftOf="@+id/buttonSEND"
  81. android:layout_toStartOf="@+id/buttonSEND" ></EditText>
  82. <Button
  83. android:layout_width="wrap_content"
  84. android:layout_height="wrap_content"
  85. android:text="SEND"
  86. android:onClick="write"
  87. android:id="@+id/buttonSEND"
  88. android:layout_alignBottom="@+id/bluetooth_word"
  89. android:layout_alignLeft="@+id/buttonRIGHT"
  90. android:layout_alignStart="@+id/buttonRIGHT" ></Button>
  91. <TextView
  92. android:layout_width="wrap_content"
  93. android:layout_height="wrap_content"
  94. android:text="Received Data"
  95. android:id="@+id/textView2"
  96. android:textColor="#ff34ff06"
  97. android:textSize="25dp"
  98. android:layout_alignParentBottom="true"
  99. android:layout_alignRight="@+id/buttonRIGHT"
  100. android:layout_alignEnd="@+id/buttonRIGHT"
  101. android:layout_alignParentLeft="true"
  102. android:layout_alignParentStart="true"
  103. android:layout_below="@+id/bluetooth_word" ></TextView>
  104. </RelativeLayout>

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.kipling.bletooth">
  4. <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
  5. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity">
  14. <intent-filter>
  15. <action android:name="android.intent.action.MAIN" ></action>
  16. <category android:name="android.intent.category.LAUNCHER" ></category>
  17. </intent-filter>
  18. </activity>
  19. </application>
  20. </manifest>