Android单片机与蓝牙模块通信实例代码

参考路径:http://www.jb51.net/article/83349.htm

啦啦毕业了,毕业前要写毕业设计,需要写一个简单的蓝牙APP进行交互,通过参考网上资料,问题顺利搞定,下面小编把具体实现思路分享给大家,供大家参考。

1、Android蓝牙编程

  蓝牙3.0及以下版本编程需要使用UUID,UUID是通用唯一识别码(Universally Unique Identifier),这是一个软件构建的标准,也是被开源基金会组织应用在分布式计算环境领域的一部分。在蓝牙3.0及下一版本中,UUID被用于唯一标识一个服务,比如文件传输服务,串口服务、打印机服务等,如下:

1
2
3
4
5
6
7
8
9
10
11
#蓝牙串口服务
SerialPortServiceClass_UUID = '{00001101-0000-1000-8000-00805F9B34FB}'
LANAccessUsingPPPServiceClass_UUID = '{00001102-0000-1000-8000-00805F9B34FB}'
#拨号网络服务
DialupNetworkingServiceClass_UUID = '{00001103-0000-1000-8000-00805F9B34FB}'
#信息同步服务
IrMCSyncServiceClass_UUID = '{00001104-0000-1000-8000-00805F9B34FB}'
SDP_OBEXObjectPushServiceClass_UUID = '{00001105-0000-1000-8000-00805F9B34FB}'
#文件传输服务
OBEXFileTransferServiceClass_UUID = '{00001106-0000-1000-8000-00805F9B34FB}'
IrMCSyncCommandServiceClass_UUID = '{00001107-0000-1000-8000-00805F9B34FB}'

  蓝牙的连接有主从设备,提供服务的可以认为是从设备。主设备通过UUID访问从设备提供具有相同UUID的服务,从而建立客服端—服务器(C/S)模式。

2、编程步骤

Android使用蓝牙,需要获得权限,蓝牙权限获得代码如下:

1
2
3
<!-- 蓝牙权限 -->
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

获取本地蓝牙适配器,如果蓝牙未开启,开启蓝牙设备:

1
2
3
4
5
6
7
8
9
10
11
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
// Device does not support Bluetooth
return;
}
// 开启蓝牙
int REQUEST_ENABLE_BT = 1;
if (!bluetoothAdapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BT);
}

搜索已配对的蓝牙设备,并添加到已配对列表中:

1
2
3
4
5
6
// 查询配对设备
List<String> devices = new ArrayList<String>();
Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : bondedDevices) {
devices.add(device.getName() + "-" + device.getAddress());
}

搜索未配对蓝牙设备,并添加到未配对列表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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,服务器监听类如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
private class ConnectThread extends Thread {
private final String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB";
private final BluetoothSocket socket;
private final BluetoothDevice device;
public ConnectThread(BluetoothDevice device) {
this.device = device;
BluetoothSocket tmp = null;
try {
tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));
} catch (IOException e) {
e.printStackTrace();
}
this.socket = tmp;
}
public void run() {
bluetoothAdapter.cancelDiscovery();
try {
socket.connect();
connectedThread = new ConnectedThread(socket);
connectedThread.start();
} catch (IOException e) {
try {
socket.close();
} catch (IOException ee) {
ee.printStackTrace();
}
return;
}
//manageConnectedSocket(socket);
}
public void cancel() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

