{"id":13613354,"url":"https://github.com/KipCrossing/PyBoard-HC05-Android","last_synced_at":"2025-04-13T15:33:09.440Z","repository":{"id":80900178,"uuid":"103367261","full_name":"KipCrossing/PyBoard-HC05-Android","owner":"KipCrossing","description":"Micropython code for the HC05 Bluetooth adaptor and an example application for android devices made specifically for the HC05.","archived":false,"fork":false,"pushed_at":"2020-05-09T07:45:58.000Z","size":16402,"stargazers_count":12,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-06-11T17:27:37.361Z","etag":null,"topics":["android","android-device","hc05","hc06","micropython","pyboard"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KipCrossing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-13T07:21:41.000Z","updated_at":"2024-08-01T20:44:34.153Z","dependencies_parsed_at":null,"dependency_job_id":"1ca24ace-6b74-4f76-bbb7-c6c6d45a30f1","html_url":"https://github.com/KipCrossing/PyBoard-HC05-Android","commit_stats":{"total_commits":15,"total_committers":1,"mean_commits":15.0,"dds":0.0,"last_synced_commit":"37b154f8152b9bb7699e67f23831bed401fe9dc1"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KipCrossing%2FPyBoard-HC05-Android","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KipCrossing%2FPyBoard-HC05-Android/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KipCrossing%2FPyBoard-HC05-Android/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KipCrossing%2FPyBoard-HC05-Android/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KipCrossing","download_url":"https://codeload.github.com/KipCrossing/PyBoard-HC05-Android/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248736155,"owners_count":21153540,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","android-device","hc05","hc06","micropython","pyboard"],"created_at":"2024-08-01T20:00:44.934Z","updated_at":"2025-04-13T15:33:09.433Z","avatar_url":"https://github.com/KipCrossing.png","language":"Java","funding_links":[],"categories":["Libraries","精选驱动库"],"sub_categories":["Communications","通讯类"],"readme":"# PyBoard-HC05-Android\nMicropython code for the HC05 Bluetooth adaptor and an example application for android devices made specifically for the HC05.\n\n\n##  main.py\n\n```python\n# code by Kipling\nprint(\"(Main program started)\")\n\nimport pyb\n\n# HC05 connection with the PyBoard\n#\n# HC06 - PyBoard\n# --------------\n#  GND - GND\n#  VCC - VCC\n#  RXD - X3 (TX)\n#  TXD - x4 (RX)\n\n\nblue_uart = pyb.UART(2, 9600)\nblue_uart.init(9600, bits=8, stop=1, parity=None)\n#pyb.repl_uart(blue_uart)\n\nwhile True:\n    if blue_uart.any():\n        line = blue_uart.readline()\n        line = str(line,'utf-8')\n        if line[-5:-1] == \"BTM-\":\n            if line[-5:] == \"BTM-U\":\n                print(\"UP\")\n                blue_uart.write(\"GO UP\")\n\n                # Write you code here\n\n            elif line[-5:] == \"BTM-D\":\n                print(\"DOWN\")\n                blue_uart.write(\"GO DOWN\")\n\n                # Write you code here\n\n            elif line[-5:] == \"BTM-L\":\n                print(\"LEFT\")\n                blue_uart.write(\"GO LEFT\")\n\n                # Write you code here\n\n            elif line[-5:] == \"BTM-R\":\n                print(\"RIGHT\")\n                blue_uart.write(\"GO RIGHT\")\n\n                # Write you code here\n\n        else:\n            print(line)\n            blue_uart.write(\"You sent: \" + line)\n\n            # Write you code here\n\n\n```\n\n## MainActivity.java\n\n```java\n\npackage com.example.kipling.bletooth;\n\nimport android.app.Activity;\nimport android.bluetooth.BluetoothAdapter;\nimport android.bluetooth.BluetoothDevice;\n\nimport android.bluetooth.BluetoothSocket;\nimport android.content.Intent;\nimport android.os.Bundle;\nimport android.os.Handler;\nimport android.os.SystemClock;\nimport android.util.Log;\nimport android.view.View;\n\nimport android.widget.EditText;\n\nimport android.widget.TextView;\nimport android.widget.Toast;\n\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.io.OutputStream;\nimport java.util.Set;\nimport java.util.UUID;\n\nimport static android.content.ContentValues.TAG;\n\n\n\npublic class MainActivity extends Activity  {\n\n    EditText bluetoothSend;\n    private BluetoothAdapter bluetoothAdapter;\n    private Set\u003cBluetoothDevice\u003epairedDevices;\n\n    private static UUID MY_UUID = UUID.fromString(\"00001101-0000-1000-8000-00805F9B34FB\");\n\n    private OutputStream mmOutStream;\n    private InputStream mmInStream;\n\n    private BluetoothSocket mmSocket;\n    private byte[] mmBuffer; // mmBuffer store for the stream\n    private TextView textView;\n\n    private Handler mHandler; // handler that gets info from Bluetooth service\n\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n        bluetoothSend = (EditText) findViewById(R.id.bluetooth_word);\n\n        textView = (TextView) findViewById(R.id.textView2);\n\n        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();\n        mmSocket = null;\n\n\n\n        on();\n        connector();\n        th.start();\n\n    }\n\n\n\n\n    // This is to turn on the bluetooth adapter if it is not already on\n    public void on(){\n        if (!bluetoothAdapter.isEnabled()) {\n            Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);\n            startActivityForResult(turnOn, 0);\n            Toast.makeText(getApplicationContext(), \"Turned on\",Toast.LENGTH_LONG).show();\n        } else {\n            Toast.makeText(getApplicationContext(), \"Already on\", Toast.LENGTH_LONG).show();\n        }\n    }\n\n\n    // Call this to turn off the bluetooth adapter (not used)\n    public void off(View v){\n        bluetoothAdapter.disable();\n        Toast.makeText(getApplicationContext(), \"Turned off\" ,Toast.LENGTH_LONG).show();\n    }\n\n\n\n    // If connection is not established on app startup (onCreate) try again with this method\n    public void connect(View v){\n\n\n        try{\n            String name = \"CONNECTED\";\n            byte[] bytes = name.getBytes();\n            mmOutStream.write(bytes);\n        }catch (IOException e){\n            Toast.makeText(getApplicationContext(), \"Connecting...\" ,Toast.LENGTH_LONG).show();\n            connector();\n\n\n        }\n    }\n\n\n    public void connector(){\n\n        OutputStream tmpOut = null;\n        InputStream tmpIn = null;\n\n        // Get list of paired devices\n\n        BluetoothSocket tmp = null;\n\n        String dname;\n\n\n        pairedDevices = bluetoothAdapter.getBondedDevices();\n        BluetoothDevice device = null;\n        if(pairedDevices.size() \u003e0) {\n            for (BluetoothDevice bt : pairedDevices) {\n                Log.d(\"TAG\", bt.getName());\n                dname = bt.getName();\n                if (dname.equals(\"HC-05\")) {\n                    device = bt;\n                    Log.d(\"TAG\", \"HC-05 PARED!!!\");\n                    //Toast.makeText(getApplicationContext(), device.getName(), Toast.LENGTH_LONG).show();\n\n\n                } else {\n                    Log.d(\"TAG\", \"Not HC-05\");\n                }\n\n            }\n\n            try {\n                // MY_UUID is the app's UUID string, also used by the client code.\n                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);\n\n            } catch (IOException e) {\n                Log.d(\"TAG\", \"Socket's listen() method failed\", e);\n                Toast.makeText(getApplicationContext(), \"Error 1\" ,Toast.LENGTH_LONG).show();\n            }\n            mmSocket = tmp;\n\n\n            bluetoothAdapter.cancelDiscovery();\n\n\n\n            try {\n                // Connect to the remote device through the socket. This call blocks\n                // until it succeeds or throws an exception.\n                mmSocket.connect();\n\n\n                Log.d(\"TAG\", \"Socket connected!!!!!\");\n                Toast.makeText(getApplicationContext(), \"Connected\" ,Toast.LENGTH_LONG).show();\n            } catch (IOException connectException) {}\n\n\n\n            try {\n                tmpIn = mmSocket.getInputStream();\n            } catch (IOException e) {\n                Log.e(TAG, \"Error occurred when creating input stream\", e);\n            }\n\n\n            try {\n\n                tmpOut = mmSocket.getOutputStream();\n\n\n            } catch (IOException e) {\n                Log.e(TAG, \"Error occurred when creating output stream\", e);\n                Toast.makeText(getApplicationContext(), \"Error 2\" ,Toast.LENGTH_LONG).show();\n            }\n\n            mmOutStream = tmpOut;\n            mmInStream = tmpIn;\n\n\n\n        }else{\n            Log.d(\"TAG\", \"No devices\");\n            Toast.makeText(getApplicationContext(), \"HC-05 is not pared\", Toast.LENGTH_LONG).show();\n        }\n\n\n\n\n    }\n\n\n    // thread to listen to the input data from HC05 (not perfect)\n    Thread th = new Thread(new Runnable() {\n        public void run() {\n\n\n            mmBuffer = new byte[4096];\n            int numBytes; // bytes returned from read()\n\n            // Keep listening to the InputStream until an exception occurs.\n            while (true) {\n                try {\n                    if(mmInStream.available()\u003e2) {\n                        Log.d(\"TAG\",\"mmInStream.available()\u003e2\");\n\n                        // Read from the InputStream.\n                        numBytes = mmInStream.read(mmBuffer);\n\n\n\n                        final String readMessage = new String(mmBuffer, 0, numBytes);\n                        runOnUiThread(new Runnable() {\n                            @Override\n                            public void run() {\n                                textView.setText(readMessage);\n                            }\n                        });\n\n\n\n                        Log.d(\"TAG\", readMessage);\n                    }else{\n                        SystemClock.sleep(100);\n                        Log.d(\"TAG\", \"No Data\");\n                    }\n\n\n\n\n\n                } catch (IOException e) {\n                    Log.d(\"TAG\", \"Input stream was disconnected\", e);\n                    break;\n                }\n            }\n\n\n        }\n    });\n\n\n\n\n\n        // Receives commands from the UI to send to HC05\n    public void write(View v) {\n\n        String name = bluetoothSend.getText().toString();\n        byte[] bytes = name.getBytes();\n        Log.d(\"TAG\",\"Pressed: \"+name);\n\n        try {\n            mmOutStream.write(bytes);\n        } catch (IOException e) {\n            e.printStackTrace();\n            Log.d(\"TAG\",\"\");\n            Toast.makeText(getApplicationContext(), \"Send failed\" ,Toast.LENGTH_LONG).show();\n        }\n\n\n    }\n\n\n    public  void up(View v){\n        String name = \"BTM-U\";\n        byte[] bytes = name.getBytes();\n        Log.d(\"TAG\",\"Pressed: \"+name);\n        try {\n            mmOutStream.write(bytes);\n        } catch (IOException e) {\n            e.printStackTrace();\n            Log.d(\"TAG\",\"\"+e);\n            Toast.makeText(getApplicationContext(), \"Send failed\" ,Toast.LENGTH_LONG).show();\n        }\n\n    }\n\n    public  void down(View v){\n        String name = \"BTM-D\";\n        byte[] bytes = name.getBytes();\n        Log.d(\"TAG\",\"Pressed: \"+name);\n        try {\n            mmOutStream.write(bytes);\n        } catch (IOException e) {\n            e.printStackTrace();\n            Log.d(\"TAG\",\"\"+e);\n            Toast.makeText(getApplicationContext(), \"Send failed\" ,Toast.LENGTH_LONG).show();\n        }\n\n    }\n\n\n    public  void left(View v){\n        String name = \"BTM-L\";\n        byte[] bytes = name.getBytes();\n        Log.d(\"TAG\",\"Pressed: \"+name);\n        try {\n            mmOutStream.write(bytes);\n        } catch (IOException e) {\n            e.printStackTrace();\n            Log.d(\"TAG\",\"\"+e);\n            Toast.makeText(getApplicationContext(), \"Send failed\" ,Toast.LENGTH_LONG).show();\n        }\n\n    }\n\n    public  void right(View v){\n        String name = \"BTM-R\";\n        byte[] bytes = name.getBytes();\n        Log.d(\"TAG\",\"Pressed: \"+name);\n        try {\n            mmOutStream.write(bytes);\n        } catch (IOException e) {\n            e.printStackTrace();\n            Log.d(\"TAG\",\"\"+e);\n            Toast.makeText(getApplicationContext(), \"Send failed\" ,Toast.LENGTH_LONG).show();\n        }\n\n    }\n\n\n\n}\n\n\n```\n\n## activity_main.xml\n\n```xml\n\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cRelativeLayout\n    xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    xmlns:tools=\"http://schemas.android.com/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    tools:context=\".MainActivity\"\n    android:transitionGroup=\"true\"\u003e\n\n    \u003cTextView android:text=\"HC05\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:id=\"@+id/textview\"\n        android:textSize=\"35dp\"\n        android:layout_alignParentTop=\"true\"\n        android:layout_centerHorizontal=\"true\" /\u003e\n\n\n\n\n    \u003cImageView\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:id=\"@+id/imageView\"\n        android:layout_below=\"@+id/textView\"\n        android:layout_centerHorizontal=\"true\"\n        android:theme=\"@style/Base.TextAppearance.AppCompat\" /\u003e\n\n\n\n    \u003cButton\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Connect\"\n        android:onClick=\"connect\"\n        android:id=\"@+id/button3\"\n        android:layout_below=\"@+id/textview\"\n        android:layout_alignLeft=\"@+id/buttonLEFT\"\n        android:layout_alignStart=\"@+id/buttonLEFT\" /\u003e\n\n\n\n    \u003cButton\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"UP\"\n        android:onClick=\"up\"\n        android:id=\"@+id/buttonUP\"\n        android:layout_marginTop=\"11dp\"\n        android:layout_below=\"@+id/textview\"\n        android:layout_alignLeft=\"@+id/buttonDOWN\"\n        android:layout_alignStart=\"@+id/buttonDOWN\" /\u003e\n\n    \u003cButton\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"DOWN\"\n        android:onClick=\"down\"\n        android:id=\"@+id/buttonDOWN\"\n        android:layout_below=\"@+id/buttonLEFT\"\n        android:layout_centerHorizontal=\"true\" /\u003e\n    \u003cButton\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"LEFT\"\n        android:onClick=\"left\"\n        android:id=\"@+id/buttonLEFT\"\n        android:layout_below=\"@+id/buttonUP\"\n        android:layout_alignParentLeft=\"true\"\n        android:layout_alignParentStart=\"true\"\n        android:layout_marginLeft=\"11dp\"\n        android:layout_marginStart=\"11dp\" /\u003e\n\n    \u003cButton\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"RIGHT\"\n        android:onClick=\"right\"\n        android:id=\"@+id/buttonRIGHT\"\n        android:layout_below=\"@+id/buttonUP\"\n        android:layout_alignParentRight=\"true\"\n        android:layout_alignParentEnd=\"true\"\n        android:layout_marginRight=\"13dp\"\n        android:layout_marginEnd=\"13dp\" /\u003e\n\n\n\n    \u003cEditText\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:id=\"@+id/bluetooth_word\"\n        android:text=\"Input text\"\n        android:layout_centerVertical=\"true\"\n        android:layout_alignParentLeft=\"true\"\n        android:layout_alignParentStart=\"true\"\n        android:layout_toLeftOf=\"@+id/buttonSEND\"\n        android:layout_toStartOf=\"@+id/buttonSEND\" /\u003e\n\n    \u003cButton\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"SEND\"\n        android:onClick=\"write\"\n        android:id=\"@+id/buttonSEND\"\n        android:layout_alignBottom=\"@+id/bluetooth_word\"\n        android:layout_alignLeft=\"@+id/buttonRIGHT\"\n        android:layout_alignStart=\"@+id/buttonRIGHT\" /\u003e\n\n\n    \u003cTextView\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Received Data\"\n        android:id=\"@+id/textView2\"\n        android:textColor=\"#ff34ff06\"\n        android:textSize=\"25dp\"\n        android:layout_alignParentBottom=\"true\"\n        android:layout_alignRight=\"@+id/buttonRIGHT\"\n        android:layout_alignEnd=\"@+id/buttonRIGHT\"\n        android:layout_alignParentLeft=\"true\"\n        android:layout_alignParentStart=\"true\"\n        android:layout_below=\"@+id/bluetooth_word\" /\u003e\n\n\u003c/RelativeLayout\u003e\n\n```\n## AndroidManifest.xml\n\n```xml\n\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cmanifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    package=\"com.example.kipling.bletooth\"\u003e\n\n    \u003cuses-permission android:name=\"android.permission.BLUETOOTH\"/\u003e\n    \u003cuses-permission android:name=\"android.permission.BLUETOOTH_ADMIN\"/\u003e\n\n    \u003capplication\n        android:allowBackup=\"true\"\n        android:icon=\"@mipmap/ic_launcher\"\n        android:label=\"@string/app_name\"\n        android:roundIcon=\"@mipmap/ic_launcher_round\"\n        android:supportsRtl=\"true\"\n        android:theme=\"@style/AppTheme\"\u003e\n        \u003cactivity android:name=\".MainActivity\"\u003e\n            \u003cintent-filter\u003e\n                \u003caction android:name=\"android.intent.action.MAIN\" /\u003e\n\n                \u003ccategory android:name=\"android.intent.category.LAUNCHER\" /\u003e\n            \u003c/intent-filter\u003e\n        \u003c/activity\u003e\n    \u003c/application\u003e\n\n\u003c/manifest\u003e\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKipCrossing%2FPyBoard-HC05-Android","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKipCrossing%2FPyBoard-HC05-Android","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKipCrossing%2FPyBoard-HC05-Android/lists"}