利用安卓手机的蓝牙功能,使用蓝牙串口模块,可以建立嵌入式设备与安卓手机APP的蓝牙数据
收发通道,从而实现手机APP对嵌入式设备的无线控制和通信。建立蓝牙通信测试程序的过程如
下:
1. 使用Android Studio建立一个APP工程。
2. 修改xml,获取蓝牙权限。在AndroidManifest.xml文件中增加以下两行:
![安卓手机与蓝牙串口模块的通信 安卓手机与蓝牙串口模块的通信](https://i-blog.csdnimg.cn/blog_migrate/45106e36230e158e3955b9756273f0d1.jpeg)
3. 增加蓝牙通信相关的java代码。
3.1 导入相关的库。
3.2 定义要使用的变量。
3.3 获取手机蓝牙适配器的对象,获得目标蓝牙设备对象,建立连接,获得输入输出流。
![安卓手机与蓝牙串口模块的通信 安卓手机与蓝牙串口模块的通信](https://i-blog.csdnimg.cn/blog_migrate/12602874a7f2ead7bc6b58cd272155f4.jpeg)
3.4 收发一些数据,关闭连接。
![安卓手机与蓝牙串口模块的通信 安卓手机与蓝牙串口模块的通信](https://i-blog.csdnimg.cn/blog_migrate/3af44763301b1e69ff286f9526e32d3d.jpeg)
如果能够完成数据收发,接下来就可以建立自己需要的应用级蓝牙功能了。
本文所用的蓝牙模块:
![安卓手机与蓝牙串口模块的通信 安卓手机与蓝牙串口模块的通信](https://i-blog.csdnimg.cn/blog_migrate/5809a3f1b7f09aefb0a610e9d96cea38.jpeg)
使用的手机以及APP,点击Settings即启动一次蓝牙数据收发:
![安卓手机与蓝牙串口模块的通信 安卓手机与蓝牙串口模块的通信](https://i-blog.csdnimg.cn/blog_migrate/2e2533bd2f265137c62ea3462f716a75.jpeg)
PC通过蓝牙串口与手机APP收发的数据:
![安卓手机与蓝牙串口模块的通信 安卓手机与蓝牙串口模块的通信](https://i-blog.csdnimg.cn/blog_migrate/50805080a26d9911eff005f183003c25.jpeg)
--------------------------------------------------------------------------------------------------------------------
本文所述的蓝牙通信测试程序相关文件有两个,AndroidManifest.xml内容见第一个图,MyActivity.java内容如下:
package com.example.administrator.myapplication4;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
public class MyActivity extends Activity {
private BluetoothAdapter BA;
private Set pairedDevices;
private static String address = "00:06:71:00:6B:D0"; // <==应填写蓝牙串口模块的蓝牙地址。
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
private InputStream inStream = null;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
BA = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = BA.getRemoteDevice(address);
// pairedDevices = BA.getBondedDevices();
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
BA.cancelDiscovery();
try {
btSocket.connect();
outStream = btSocket.getOutputStream();
outStream.write(0x41);
outStream.write(0x61);
outStream.write(0x41);
inStream = btSocket.getInputStream();
int x = inStream.read();
outStream.write(x + 1);
} catch (IOException e) {
}
try {
btSocket.close();
} catch (IOException e2) {
// Log .e(TAG,"ON RESUME: Unable to close socket during connection failure", e2);
}
return true;
}
return super.onOptionsItemSelected(item);
}
}