蓝牙与单片机通信收发问题

HC-06从机模块基本使用方法
一、蓝牙和单片机怎么连接
如图,和就接地和电源&
RXD和分别接单片机的和
RXD:接收数据&
TXD:发送数据&
单片机的和在哪里呢?打开单片机的原理图
图示和就是和接口,把蓝牙的接口与它们连接好
这个时候蓝牙上面的指示灯是一闪一闪的(只要通电它就会一闪一闪)
一、用串口助手调试蓝牙(指令)
打开设备管理器,查看单片机与计算机连接的串口号是
打开串口助手,找到串口号,打开串口(还要注意波特率,默认,如果有改动而不记得的话只能一个个试过去啦)
输入&AT&,单击&发送&,收到&OK&
一、常用的指令
1.测试通讯
2.改蓝牙串口通讯波特率(这个改过之后必须要马上调整串口助手中的波特率!)
5----19200
......(反正就是数字,波特率)
8----115200
C----1382400
我们就用默认的吧,这个和之后串口收发的代码也有关系,就不调整啦
3.改蓝牙名称(可改为你给蓝牙取的名字)
4.改蓝牙配对密码
二、蓝牙与计算机不使用代码配对
打开控制面板查看设备和打印机右键添加设备
选中后单击下一步(我改过蓝牙名的,原来默认是linvor,不过这个买的蓝牙不一样名字就不一样,相信能找到的)
等一会儿之后会跳出以下界面,选择&输入设备的配对码&这一项
输入刚刚设定的配对码,我设的是1234
稍等后配对成功。然后右键查看该设备属性
我的串口号是COM12,每个人可能不一样,记住这个串口号!
一、烧录通讯代码
把我上一篇写的串口通信的代码烧录进单片机。代码实现的功能是发送&&返回&&,否则返回&&。我这部分代码写的很简单,有需要可以自己调整。
注:烧录的时候一定要把蓝牙拔掉,因为蓝牙的连接会干扰数据线的通信!
我的代码中用的波特率是,晶振是的,如果其他的话需要调整代码!
二、使用蓝牙通信测试
1.重新将蓝牙与单片机进行连接。
特别要注意这个时候蓝牙的接单片机的,蓝牙的接单片机的!这是因为之前我们使用指令的时候用的是单片机上的数据线与计算机进行通信的,而单片机接收到什么数据即蓝牙接收到了什么数据,所以蓝牙与单片机的接线口是一致的。而我们用蓝牙进行通信时,蓝牙收到数据后将收到的数据发送给单片机,即蓝牙的发送端应接单片机的接收端,蓝牙的接收端应接单片机的发送端。(希望我这样讲能看的明白)
2.打开串口助手,选择串口号(即之前蓝牙的串口号),打开串口
这时蓝牙上的指示灯是常亮的,不会一闪一闪,表示蓝牙与计算机成功连接啦
3.发送&&,成功返回&&!
阅读(...) 评论()Android蓝牙实例(和单片机蓝牙模块通信) - Sun‘刺眼的博客 - 博客园
随笔 - 732, 文章 - 0, 评论 - 38, 引用 - 0
1、Android蓝牙编程
  蓝牙3.0及以下版本编程需要使用UUID,UUID是通用唯一识别码(Universally Unique Identifier),这是一个软件构建的标准,也是被开源基金会组织应用在分布式计算环境领域的一部分。在蓝牙3.0及下一版本中,UUID被用于唯一标识一个服务,比如文件传输服务,串口服务、打印机服务等,如下:
#蓝牙串口服务&SerialPortServiceClass_UUID = '{0-805F9B34FB}'&LANAccessUsingPPPServiceClass_UUID = '{0-805F9B34FB}'
#拨号网络服务&DialupNetworkingServiceClass_UUID = '{0-805F9B34FB}'
#信息同步服务&IrMCSyncServiceClass_UUID = '{0-805F9B34FB}'&SDP_OBEXObjectPushServiceClass_UUID = '{0-805F9B34FB}'
#文件传输服务&OBEXFileTransferServiceClass_UUID = '{0-805F9B34FB}'&IrMCSyncCommandServiceClass_UUID = '{0-805F9B34FB}'
  蓝牙的连接有主从设备,提供服务的可以认为是从设备。主设备通过UUID访问从设备提供具有相同UUID的服务,从而建立客服端&服务器(C/S)模式。
