Development of mobile phone and MCU via Bluetooth module

Source: Internet
Author: User

1.xml Layout file

<?xml version= "1.0" encoding= "Utf-8"?>
<android.support.constraint.constraintlayout xmlns:app= "Http://schemas.android.com/apk/res-auto"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/widget0"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Xmlns:android= "Http://schemas.android.com/apk/res/android"
tools:layout_editor_absolutey= "81DP"
tools:layout_editor_absolutex= "0DP" >

<button
Android:id= "@+id/btnf"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Forward"
Tools:layout_constrainttop_creator= "1"
Tools:layout_constraintright_creator= "1"
app:layout_constraintright_torightof= "Parent"
android:layout_margintop= "119DP"
Tools:layout_constraintleft_creator= "1"
app:layout_constraintleft_toleftof= "Parent"
app:layout_constrainttop_totopof= "Parent" ></Button>

<button
Android:id= "@+id/btnl"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_marginstart= "16DP"
Android:text= "left"
app:layout_constraintbaseline_tobaselineof= "@+id/btns"
Tools:layout_constraintbaseline_creator= "1"
Tools:layout_constraintleft_creator= "1"
app:layout_constraintleft_toleftof= "Parent"
android:layout_marginleft= "16DP" ></Button>

<button
Android:id= "@+id/btnr"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Turn Right"
Tools:layout_constraintright_creator= "1"
Android:layout_marginend= "6DP"
app:layout_constraintright_torightof= "Parent"
app:layout_constraintbaseline_tobaselineof= "@+id/btns"
Tools:layout_constraintbaseline_creator= "1"
android:layout_marginright= "6DP" ></Button>

<button
Android:id= "@+id/btnb"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Back"
Tools:layout_constrainttop_creator= "1"
Tools:layout_constraintright_creator= "1"
app:layout_constraintright_torightof= "@+id/btns"
android:layout_margintop= "58DP"
app:layout_constrainttop_tobottomof= "@+id/btns"
Tools:layout_constraintleft_creator= "1"
app:layout_constraintleft_toleftof= "@+id/btns" ></Button>

<button
Android:id= "@+id/btns"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Stop"
Tools:layout_constrainttop_creator= "1"
Tools:layout_constraintright_creator= "1"
app:layout_constraintright_torightof= "@+id/btnf"
android:layout_margintop= "64DP"
app:layout_constrainttop_tobottomof= "@+id/btnf"
Tools:layout_constraintleft_creator= "1"
app:layout_constraintleft_toleftof= "@+id/btnf" ></Button>
</android.support.constraint.ConstraintLayout>

2.MainActivity
Package com.example.administrator.bluetooth_cmd;

