手机蓝牙连接不求人,Android蓝牙开发入门必看教程

2026-10-29 0 阅读

蓝牙技术简介

蓝牙(Bluetooth)是一种无线技术标准,旨在实现固定和移动设备之间的短距离通信。它允许电子设备之间传输数据,如音频、视频、图像等。在Android开发中,蓝牙技术广泛应用于设备之间的数据交换,如智能家居、健康监测设备、车载系统等。

开发环境准备

1. 安装Android Studio

首先,您需要在您的计算机上安装Android Studio。Android Studio是官方推荐的Android开发工具,它提供了丰富的功能,包括代码编辑、调试、性能分析等。

2. 创建新项目

在Android Studio中,创建一个新的项目,选择“Empty Activity”模板。

3. 添加蓝牙权限

在项目的AndroidManifest.xml文件中,添加以下权限:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

这些权限允许您的应用访问蓝牙功能、管理蓝牙设备以及访问位置信息。

蓝牙基础概念

1. 蓝牙设备

蓝牙设备分为两类:主设备(Master)和从设备(Slave)。主设备负责发起连接,从设备则被连接。

2. 蓝牙服务

蓝牙服务是运行在设备上的应用程序组件,用于处理蓝牙通信。它允许其他设备发现并与之通信。

3. 蓝牙连接

蓝牙连接是通过配对(Pairing)过程建立的。配对过程涉及交换安全密钥,以确保通信的安全性。

蓝牙开发步骤

1. 扫描设备

在您的应用中,使用BluetoothAdapter类扫描附近的蓝牙设备:

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();

这会返回一个包含所有已配对设备的集合。

2. 连接设备

要连接到设备,您需要使用BluetoothDevice类:

BluetoothDevice device = devices.iterator().next();
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
socket.connect();

这会创建一个与服务记录(Service Record)对应的套接字,并连接到设备。

3. 通信

连接成功后,您可以使用套接字进行通信:

OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();

// 发送数据
outputStream.write("Hello, Bluetooth!");

// 接收数据
byte[] buffer = new byte[1024];
int bytesRead = inputStream.read(buffer);
String receivedMessage = new String(buffer, 0, bytesRead);

4. 断开连接

完成通信后,不要忘记断开连接:

socket.close();

注意事项

  1. 蓝牙通信可能受到距离、障碍物等因素的影响。
  2. 确保您的设备支持蓝牙功能。
  3. 在开发过程中,注意处理异常情况,如连接失败、数据传输错误等。

实战案例

以下是一个简单的蓝牙通信示例:

public class BluetoothActivity extends AppCompatActivity {

    private BluetoothAdapter bluetoothAdapter;
    private BluetoothSocket socket;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth);

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        // 扫描设备
        Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
        BluetoothDevice device = devices.iterator().next();

        // 连接设备
        try {
            socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
            socket.connect();

            // 通信
            OutputStream outputStream = socket.getOutputStream();
            InputStream inputStream = socket.getInputStream();

            outputStream.write("Hello, Bluetooth!".getBytes());
            byte[] buffer = new byte[1024];
            int bytesRead = inputStream.read(buffer);
            String receivedMessage = new String(buffer, 0, bytesRead);

            // 显示接收到的消息
            TextView textView = findViewById(R.id.textView);
            textView.setText(receivedMessage);

            // 断开连接
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这个示例演示了如何扫描设备、连接设备、发送消息、接收消息以及断开连接。

总结

通过本文,您应该对Android蓝牙开发有了基本的了解。在实际开发过程中,您需要根据具体需求调整代码,并注意处理各种异常情况。祝您在蓝牙开发领域取得成功!

分享到: