Android BLE 开发入门

随着物联网的发展,蓝牙低功耗(BLE,Bluetooth Low Energy)成为了设备间通信的重要方式。其低功耗、高效能的特点使得BLE广泛应用于医疗设备、健康监测、智能家居等各个领域。本文将带您了解如何在Android中开发BLE应用,提供代码示例,并展示状态图和旅行图以帮助理解开发过程。

1. BLE 的基本概念

BLE是一种无线通信技术,主要用于在设备间传输小量数据。与经典蓝牙相比,BLE在省电和连接时间上有显著优势。BLE设备通常被称为“外设”,而连接它们的设备通常称为“中心设备”。

BLE 的核心组件
  • 外设(Peripheral):提供数据的设备,例如心率监测器。
  • 中心(Central):接收数据的设备,例如智能手机。
  • 服务(Service):一组相关的功能,外设通过服务来组织数据。
  • 特征(Characteristic):服务的具体数据项。

2. Android BLE 开发环境准备

在开始开发前,确保您已经安装了以下开发工具:

  • Android Studio
  • Android SDK
  • Android Emulator(具有BLE支持)或一台实际的Android设备
权限申请

在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.
  • 2.
  • 3.

3. 发现 BLE 设备

下面是一个简单的代码示例,用于扫描可用的BLE设备。

private BluetoothAdapter bluetoothAdapter;
private final static int REQUEST_ENABLE_BT = 1;

private void initializeBluetooth() {
    BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();
    
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
}

private void startScanning() {
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(receiver, filter);
    bluetoothAdapter.startDiscovery();
}

private BroadcastReceiver receiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // 处理发现的设备
        }
    }
};
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

4. 连接 BLE 设备

在扫描到设备后,您可以连接到外设。以下是连接的基本步骤:

private void connectToDevice(BluetoothDevice device) {
    BluetoothGatt bluetoothGatt = device.connectGatt(this, false, gattCallback);
}
  • 1.
  • 2.
  • 3.

5. 数据传输

连接成功后,您可以通过GATT(通用属性配置文件)来进行数据传输。以下是如何发现服务和特征:

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothGatt.STATE_CONNECTED) {
            gatt.discoverServices();
        }
    }
    
    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        // 处理发现的服务和特征
        BluetoothGattService service = gatt.getService(YOUR_SERVICE_UUID);
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(YOUR_CHARACTERISTIC_UUID);
    }
    
    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, 
                                      BluetoothGattCharacteristic characteristic, 
                                      int status) {
        // 处理读取的数据
    }
};
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

6. 状态图

在Android BLE开发中,状态变化是一个重要的环节。以下是BLE连接状态的状态图,展示了不同的状态转移。

connect() connection successful disconnect() Disconnected Connecting Connected Disconnecting

7. 旅行图

下图展示了BLE设备的使用流程,从搜索设备、连接到读取数据的旅行路线:

BLE设备使用流程 用户 系统
搜索设备
搜索设备
用户
用户打开APP
用户打开APP
系统
系统扫描附近BLE设备
系统扫描附近BLE设备
连接设备
连接设备
用户
用户选择BLE设备
用户选择BLE设备
系统
系统开始连接
系统开始连接
读取数据
读取数据
系统
系统读取设备数据
系统读取设备数据
用户
用户查看数据
用户查看数据
BLE设备使用流程

8. 处理连接问题

在BLE开发中,连接问题是常见的。以下是一些常见的错误处理方法:

  • 确保设备在可发现状态。
  • 用户已打开蓝牙且授予了定位权限。
  • 网络状态良好。

结论

通过上述步骤,您可以初步搭建一个BLE应用。BLE技术使得设备间的通信变得更加高效,同时其应用场景也在不断扩展。随着您对BLE技术的深入了解,您将能够开发出更加复杂和有趣的应用。

在BLE开发的过程中,遇到问题时要及时参考官方文档和在线社区。希望本篇文章对您有所帮助,期待您在BLE开发中取得更多的成果!