Import Android.bluetooth.BluetoothAdapter;
Import Android.bluetooth.BluetoothDevice;
Import Android.bluetooth.BluetoothSocket;
Import Android.os.Bundle;
Import android.support.v7.app.AppCompatActivity;
Import Android.util.Log;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.Toast;

Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.util.UUID;
public class Mainactivity extends Appcompatactivity {
private static final String TAG = "Thinbtclient";

Private static Final Boolean D = true;

Private Bluetoothadapter mbluetoothadapter = null;

Private Bluetoothsocket btsocket = null; The socket between the Bluetooth and Bluetooth modules on your phone

Private OutputStream outstream = null; Output stream of the send instruction
Button mbuttonf;


Button mbuttonb;
Button mbuttonl;
Button Mbuttonr;
Button mbuttons;

private static final UUID My_uuid = uuid.fromstring ("00001101-0000-1000-8000-00805F9B34FB");


private static String address = "00:0c:bf:09:4a:4a"; <== the MAC address of the Bluetooth device you want to connect to


/** called when the activity is first created. */

@Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mbluetoothadapter = Bluetoothadapter.getdefaultadapter ();

Btn_function ();
Init_bluetooth ();
}
private void Btn_function () {
Forward
mbuttonf = (Button) Findviewbyid (R.ID.BTNF);
Mbuttonf.setontouchlistener (New Button.ontouchlistener () {

@Override
public boolean OnTouch (View V, motionevent event) {
TODO auto-generated Method Stub
String message;
Byte[] Msgbuffer;
int action = Event.getaction ();
Switch (action) {
Case Motionevent.action_down://Press the Forward button
try {
OutStream = Btsocket.getoutputstream (); Get the output stream through the socket

} catch (IOException e) {
LOG.E (TAG, "on Resume:output stream creation failed.", e);
}


message = "1"; The upper computer and the lower machine agreed to the instruction 1 means forward

Msgbuffer = Message.getbytes (); Because OutputStream can only transmit bytes, the string instruction programming byte stream

try {
Outstream.write (Msgbuffer); Writes the instruction to the output stream. Which is written in the socket.

} catch (IOException e) {
LOG.E (TAG, "on resume:exception during write.", e);
}
Break

Case MOTIONEVENT.ACTION_UP://Release the Forward button, similar to the previous, just different instructions.
try {
OutStream = Btsocket.getoutputstream ();

} catch (IOException e) {
LOG.E (TAG, "on Resume:output stream creation failed.", e);
}


Message = "0";

Msgbuffer = Message.getbytes ();

try {
Outstream.write (Msgbuffer);

} catch (IOException e) {
LOG.E (TAG, "on resume:exception during write.", e);
}
Break
}
return false;
}


});
Back off
MBUTTONB = (Button) Findviewbyid (R.ID.BTNB);
Mbuttonb.setontouchlistener (New Button.ontouchlistener () {


@Override
public boolean OnTouch (View V, motionevent event) {
TODO auto-generated Method Stub
String message;
Byte[] Msgbuffer;
int action = Event.getaction ();
Switch (action) {
Case Motionevent.action_down:
try {
OutStream = Btsocket.getoutputstream ();

} catch (IOException e) {
LOG.E (TAG, "on Resume:output stream creation failed.", e);
}


Message = "3";

Msgbuffer = Message.getbytes ();

try {
Outstream.write (Msgbuffer);

} catch (IOException e) {
LOG.E (TAG, "on resume:exception during write.", e);
}
Break

Case MOTIONEVENT.ACTION_UP:
try {
OutStream = Btsocket.getoutputstream ();

} catch (IOException e) {
LOG.E (TAG, "on Resume:output stream creation failed.", e);
}


Message = "0";

Msgbuffer = Message.getbytes ();

try {
Outstream.write (Msgbuffer);

} catch (IOException e) {
LOG.E (TAG, "on resume:exception during write.", e);
}
Break
}

return false;
}


});
Left
Mbuttonl = (Button) Findviewbyid (R.ID.BTNL);
Mbuttonl.setontouchlistener (New Button.ontouchlistener () {

@Override
public boolean OnTouch (View V, motionevent event) {
TODO auto-generated Method Stub
String message;
Byte[] Msgbuffer;
int action = Event.getaction ();
Switch (action) {
Case Motionevent.action_down:
try {
OutStream = Btsocket.getoutputstream ();

} catch (IOException e) {
LOG.E (TAG, "on Resume:output stream creation failed.", e);
}


Message = "2";

Msgbuffer = Message.getbytes ();

try {
Outstream.write (Msgbuffer);

} catch (IOException e) {
LOG.E (TAG, "on resume:exception during write.", e);
}
Break

Case MOTIONEVENT.ACTION_UP:
try {
OutStream = Btsocket.getoutputstream ();

} catch (IOException e) {
LOG.E (TAG, "on Resume:output stream creation failed.", e);
}


Message = "0";

Msgbuffer = Message.getbytes ();

try {
Outstream.write (Msgbuffer);

} catch (IOException e) {
LOG.E (TAG, "on resume:exception during write.", e);
}
Break
}

return false;

}
});
Turn right
Mbuttonr = (Button) Findviewbyid (R.ID.BTNR);
Mbuttonr.setontouchlistener (New Button.ontouchlistener () {

@Override
public boolean OnTouch (View V, motionevent event) {
TODO auto-generated Method Stub
String message;
Byte[] Msgbuffer;
int action = Event.getaction ();
Switch (action) {
Case Motionevent.action_down:
try {
OutStream = Btsocket.getoutputstream ();

} catch (IOException e) {
LOG.E (TAG, "on Resume:output stream creation failed.", e);
}


Message = "4";

Msgbuffer = Message.getbytes ();

try {
Outstream.write (Msgbuffer);

} catch (IOException e) {
LOG.E (TAG, "on resume:exception during write.", e);
}
Break

Case MOTIONEVENT.ACTION_UP:
try {
OutStream = Btsocket.getoutputstream ();

} catch (IOException e) {
LOG.E (TAG, "on Resume:output stream creation failed.", e);
}


Message = "0";

Msgbuffer = Message.getbytes ();

try {
Outstream.write (Msgbuffer);

} catch (IOException e) {
LOG.E (TAG, "on resume:exception during write.", e);
}
Break
}

return false;

}


});

Stop it
Mbuttons = (Button) Findviewbyid (R.ID.BTNS);
Mbuttons.setontouchlistener (New Button.ontouchlistener () {

@Override
public boolean OnTouch (View V, motionevent event) {
TODO auto-generated Method Stub
if (event.getaction () = = Motionevent.action_down)
try {
OutStream = Btsocket.getoutputstream ();

} catch (IOException e) {
LOG.E (TAG, "on Resume:output stream creation failed.", e);
}


String message = "0";

byte[] Msgbuffer = Message.getbytes ();

try {
Outstream.write (Msgbuffer);

} catch (IOException e) {
LOG.E (TAG, "on resume:exception during write.", e);
}
return false;
}

});
}
private void Init_bluetooth ()
{
if (D) log.e (TAG, "+ + + on CREATE + + +");

Mbluetoothadapter = Bluetoothadapter.getdefaultadapter ();

if (Mbluetoothadapter = = null) {
Toast.maketext (This, "Bluetooth was not available.", Toast.length_long). Show ();
Finish ();
Return
}


if (!mbluetoothadapter.isenabled ()) {
Toast.maketext (This, "* * Enable your Bluetooth and re-run this program.", Toast.length_long). Show ();
Finish ();
Return

}


if (D)
LOG.E (TAG, "+ + + do in on CREATE, GOT LOCAL BT ADAPTER + + + + +");

}


