Connecting Android and Arduino using bluetooth with Serial communication
- Easy to use
- Show paired and unpaired bluetooth device
- Can pairing bluetooth device
- Can send string to arduino with Serial
- Can receive string from arduino
- Android 4.1.2 and Arduino with HC-05 module
This application using bluetooth permission to connect to Arduino. This permission is added on library
<uses-permissionandroid:name="android.permission.BLUETOOTH"/>
<uses-permissionandroid:name="android.permission.BLUETOOTH_PRIVILEGED"/>
<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN"/>- Add this to your root build.gradle
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}- Add dependency
compile 'com.github.kido1611:ArduinoConnect:-SNAPSHOT'- Add line to your activity
publicclassMainActivityextendsAppCompatActivityimplementsArduinoConnectCallback{
privateArduinoConnectmArduinoConnect;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
...
mArduinoConnect = newArduinoConnect(this, getSupportFragmentManager());
mArduinoConnect.setCallback(this);
...
mArduinoConnect.showDialog(); // Show dialog bluetooth list to connectmArduinoConnect.sendMessage("Hello"); // Send string to arduino
}
@OverrideprotectedvoidonActivityResult(intrequestCode, intresultCode, Intentdata) {
super.onActivityResult(requestCode, resultCode, data);
...
if(mArduinoConnect!=null) // Must added to activitymArduinoConnect.onActivityResult(requestCode, resultCode, data);
}
@OverrideprotectedvoidonDestroy() {
super.onDestroy();
...
if(mArduinoConnect!=null) // Recommended to add, to reduce memory leakmArduinoConnect.disconnected(); // Disconnecting Android and Arduino
}
privatevoidshowLog(Stringmessage){
Log.d("ArduinoConnect", message);
}
@OverridepublicvoidonArduinoConnected(BluetoothDevicedevice) {
showLog("Connected");
}
@OverridepublicvoidonArduinoConnectFailed() {
showLog("Failed to connected");
}
@OverridepublicvoidonSerialTextReceived(Stringtext) { // Function to receive Serial text from ArduinoshowLog(text);
}
@OverridepublicvoidonBluetoothDeviceNotFound() {
showLog("Bluetooth device not found");
}
@OverridepublicvoidonBluetoothFailedEnabled() {
showLog("Bluetooth device not enabled");
}
}Show section paired and unpaired- Still imagine
The MIT License (MIT)
Copyright (c) 2016 Muhammad Abdusy Syukur
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.