&2、编程步骤
Android使用蓝牙,需要获得权限,蓝牙权限获得代码如下:
&!-- 蓝牙权限 --&
&uses-permission android:name="android.permission.BLUETOOTH"/&
&uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/&
获取本地蓝牙适配器,如果蓝牙未开启,开启蓝牙设备:
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
// Device does not support Bluetooth
// 开启蓝牙
int REQUEST_ENABLE_BT = 1;
if (!bluetoothAdapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BT);
&搜索已配对的蓝牙设备,并添加到已配对列表中:
// 查询配对设备
List&String& devices = new ArrayList&String&();
Set&BluetoothDevice& bondedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : bondedDevices) {
devices.add(device.getName() + "-" + device.getAddress());
搜索未配对蓝牙设备,并添加到未配对列表:
mBluetoothAdapter.startDiscovery();
//开始收索 搜索接收函数:
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show
in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
// 收索接收函数需要注册:
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister
during onDestroy
如果是服务器端,需要建立监听,注意监听的是某个服务的UUID,服务器监听类如下:
private class ConnectThread extends Thread {
private final String MY_UUID = "0-805F9B34FB";
private final BluetoothS
private final BluetoothD
public ConnectThread(BluetoothDevice device) {
this.device =
BluetoothSocket tmp =
tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));
} catch (IOException e) {
e.printStackTrace();
this.socket =
public void run() {
bluetoothAdapter.cancelDiscovery();
socket.connect();
connectedThread = new ConnectedThread(socket);
connectedThread.start();
} catch (IOException e) {
socket.close();
} catch (IOException ee) {
ee.printStackTrace();
//manageConnectedSocket(socket);
public void cancel() {
socket.close();
} catch (IOException e) {
e.printStackTrace();
客户端与服务器端建立连接成功后,需要ConnectedThread类接收发送数据:
// 客户端与服务器建立连接成功后,用ConnectedThread收发数据
private class ConnectedThread extends Thread {
private final BluetoothS
private final InputStream inputS
private final OutputStream outputS
public ConnectedThread(BluetoothSocket socket) {
this.socket =
InputStream input =
OutputStream output =
input = socket.getInputStream();
output = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
this.inputStream =
this.outputStream =
public void run() {
byte[] buff = new byte[1024];
while (true) {
bytes = inputStream.read(buff);
String str = new String(buff, "ISO-8859-1");
str = str.substring(0, bytes);
Log.e("recv", str);
} catch (IOException e) {
e.printStackTrace();
public void write(byte[] bytes) {
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
public void cancel() {
socket.close();
} catch (IOException e) {
e.printStackTrace();
  到此为止就是蓝牙开发的大致步骤,其中没有涉及到蓝牙客户端建立连接类,不过可查阅。
&3、毕设蓝牙APP介绍
  毕设蓝牙APP需要接收单片机通过蓝牙模块发送上来的数据,并且蓝牙APP也可以给单片机发送数据来进行控制。页面布局如下,一个是整体页面,一个是设置页面,测试手机是魅蓝note。因为毕设做的是十字路口红绿灯控制系统,所有页面布局有4个LED灯,分别代表路口的4个红绿灯,会根据时间不同显示不同的颜色(红/绿/黄),并且会显示倒计时,最后来一张红绿灯系统整体图。
activity_main.xml文件如下:
&?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:layout_width="match_parent"
android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"&
&LinearLayout
android:id="@+id/linear_layout_top"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="40dp"&
android:id="@+id/notice_view"
android:layout_width="0dp"
android:layout_height="40dp"
android:text="蓝牙未开启"
android:layout_weight="3"/&
android:id="@+id/notice_recv_view"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="3"/&
android:id="@+id/notice_send_view"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="3"/&
android:id="@+id/turn_on_off"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="2"
android:text="ON"/&
&/LinearLayout&
android:id="@+id/led1"
android:layout_centerHorizontal="true"
android:layout_below="@+id/linear_layout_top"
android:layout_width="40dp"
android:layout_height="20dp"
android:gravity="center"
android:text="LED1"/&
android:id="@+id/led0"
android:layout_centerHorizontal="true"
android:layout_below="@+id/led1"
android:layout_width="40dp"
android:layout_height="20dp"
android:gravity="center"
android:text="+"/&
android:id="@+id/led3"
android:layout_below="@+id/led1"
android:layout_toLeftOf="@+id/led1"
android:layout_width="40dp"
android:layout_height="20dp"
android:gravity="center"
android:text="LED3"/&
android:id="@+id/led2"
android:layout_centerHorizontal="true"
android:layout_below="@+id/led3"
android:layout_width="40dp"
android:layout_height="20dp"
android:gravity="center"
android:text="LED2"/&
android:id="@+id/led4"
android:layout_below="@+id/led1"
android:layout_toRightOf="@+id/led1"
android:layout_width="40dp"
android:layout_height="20dp"
android:gravity="center"
android:text="LED4"/&
&ScrollView
android:id="@+id/scroll_view"
android:layout_below="@+id/led2"
android:layout_above="@+id/linear_layout_bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"&
android:id="@+id/recv_view"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content" /&
&/ScrollView&
&LinearLayout
android:id="@+id/linear_layout_bottom"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="50dp"&
android:id="@+id/clear_recv_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="clear" /&
android:id="@+id/send_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="输入框,默认@#结尾"/&
android:id="@+id/send"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="send" /&
&/LinearLayout&
&/RelativeLayout&
MainActivity.java文件如下:
package com.luoxn28.
import android.bluetooth.BluetoothA
import android.bluetooth.BluetoothD
import android.bluetooth.BluetoothS
import android.content.I
import android.graphics.C
import android.os.B
import android.os.M
import android.support.v7.app.ActionBarA
import android.view.M
import android.view.MenuI
import android.view.V
import android.widget.B
import android.widget.EditT
import android.widget.ScrollV
import android.widget.TextV
import android.widget.T
import java.io.IOE
import java.io.InputS
import java.io.OutputS
import java.util.ArrayL
import java.util.L
import java.util.S
import java.util.UUID;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
public static final int RECV_VIEW = 0;
public static final int NOTICE_VIEW = 1;
private BluetoothAdapter bluetoothAdapter =
private ConnectThread connectThread =
private ConnectedThread connectedThread =
private TextView noticeView =
private Button turnOnOff =
private TextView led0, led1, led2, led3, led4;
ScrollView scrollView =
private TextView recvView =
private Button clearRecvView =
private EditText sendText =
private Button send =
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取BluetoothAdapter
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
// Device does not support Bluetooth
// 注册监听事件
noticeView = (TextView) findViewById(R.id.notice_view);
turnOnOff = (Button) findViewById(R.id.turn_on_off);
led0 = (TextView) findViewById(R.id.led0);
led1 = (TextView) findViewById(R.id.led1);
led2 = (TextView) findViewById(R.id.led2);
led3 = (TextView) findViewById(R.id.led3);
led4 = (TextView) findViewById(R.id.led4);
scrollView = (ScrollView) findViewById(R.id.scroll_view);
recvView = (TextView) findViewById(R.id.recv_view);
clearRecvView = (Button) findViewById(R.id.clear_recv_view);
sendText = (EditText) findViewById(R.id.send_text);
send = (Button) findViewById(R.id.send);
turnOnOff.setOnClickListener(this);
clearRecvView.setOnClickListener(this);
send.setOnClickListener(this);
if (!bluetoothAdapter.isEnabled()) {
noticeView.setText("蓝牙未开启");
noticeView.setText("蓝牙已开启");
noticeView.setBackgroundColor(Color.GRAY);
led0.setBackgroundColor(Color.GRAY);
led1.setBackgroundColor(Color.GRAY);
led2.setBackgroundColor(Color.GRAY);
led3.setBackgroundColor(Color.GRAY);
led4.setBackgroundColor(Color.GRAY);
private boolean isOn =
public void onClick(View view) {
switch (view.getId()) {
case R.id.turn_on_off: // 发送'0'或者'1'都可以
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Toast.makeText(this, "蓝牙未开启", Toast.LENGTH_SHORT).show();
if (connectedThread == null) {
Toast.makeText(this, "未连接设备", Toast.LENGTH_SHORT).show();
String turn_string = "1@#";
connectedThread.write(turn_string.getBytes());
if (isOn == false) {
isOn = // 打开了
turnOnOff.setText("OFF");
led1.setText("");
led2.setText("");
led3.setText("");
led4.setText("");
isOn = // 关闭了
turnOnOff.setText("ON");
led1.setText("LED1");
led2.setText("LED2");
led3.setText("LED3");
led4.setText("LED4");
case R.id.clear_recv_view: // 清空接收框
recvView.setText("");
case R.id.send: // 发送数据,默认以"@#"结尾
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Toast.makeText(this, "蓝牙未开启", Toast.LENGTH_SHORT).show();
if (connectedThread == null) {
Toast.makeText(this, "未连接设备", Toast.LENGTH_SHORT).show();
String inputText = sendText.getText().toString() + "@#"; // 发送给单片机数据以"@#结尾",这样单片机知道一条数据发送结束
//Toast.makeText(MainActivity.this, inputText, Toast.LENGTH_SHORT).show();
connectedThread.write(inputText.getBytes());
private android.os.Handler handler = new android.os.Handler() {
public void handleMessage(Message msg) {
Bundle bundle =
switch (msg.what) {
case RECV_VIEW:
if (isOn == false) {
turnOnOff.setText("OFF");
bundle = msg.getData();
String recv = bundle.getString("recv");
recvView.append(recv + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN); // 滚动到底部
if (recv.isEmpty() || recv.contains(" ") || recv.contains("#")) {
int num = Integer.valueOf(recv) / 2; // 0-60s
if (num &= 20) {
led1.setText("");
led2.setText("");
led3.setText("");
led4.setText("");
led1.setBackgroundColor(Color.RED);
led2.setBackgroundColor(Color.RED);
led3.setBackgroundColor(Color.GREEN);
led4.setBackgroundColor(Color.GREEN);
else if (num & 30) {
int n = 30 -
led1.setText("" + n);
led2.setText("" + n);
if (num & 28) {
led3.setBackgroundColor(Color.GREEN);
led4.setBackgroundColor(Color.GREEN);
led3.setBackgroundColor(Color.YELLOW);
led4.setBackgroundColor(Color.YELLOW);
else if (num &= 50) {
led1.setText("");
led2.setText("");
led3.setText("");
led4.setText("");
led1.setBackgroundColor(Color.GREEN);
led2.setBackgroundColor(Color.GREEN);
led3.setBackgroundColor(Color.RED);
led4.setBackgroundColor(Color.RED);
int n = 60 -
led3.setText("" + n);
led4.setText("" + n);
if (num & 58) {
led1.setBackgroundColor(Color.GREEN);
led2.setBackgroundColor(Color.GREEN);
led1.setBackgroundColor(Color.YELLOW);
led2.setBackgroundColor(Color.YELLOW);
case NOTICE_VIEW:
bundle = msg.getData();
String notice = bundle.getString("notice");
noticeView.setText(notice);
public boolean onCreateOptionsMenu(Menu menu) {
// I this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.start_bluetooth) {
if (bluetoothAdapter != null) {
// 开启蓝牙
int REQUEST_ENABLE_BT = 1;
if (!bluetoothAdapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BT);
noticeView.setText("开启蓝牙成功");
//Toast.makeText(this, "开启蓝牙成功", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "蓝牙已开启", Toast.LENGTH_SHORT).show();
else if (id == R.id.show_devices) {
if (bluetoothAdapter != null) {
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(this, "蓝牙未开启", Toast.LENGTH_SHORT).show();
// 查询配对设备
List&String& devices = new ArrayList&String&();
Set&BluetoothDevice& bondedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : bondedDevices) {
devices.add(device.getName() + "-" + device.getAddress());
StringBuilder text = new StringBuilder();
for (String device : devices) {
text.append(device + "\n");
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
else if (id == R.id.find_devices) {
Toast.makeText(this, "该功能暂时不可用", Toast.LENGTH_SHORT).show();
else if (id == R.id.connect_devices) {
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Toast.makeText(this, "蓝牙未开启", Toast.LENGTH_SHORT).show();
// 查询配对设备 建立连接,只能连接第一个配对的设备
List&String& devices = new ArrayList&String&();
Set&BluetoothDevice& bondedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : bondedDevices) {
connectThread = new ConnectThread(device);
connectThread.start();
//Toast.makeText(this, "连接成功", Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
private class ConnectThread extends Thread {
private final String MY_UUID = "0-805F9B34FB";
private final BluetoothS
private final BluetoothD
public ConnectThread(BluetoothDevice device) {
this.device =
BluetoothSocket tmp =
tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));
} catch (IOException e) {
e.printStackTrace();
this.socket =
public void run() {
bluetoothAdapter.cancelDiscovery();
socket.connect();
connectedThread = new ConnectedThread(socket);
connectedThread.start();
} catch (IOException e) {
socket.close();
} catch (IOException ee) {
ee.printStackTrace();
//manageConnectedSocket(socket);
public void cancel() {
socket.close();
} catch (IOException e) {
e.printStackTrace();
// 客户端与服务器建立连接成功后,用ConnectedThread收发数据
private class ConnectedThread extends Thread {
private final BluetoothS
private final InputStream inputS
private final OutputStream outputS
public ConnectedThread(BluetoothSocket socket) {
this.socket =
InputStream input =
OutputStream output =
input = socket.getInputStream();
output = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
this.inputStream =
this.outputStream =
public void run() {
StringBuilder recvText = new StringBuilder();
byte[] buff = new byte[1024];
Bundle tmpBundle = new Bundle();
Message tmpMessage = new Message();
tmpBundle.putString("notice", "连接成功");
tmpMessage.what = NOTICE_VIEW;
tmpMessage.setData(tmpBundle);
handler.sendMessage(tmpMessage);
while (true) {
bytes = inputStream.read(buff);
String str = new String(buff, "ISO-8859-1");
str = str.substring(0, bytes);
// 收到数据,单片机发送上来的数据以"#"结束,这样手机知道一条数据发送结束
//Log.e("read", str);
if (!str.endsWith("#")) {
recvText.append(str);
recvText.append(str.substring(0, str.length() - 1)); // 去除'#'
Bundle bundle = new Bundle();
Message message = new Message();
bundle.putString("recv", recvText.toString());
message.what = RECV_VIEW;
message.setData(bundle);
handler.sendMessage(message);
recvText.replace(0, recvText.length(), "");
} catch (IOException e) {
e.printStackTrace();
public void write(byte[] bytes) {
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
public void cancel() {
socket.close();
} catch (IOException e) {
e.printStackTrace();
&menu_main.xml文件如下:
&menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"&
android:id="@+id/show_devices"
android:title="@string/show_devices"
android:orderInCategory="100"
app:showAsAction="never" /&
android:id="@+id/start_bluetooth"
android:title="@string/start_bluetooth"
android:orderInCategory="100"
app:showAsAction="never" /&
android:id="@+id/find_devices"
android:title="@string/find_devices"
android:orderInCategory="100"
app:showAsAction="never" /&
android:id="@+id/connect_devices"
android:title="@string/connect_devices"
android:orderInCategory="100"
app:showAsAction="never" /&
strings.xml文件如下:
&resources&
&string name="app_name"&BlueTooth&/string&
&string name="action_settings"&Settings&/string&
&string name="start_bluetooth"&开启蓝牙&/string&
&string name="show_devices"&查询配对设备&/string&
&string name="find_devices"&搜索设备&/string&
&string name="connect_devices"&连接设备&/string&
&/resources&
  &到这里整个APP已经开发完成,亲测可用,如果有什么错误,欢迎评论指正谈论^_^。
  3、《Android第一行代码》}

我要回帖

更多关于 蓝牙与单片机通信 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信