@Override

public void OnStart () {

Super.onstart ();

if (D) log.e (TAG, "+ + on START + +");
}


@Override

public void Onresume () {

Super.onresume ();
if (D) {
LOG.E (TAG, "+ on RESUME +");
LOG.E (TAG, "+ About to attempt CLIENT CONNECT +");

}

Bluetoothdevice device = Mbluetoothadapter.getremotedevice (address);

try {

Btsocket = Device.createrfcommsockettoservicerecord (My_uuid);

} catch (IOException e) {

LOG.E (TAG, "on Resume:socket creation failed.", e);

}
Mbluetoothadapter.canceldiscovery ();
try {

Btsocket.connect ();

LOG.E (TAG, "on RESUME:BT connection established, data transfer link open.");

} catch (IOException e) {

try {
Btsocket.close ();

} catch (IOException E2) {

Log. E (TAG, "on resume:unable to close socket during connection failure", E2);
}

}


Create A data stream so we can talk to server.

if (D)
LOG.E (TAG, "+ about-SAY SOMETHING to SERVER +");
/* Try {
OutStream = Btsocket.getoutputstream ();

} catch (IOException e) {
LOG.E (TAG, "on Resume:output stream creation failed.", e);
}


String message = "1";

byte[] Msgbuffer = Message.getbytes ();

try {
Outstream.write (Msgbuffer);

} catch (IOException e) {
LOG.E (TAG, "on resume:exception during write.", e);
}
*/

}


@Override

public void OnPause () {

Super.onpause ();


if (D)
LOG.E (TAG, "-on PAUSE-");
if (OutStream! = null) {
try {
Outstream.flush ();
} catch (IOException e) {
LOG.E (TAG, "on pause:couldn ' t flush output stream.", e);
}

}


try {
Btsocket.close ();
} catch (IOException E2) {
LOG.E (TAG, "on pause:unable to close socket.", E2);
}

}


@Override

public void OnStop () {

Super.onstop ();

if (D) log.e (TAG, "--on STOP--");

}


@Override

public void OnDestroy () {

Super.ondestroy ();

if (D) log.e (TAG, "---on DESTROY---");

}
}
3. Modify Phone Permissionsmanifest. XML
<uses-permission android:name= "Android.permission.BLUETOOTH"/>
<uses-permission android:name= "Android.permission.BLUETOOTH_ADMIN"/>
4. Effects

Development of mobile phone and MCU via Bluetooth module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.