You can get your connection and device assembling from another stuffs.Here I place My code which i developed. which may useful for You.
For Any Query comment below ..
//Arduino code..
#include <Servo.h>
int pos=0;
int servoPin=9;
int servoDelay=25;
Servo myPointer;
char data = 0; //Variable for storing received data
void setup()
{
Serial.begin(9600); //Sets the data rate in bits per second (baud) for serial data transmission
//pinMode(13, OUTPUT); //Sets digital pin 13 as output pin
myPointer.attach(servoPin);
}
void loop()
{
if(Serial.available() > 0) // Send data only when you receive data:
{
while(Serial.available()==0){}
pos=Serial.parseInt();
myPointer.write(pos);
}
}
//code FOR android application
AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.kiit1.audrinoblutooth">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml File
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.kiit1.audrinoblutooth.MainActivity">
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="184dp"
android:id="@+id/editText" />
<Button android:text="Move"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/button"
android:layout_marginBottom="82dp"
android:id="@+id/button2" />
<Button android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
for mainActivity package com.example.kiit1.audrinoblutooth;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.ParcelUuid;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private OutputStream outputStream;
private InputStream inStream;
@Override protected void onCreate(Bundle savedInstanceState) {
int position=0;
try {
BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter();
if (blueAdapter != null) {
if (blueAdapter.isEnabled()) {
Set<BluetoothDevice> bondedDevices = blueAdapter.getBondedDevices();
if (bondedDevices.size() > 0) {
Object[] devices = (Object[]) bondedDevices.toArray();
Toast.makeText(getBaseContext(), devices.length+"", Toast.LENGTH_LONG).show();
BluetoothDevice device = (BluetoothDevice) devices[position];
ParcelUuid[] uuids = device.getUuids();
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
socket.connect();
outputStream = socket.getOutputStream();
inStream = socket.getInputStream();
}
Log.e("error", "No appropriate paired devices.");
} else {
Log.e("error", "Bluetooth is disabled.");
}
}
}catch(Exception e){
e.printStackTrace();
Toast.makeText(getBaseContext(), "SETUP Failure", Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//Creating dialog box AlertDialog alert = builder.create();
//Setting the title manually alert.setTitle("Error");
String sErr=e.toString();
alert.setMessage(sErr);
alert.show();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button2=(Button)findViewById(R.id.button2);
final EditText editText=(EditText)findViewById(R.id.editText);
button2.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
String str= editText.getText().toString();
try {
write(str);
}catch(Exception e){
Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show();
}
}
});
}
public void write(String s) throws IOException {
outputStream.write(s.getBytes());
}
}
OUT PUT
It rotate degree you Send form your app..
No comments:
Post a Comment