轻松上手Android蓝牙编程:从入门到实战,打造智能设备连接之道

2026-11-08 0 阅读

在当今的智能设备时代,蓝牙技术已经成为了连接各种设备的重要手段。对于Android开发者来说,掌握蓝牙编程技能不仅能够拓展应用场景,还能提升用户体验。本文将带你从入门到实战,轻松上手Android蓝牙编程,让你打造出属于自己的智能设备连接之道。

一、蓝牙技术简介

1.1 蓝牙技术概述

蓝牙(Bluetooth)是一种无线技术标准,旨在实现固定和移动设备之间的短距离通信。它基于2.4GHz的ISM频段,最大传输距离可达10米。蓝牙技术具有低成本、低功耗、易用性等特点,广泛应用于各种智能设备。

1.2 蓝牙协议栈

蓝牙协议栈是蓝牙技术的核心,包括以下层次:

  • 物理层:负责无线信号的调制和解调。
  • 链路层:负责数据的封装、传输和错误检测。
  • 逻辑链路控制与适配协议(L2CAP):提供面向连接和无连接的服务。
  • 传输控制协议/互联网协议(TCP/IP):提供端到端的可靠数据传输。
  • 串行端口 Profile(SPP):提供串行通信功能。

二、Android蓝牙编程基础

2.1 蓝牙设备扫描与连接

在Android中,要实现蓝牙设备扫描与连接,需要使用BluetoothAdapter和BluetoothDevice类。

// 获取蓝牙适配器
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// 扫描蓝牙设备
List<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();

// 连接蓝牙设备
BluetoothDevice device = devices.get(0);
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
socket.connect();

2.2 蓝牙数据传输

在连接到蓝牙设备后,可以通过BluetoothSocket进行数据传输。

// 发送数据
OutputStream outputStream = socket.getOutputStream();
outputStream.write("Hello, Bluetooth!".getBytes());

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

三、蓝牙编程实战

3.1 蓝牙低功耗(BLE)

相较于传统蓝牙技术,蓝牙低功耗(BLE)具有更低功耗、更短连接距离等特点,适用于物联网设备。

// 创建BLE扫描过滤器
BluetoothGattCallback callback = new BluetoothGattCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        // 处理扫描结果
    }

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        // 处理连接状态变化
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        // 处理服务发现
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        // 处理特征读取
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, boolean success) {
        // 处理特征写入
    }
};

BluetoothGatt gatt = device.connectGatt(context, false, callback);

// 读取特征值
BluetoothGattCharacteristic characteristic = gatt.getService(UUID.fromString("0000FF01-0000-1000-8000-00805F9B34FB")).getCharacteristic(UUID.fromString("0000FF02-0000-1000-8000-00805F9B34FB"));
gatt.readCharacteristic(characteristic);

// 写入特征值
BluetoothGattCharacteristic characteristic = gatt.getService(UUID.fromString("0000FF01-0000-1000-8000-00805F9B34FB")).getCharacteristic(UUID.fromString("0000FF02-0000-1000-8000-00805F9B34FB"));
characteristic.setValue("Hello, BLE!");
gatt.writeCharacteristic(characteristic);

3.2 蓝牙广播与接收

蓝牙广播与接收技术可以实现设备之间的数据交换。

// 创建广播管理者
BluetoothBroadcastManager bluetoothBroadcastManager = BluetoothBroadcastManager.getInstance(context);

// 注册广播接收器
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
bluetoothBroadcastManager.registerReceiver(receiver, filter);

// 发送广播
Intent intent = new Intent(BluetoothDevice.ACTION_FOUND);
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
sendBroadcast(intent);

四、总结

通过本文的学习,相信你已经对Android蓝牙编程有了初步的了解。在实际开发过程中,你需要根据具体需求选择合适的蓝牙技术,并熟练运用相关API进行编程。不断实践和总结,你将能够轻松上手Android蓝牙编程,打造出属于自己的智能设备连接之道。

分享到: