
作者:邯郸市拓航金属制品制造有限公司 来源: 邯郸市拓航金属制品制造有限公司 日期:2026-11-30 11:33:49
Android蓝牙通信是蓝例指在Android设备之间或Android设备与(yu)蓝牙外设之间进行数据传输的过程,通过蓝牙通信,牙通(tong)可以实现设备之(zhi)间的信实无线连接(jie)和数据传输。(图片来源网络,蓝例侵删)
1、蓝牙协议:蓝牙协议是信实一种短距离无线通信协议,主要用于(yu)在设备之间传输数据。蓝例

2、牙通蓝牙设备:蓝牙设备是信实指支持蓝牙协议的设备,包括手机、蓝例平板电脑、牙通笔记本电脑等。信实

3、蓝牙适配器:蓝牙适配器是用于实现蓝牙设备与其他设备之间通信的硬件设备。

4、蓝牙服务:蓝牙服务是一组操作,用于实现特定功能(neng),如音频传输、文件传输等。
5、蓝牙客户端:蓝牙客户端是使(shi)用(yong)蓝牙服务的应用程序。
6、蓝牙服务器:蓝牙服务器是提供蓝牙服务的应用程序。
本实例将演示如何在Android设备之间进行蓝牙通信,实现文件的发送和接(jie)收。
(图(tu)片来源网络,侵删)1、添加蓝牙权限
在AndroidManifest.xml文件中添加以下权限:
<usespermission android:name="android.permission.BLUETOOTH" /><usespermission android:name="android.permission.BLUETOOTH_ADMIN" /><usespermission android:name="android.permission.ACCESS_FINE_LOCATION" />2、开启蓝牙
在(zai)MainActivity.java文件中,编写代码开启蓝牙:
private BluetoothAdapter mBluetoothAdapter;private boolean mBluetoothEnabled = false;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { // 设备不支持蓝牙功能 Toast.makeText(this, "设备不支持蓝牙功能", Toast.LENGTH_SHORT).show(); finish(); } else { if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } else { mBluetoothEnabled = true; } }}3、搜索附近的蓝牙设备
在MainActivity.java文件中,编写代码搜索附近的蓝牙设备:
private final static int REQUEST_DISCOVER_BT = 1;private BluetoothAdapter mBluetoothAdapter;private ArrayAdapter<String> mArrayAdapter;private ListView mListView;private Set<BluetoothDevice> mPairedDevices;private ArrayAdapter<String> mArrayAdapterPairedDevices;private ListView mListViewPairedDevices;private BluetoothDevice mDevice;private BluetoothSocket mSocket;private InputStream mmInStream;private OutputStream mmOutStream;private byte[] buffer;private boolean isConnected = false;private ConnectThread mConnectThread;private ConnectedThread mConnectedThread;private int bufferSize = 1024; // 设置缓冲区大小,单位为字节private static final String DEVICE_NAME = "MyDevice"; // 设置要连接的蓝牙设备名称,需要与(yu)目标设备的广(guang)播名称一致private static final UUID MY_UUID = UUID.fromString("0000110100001000800000805F9B34FB"); // 设置UUID,需要(yao)与(yu)目标设备的UUID一致,可以通过获取目标设备的ServiceRecord来获取UUID信息private static final String DEVICE_ADDRESS = "00:11:22:33:44:55"; // 设置要连接的蓝牙设备的MAC地址,需要与(yu)目标设备的MAC地址一致,可以通过扫描附近的蓝牙设备来获取MAC地址信息private static final int SCAN_PERIOD = 10000; // 设置扫描周期,单位(wei)为毫秒,默认(ren)为10秒,可以根据需要调整扫描周期以减少资源消耗和提高扫描效率private BluetoothLeScanner mBluetoothLeScanner; // 声明一个BluetoothLeScanner对象,用于(yu)执行低功耗蓝牙扫描任务,需要导入android.bluetooth.le包中的类和方法来实(shi)现低功耗蓝牙扫描功能,具体实(shi)现方法可以参考官方文档或其他相关(guan)资料。(图(tu)片来源网络,侵删)下面是一个关于Android蓝牙通信的基本实(shi)例的介绍,介绍列出了主要的步骤、功能以及对应的代码示例。
| 步骤 | 功能 | 代码示例 |
| 1. 检查蓝牙支持 | 检查设备是否支持蓝牙功能 | “`java
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
// 设备不(bu)支持蓝牙
| 2. 获取BluetoothAdapter | 获取系统蓝牙适配器(qi) | “`java
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
“` |
| 3. 启动(dong)蓝牙 | 请求用户开启蓝牙(需要权限) | “`java
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
“` |
| 4. 查找配对设备 | 查找已经配对的设备 | “`java
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
// 添加到设备列表
}
“` |
| 5. 搜索新(xin)设备 | 启动设备搜索过程(需(xu)要权限) | “`java
mBluetoothAdapter.startDiscovery();
“` |
| 6. 广播接收器 | 接收搜索结果 | “`java
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
}
}
};
“` |
| 7. 绑定设备 | 创建连接到指定设备的(de)Socket | “`java
BluetoothDevice mmDevice = …; // 获取你想要连接的设备实例
try {
mmSocket = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
// Socket创建失败
“` |
| 8. 连(lian)接设备 | 连接到绑定的Socket | “`java
mmSocket.connect();
“` |
OutputStream mmOutputStream = mmSocket.getOutputStream();
InputStream mmInputStream = mmSocket.getInputStream();
// 发送数据(ju)
mmOutputStream.write(string.getBytes());
// 接收数据(ju)
byte[] buffer = new byte[1024];
int bytes = mmInputStream.read(buffer);
String readMessage = new String(buffer, 0, bytes);
“` |
| 10. 关闭连接 | 关闭Socket,释放资源 | “`java
try {
mmSocket.close();
} catch (IOException e) {
// 关闭Socket失败
“` |
请注意,在实际(ji)开(kai)发中,您还(hai)需要处理(li)权限问题、线程管(guan)理、连接异常(chang)处理等(deng),对于蓝牙通信,您还需要(yao)定义一个(ge)UUID,这个UUID用于(yu)标(biao)识应用程序提供的蓝牙服务。
上(shang)述代码示例仅供参考,需要结合具体的Android项目来编写和调整,在实际应用(yong)中,您可能(neng)还需要考虑蓝牙通信的安全性,比如使用加密传输等。