客户端与服务器端建立连接成功后,需要ConnectedThread类接收发送数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// 客户端与服务器建立连接成功后,用ConnectedThread收发数据
private class ConnectedThread extends Thread {
private final BluetoothSocket socket;
private final InputStream inputStream;
private final OutputStream outputStream;
public ConnectedThread(BluetoothSocket socket) {
this.socket = socket;
InputStream input = null;
OutputStream output = null;
try {
input = socket.getInputStream();
output = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
this.inputStream = input;
this.outputStream = output;
}
public void run() {
byte[] buff = new byte[1024];
int bytes;
while (true) {
try {
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();
break;
}
}
}
public void write(byte[] bytes) {
try {
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
public void cancel() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

  到此为止就是蓝牙开发的大致步骤,其中没有涉及到蓝牙客户端建立连接类,不过可查阅BLE和经典蓝牙Android开发。

3、毕设蓝牙APP介绍

  毕设蓝牙APP需要接收单片机通过蓝牙模块发送上来的数据,并且蓝牙APP也可以给单片机发送数据来进行控制。页面布局如下,一个是整体页面,一个是设置页面,测试手机是魅蓝note。因为毕设做的是十字路口红绿灯控制系统,所有页面布局有4个LED灯,分别代表路口的4个红绿灯,会根据时间不同显示不同的颜色(红/绿/黄),并且会显示倒计时,最后来一张红绿灯系统整体图。

activity_main.xml文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?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">
<TextView
android:id="@+id/notice_view"
android:layout_width="0dp"
android:layout_height="40dp"
android:text="蓝牙未开启"
android:layout_weight="3"/>
<TextView
android:id="@+id/notice_recv_view"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="3"/>
<TextView
android:id="@+id/notice_send_view"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="3"/>
<Button
android:id="@+id/turn_on_off"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="2"
android:text="ON"/>
</LinearLayout>
<TextView
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"/>
<TextView
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="+"/>
<TextView
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"/>
<TextView
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"/>
<TextView
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">
<TextView
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">
<Button
android:id="@+id/clear_recv_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="clear" />
<EditText
android:id="@+id/send_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="输入框,默认@#结尾"/>
<Button
android:id="@+id/send"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="send" />
</LinearLayout>
</RelativeLayout>

MainActivity.java文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package com.luoxn28.bluetooth;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
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 = null;
private ConnectThread connectThread = null;
private ConnectedThread connectedThread = null;
private TextView noticeView = null;
private Button turnOnOff = null;
private TextView led0, led1, led2, led3, led4;
ScrollView scrollView = null;
private TextView recvView = null;
private Button clearRecvView = null;
private EditText sendText = null;
private Button send = null;
@Override
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
return;
}
// 注册监听事件
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("蓝牙未开启");
}
else {
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 = false;
@Override
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();
break;
}
if (connectedThread == null) {
Toast.makeText(this, "未连接设备", Toast.LENGTH_SHORT).show();
break;
}
String turn_string = "1@#";
connectedThread.write(turn_string.getBytes());
if (isOn == false) {
isOn = true; // 打开了
turnOnOff.setText("OFF");
led1.setText("");
led2.setText("");
led3.setText("");
led4.setText("");
}
else {
isOn = false; // 关闭了
turnOnOff.setText("ON");
led1.setText("LED1");
led2.setText("LED2");
led3.setText("LED3");
led4.setText("LED4");
}
break;
case R.id.clear_recv_view: // 清空接收框
recvView.setText("");
break;
case R.id.send: // 发送数据,默认以"@#"结尾
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Toast.makeText(this, "蓝牙未开启", Toast.LENGTH_SHORT).show();
return;
}
if (connectedThread == null) {
Toast.makeText(this, "未连接设备", Toast.LENGTH_SHORT).show();
break;
}
String inputText = sendText.getText().toString() + "@#"; // 发送给单片机数据以"@#结尾",这样单片机知道一条数据发送结束
//Toast.makeText(MainActivity.this, inputText, Toast.LENGTH_SHORT).show();
connectedThread.write(inputText.getBytes());
break;
default:
break;
}
}
private android.os.Handler handler = new android.os.Handler() {
public void handleMessage(Message msg) {
Bundle bundle = null;
switch (msg.what) {
case RECV_VIEW:
if (isOn == false) {
isOn = true;
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("#")) {
break;
}
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 - num;
led1.setText("" + n);
led2.setText("" + n);
if (num < 28) {
led3.setBackgroundColor(Color.GREEN);
led4.setBackgroundColor(Color.GREEN);
}
else {
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);
}
else {
int n = 60 - num;
led3.setText("" + n);
led4.setText("" + n);
if (num < 58) {
led1.setBackgroundColor(Color.GREEN);
led2.setBackgroundColor(Color.GREEN);
}
else {
led1.setBackgroundColor(Color.YELLOW);
led2.setBackgroundColor(Color.YELLOW);
}
}
break;
case NOTICE_VIEW:
bundle = msg.getData();
String notice = bundle.getString("notice");
noticeView.setText(notice);
break;
default:
break;
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
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();
} else {
Toast.makeText(this, "蓝牙已开启", Toast.LENGTH_SHORT).show();
}
}
return true;
}
else if (id == R.id.show_devices) {
if (bluetoothAdapter != null) {
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(this, "蓝牙未开启", Toast.LENGTH_SHORT).show();
return true;
}
// 查询配对设备
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();
}
return true;
}
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();
return true;
}
// 查询配对设备 建立连接,只能连接第一个配对的设备
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();
break;
}
}
return super.onOptionsItemSelected(item);
}
private class ConnectThread extends Thread {
private final String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB";
private final BluetoothSocket socket;
private final BluetoothDevice device;
public ConnectThread(BluetoothDevice device) {
this.device = device;
BluetoothSocket tmp = null;
try {
tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));
} catch (IOException e) {
e.printStackTrace();
}
this.socket = tmp;
}
public void run() {
bluetoothAdapter.cancelDiscovery();
try {
socket.connect();
connectedThread = new ConnectedThread(socket);
connectedThread.start();
} catch (IOException e) {
try {
socket.close();
} catch (IOException ee) {
ee.printStackTrace();
}
return;
}
//manageConnectedSocket(socket);
}
public void cancel() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 客户端与服务器建立连接成功后,用ConnectedThread收发数据
private class ConnectedThread extends Thread {
private final BluetoothSocket socket;
private final InputStream inputStream;
private final OutputStream outputStream;
public ConnectedThread(BluetoothSocket socket) {
this.socket = socket;
InputStream input = null;
OutputStream output = null;
try {
input = socket.getInputStream();
output = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
this.inputStream = input;
this.outputStream = output;
}
public void run() {
StringBuilder recvText = new StringBuilder();
byte[] buff = new byte[1024];
int bytes;
Bundle tmpBundle = new Bundle();
Message tmpMessage = new Message();
tmpBundle.putString("notice", "连接成功");
tmpMessage.what = NOTICE_VIEW;
tmpMessage.setData(tmpBundle);
handler.sendMessage(tmpMessage);
while (true) {
try {
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);
continue;
}
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();
break;
}
}
}
public void write(byte[] bytes) {
try {
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
public void cancel() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

menu_main.xml文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="@+id/show_devices"
android:title="@string/show_devices"
android:orderInCategory="100"
app:showAsAction="never" />
<item
android:id="@+id/start_bluetooth"
android:title="@string/start_bluetooth"
android:orderInCategory="100"
app:showAsAction="never" />
<item
android:id="@+id/find_devices"
android:title="@string/find_devices"
android:orderInCategory="100"
app:showAsAction="never" />
<item
android:id="@+id/connect_devices"
android:title="@string/connect_devices"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>

strings.xml文件如下:

1
2
3
4
5
6
7
8
<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已经开发完成,亲测可用,如果有什么错误,欢迎评论指正谈论^_^。

到此,本文全部内容就给大家介绍完了,亲自测试过,代码安全可靠,放心实用,如果有任何问题欢迎给我留言,小编会及时回复的!

如对本文有疑问,请提交到交流社区,广大热心网友会为你解答!! 点击进入社区

Android单片机与蓝牙模块通信实例代码的更多相关文章

  1. Android蓝牙实例(和单片机蓝牙模块通信)

    最近做毕设,需要写一个简单的蓝牙APP进行交互,在网上也找了很多资料,终于给搞定了,这里分享一下^_^. 1.Android蓝牙编程 蓝牙3.0及以下版本编程需要使用UUID,UUID是通用唯一识别码 ...

  2. 51单片机 HC05蓝牙模块

    一.注意事项 1.烧写程序时,要把蓝牙tx,rx信号线拔掉,对烧写程序有影响. 2.执行HC05集命令时,均以\r\n结尾.串口中断若选择“发送新行”时,不用添加\r\n.原理相同. 二.准备软硬件 ...

  3. Android Studio 的蓝牙串口通信(附Demo源码下载)

    根据相关代码制作了一个开源依赖包,将以下所有的代码进行打包,直接调用即可完成所有的操作.详细说明地址如下,如果觉得有用可以GIthub点个Star支持一下: 项目官网 Kotlin版本说明文档 Jav ...

  4. Bluetooth篇 开发实例之九 和蓝牙模块通信

    首先,我们要去连接蓝牙模块,那么,我们只要写客户端的程序就好了,蓝牙模块就相当于服务端. 连接就需要UUID. #蓝牙串口服务SerialPortServiceClass_UUID = ‘{00001 ...

  5. android和javascript之间相互通信实例分析

    1.  AndroidManifest.xml中必须使用许可 "android.permission.INTERNET", 否则会出Web page not available错误 ...

  6. Android BLE开发之Android手机与BLE终端通信

    本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 近期穿戴设备发展得非常火.把相关技术也带旺了,当中一项是BLE(Bluetooth Low Energy).B ...

  7. 【Arduino】开发入门【十】Arduino蓝牙模块与Android实现通信

    [Arduino]开发入门[十]蓝牙模块 首先show一下新入手的蓝牙模块 蓝牙参数特点 1.蓝牙核心模块使用HC-06从模块,引出接口包括VCC,GND,TXD,RXD,预留LED状态输出脚,单片机 ...

  8. 【转】android蓝牙开发---与蓝牙模块进行通信--不错

    原文网址:http://www.cnblogs.com/wenjiang/p/3200138.html 近半个月来一直在搞android蓝牙这方面,主要是项目需要与蓝牙模块进行通信.开头的进展很顺利, ...

  9. android蓝牙开发---与蓝牙模块进行通信

    近半个月来一直在搞android蓝牙这方面,主要是项目需要与蓝牙模块进行通信.开头的进展很顺利,但因为蓝牙模块不在我这里,所以只能用手机测试.一开头就发现手机的蓝牙不能用,为了证明这点,我刷了四次不同 ...

随机推荐

  1. es的返回数据结构

    ES即简单又复杂,你可以快速的实现全文检索,又需要了解复杂的REST API.本篇就通过一些简单的搜索命令,帮助你理解ES的相关应用.虽然不能让你理解ES的原理设计,但是可以帮助你理解ES,探寻更多的 ...

  2. 源码:Java集合源码之:数组与链表(一)

    数组和链表是数据结构中最基本的部分. 数组 在java中,数组定义为一种基本类型,其可以通过下标获取到对应位置的数据.那么这种结构的数据,在内存中是怎么存放的呢? 数组在内存中是一段连续的存储单元,每 ...

  3. 应用程序与驱动程序通信 DeviceIoControl

    之前写过一篇关于通过DeviceIoControl函数来使应用程序与驱动程序通信的博客,这次再通过这个完整的代码来简要疏通总结一下. 这种通信方式,就是驱动程序和应用程序自定义一种IO控制码,然后调用 ...

  4. 涂抹mysql笔记-mysql字符集

    字符集:查看mysql数据库当前都支持哪些字符集:system@(none)>show character set;+----------+--------------------------- ...

  5. django 分页和中间件

    分页 Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views h ...

  6. [C#]WinForm 中 comboBox控件之数据绑定

    [C#]WinForm 中 comboBox控件之数据绑定 一.IList 现在我们直接创建一个List集合,然后绑定 IList<string> list = new List<s ...

  7. Alpha阶段项目规划

    Alpha阶段任务 概述 我们团队采访学长并听从学长的意见之后,决定根据第一版的phylab项目进行重构.但由于第一版的phylab项目在github上的代码仅仅只有alpha版本,我们接手之后进行了 ...

  8. Vim 命令、操作、快捷键

    打开单个文件:vim file 同时打开多个文件:vim file1 file2 file3 ... 在vim窗口中打开一个新文件 :  :open file 在新窗口中打开文件: :split fi ...

  9. 直接从硬盘安装centos7网址整理

    1.https://blog.csdn.net/happy_joker/article/details/52822025 注意:(1)第3步-->Linux引导安装-->软件选择--> ...

  10. leetcode234

    /** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNo ...