码迷,mamicode.com
首页 > 移动开发 > 详细

开发手机与单片机通过蓝牙模块

时间:2018-07-22 16:55:42      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:length   margin   tag   mini   not   finish   字符串   之间   return   

1.xml布局文件

<?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="前进"
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="左转"
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="右转"
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="后退"
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="停止"
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; //手机蓝牙与蓝牙模块之间的socket

private OutputStream outStream = null; //发送指令的输出流
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"; // <==要连接的蓝牙设备MAC地址


/** 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() {
//前进
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: //按下了前进按钮
try {
outStream = btSocket.getOutputStream(); //通过socket 得到输出流

} catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}


message = "1"; //上位机与下位机约定的指令1表示前进

msgBuffer = message.getBytes(); //因为outputStream 只能传输字节,所以要把字符串指令编程字节流

try {
outStream.write(msgBuffer); //将指令写入输出流中。也就是写入socket中

} 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;
}


});
//后退
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;
}


});
//左转
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;

}
});
//右转
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;

}


});

//停止
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 is not available.", Toast.LENGTH_LONG).show();
//finish();
return;
}


if (!mBluetoothAdapter.isEnabled()) {
Toast.makeText(this, "Please enable your Bluetooth and re-run this program.", Toast.LENGTH_LONG).show();
finish();
return;

}


if (D)
Log.e(TAG, "+++ DONE 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 TO 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.修改手机权限manifest.XML
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
4.效果
技术分享图片

 

 

开发手机与单片机通过蓝牙模块

标签:length   margin   tag   mini   not   finish   字符串   之间   return   

原文地址:https://www.cnblogs.com/zengke556/p/9350187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!