一、权限声明
在AndroidManifest.xml文件中manifest下添加以下代码:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
BLUETOOTH权限用于执行蓝牙通信(例如请求连接、接受连接、传输数据)。
BLUETOOTH_ADMIN权限用于初始化设备查找或控制蓝牙设置。
注意:如果你使用BLUETOOTH_ADMIN权限,那么必须拥有BLUETOOTH权限。
二、Activity的成员变量
在整个类中都可以被访问的变量,以下省略部分UI组件变量。
private BluetoothAdapter ba;
private ArrayList<String> lname = new ArrayList<>(); // 用于储存已匹配的蓝牙设备信息
private ArrayList<String> laddress = new ArrayList<>();
private String sdname;
private String sdaddress;
private BluetoothSocket bsocket = null;
private OutputStream ostream = null;
private InputStream istream = null;
private static final UUID SerialPort_UUID = UUID.fromString(
"00001101-0000-1000-8000-00805F9B34FB");
private Handler hd;
private ReceiveThread rt;<span style="white-space:pre"> </span>// 自定义的用于接收蓝牙消息的内部类
BluetoothAdapter代表本地蓝牙适配器(蓝牙无线电),是所有蓝牙交互的入口。
BluetoothSocket代表一个蓝牙socket的接口(和TCP Socket类似)。这是一个连接点,它允许一个应用与其他蓝牙设备通过InputStream和OutputStream交换数据。
UUID,通用唯一识别码。蓝牙串口服务的UUID码:SerialPortServiceClass_UUID = ‘{00001101-0000-1000-8000-00805F9B34FB}’。
三、获取蓝牙适配器并开启蓝牙功能
这个步骤一般在Activity的OnCreate()方法中实现,以下省略部分UI代码,并包含用于处理蓝牙接收线程传来的消息的Handler。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ba = BluetoothAdapter.getDefaultAdapter();<span style="white-space:pre"> </span>// 获取蓝牙适配器
hd = new Handler() {<span style="white-space:pre"> </span>// 用于处理蓝牙接收线程传来的消息的Handler
public void handleMessage(Message msg)
{
super.handleMessage(msg);
if(msg.what == 112) {
emessage.setText(Arrays.toString((byte[])msg.obj));
emessage.setEnabled(true);
}
}
};
// 检测到Android设备不支持蓝牙功能
if (ba == null) {
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.dev_nsup_bt),
Toast.LENGTH_SHORT).show();
}
// 当蓝牙功能未开启,询问开启
if (!ba.isEnabled()) {
Intent turnOn = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
}
}
四、搜索蓝牙设备
点击按钮,打开系统蓝牙设置界面,自行配对。按钮方法如下:
public void SearchOnClick(View Source) {
startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
}
五、获取已匹配设备的信息及连接蓝牙设备
// 蓝牙设备选择按钮的点击事件
public void ChooseDevOnClick(View Source) {
Set<BluetoothDevice> sdevices;
// 清空设备数据
lname.clear();
laddress.clear();
// 获取已匹配的蓝牙设备信息
sdevices = ba.getBondedDevices();
if (sdevices.size() > 0) {
for (BluetoothDevice btd : sdevices) {
lname.add(btd.getName());
laddress.add(btd.getAddress());
System.out.println(btd.getName() + btd.getAddress());
}
}
// 将设备信息放入String数字
int size = lname.size();
final String[] snames = new String[size];
final String[] saddresses = new String[size];
for (int i = 0; i < size; i++) {
snames[i] = lname.get(i);
saddresses[i] = laddress.get(i);
}
// 创建显示蓝牙设备列表的AlertDialog
AlertDialog.Builder ab = new AlertDialog.Builder(MainActivity.this);
ab.setTitle(getResources().getString(R.string.choose_bt_dev));
ab.setItems(snames, new DialogInterface.OnClickListener() {<span style="white-space:pre"> </span>// 设置列表项的点击事件
public void onClick(DialogInterface dialog, int which) {
// 获取点击的设备信息
sdname = snames[which];
sdaddress = saddresses[which];
// 与选择的设备进行蓝牙连接
BluetoothDevice device = ba.getRemoteDevice(sdaddress);
try {
bsocket = device.createRfcommSocketToServiceRecord(
SerialPort_UUID);
ba.cancelDiscovery();
bsocket.connect();
// 开启蓝牙接收线程
rt = new ReceiveThread(bsocket);<span style="white-space:pre"> </span>// 定义见步骤五
rt.start();
} catch (IOException e) {
// TODO: 显示失败提示
try {
bsocket.close();
bsocket = null;
} catch (IOException e2) {
e2.printStackTrace();
}
e.printStackTrace();
}
// 显示连接结果
bdevice.setText(sdname);
}
});
// 弹出显示AlertDialog
ab.create().show();
}
public class ReceiveThread extends Thread {
BluetoothSocket tsocket;<span style="white-space:pre"> </span>// 线程内部蓝牙socket,局部变量
public ReceiveThread(BluetoothSocket socket) {
this.tsocket = socket;
InputStream tistream = null;
try {
tistream = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
istream = tistream;
}
@Override
public void run() {
byte[] buffer = new byte[32];
String str;
// 线程一直运行
while (true) {
try {
istream.read(buffer);
str = new String(buffer,"UTF-8");
buffer = new byte[32];
Message msg = hd.obtainMessage(112,str);<span style="white-space:pre"> </span>// 把接收到的消息发送给Handler
hd.sendMessage(msg);
} catch (IOException e) {
try {
if (istream != null) {
istream.close();
}
break;
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
Thread.sleep(50);<span style="white-space:pre"> </span>// 线程休眠,休眠时长影响线程每次可接收的数据量,数据越长,需要的时间越长
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
1、传入字符串参数
public void SendStr(String str) {
byte[] bf = new byte[33];<span style="white-space:pre"> </span>// 按需字节数组初始化长度
bf = str.getBytes();
if((!str.equals("")) && (bsocket!=null)) {
try {
ostream = bsocket.getOutputStream();
ostream.write(bf);
ostream.write('\0'); // 发送一个结束标志符
} catch (IOException e) {
e.printStackTrace();
}
try {
if (ostream != null) {
ostream.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、传入字节数组参数
public static void SendBytes(byte[] bytes) {
if(bytes.length>0 && (bsocket!=null)) {
try {
ostream = bsocket.getOutputStream();
ostream.write(bytes);
ostream.write('\0');
} catch (IOException e) {
e.printStackTrace();
}
try {
if (ostream != null) {
ostream.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
八、退出应用
先关闭蓝牙socket,再关闭蓝牙功能。注意捕获异常。
// 退出按钮的点击事件
public void ExitOnClick(View Source) {
if(bsocket != null)
try {
bsocket.close();
}catch (IOException e) {
e.printStackTrace();
}
// Close Bluetooth
ba.disable();
MainActivity.this.finish();
}