当前位置:首页>安卓APP>安卓开发中的BLE服务器部分

安卓开发中的BLE服务器部分

  • 2026-11-02 17:47:07
安卓开发中的BLE服务器部分

一、内容概述

实现Android 设备作为 BLE 从机(Server),功能包括:

开启 BLE 广播,让其他设备能扫描到

搭建 GATT 服务结构(服务 + 读写特征 + 标准描述符)

处理客户端的读写请求

监听连接状态、MTU 协商

正确释放资源,避免崩溃


二、核心知识点(必看)

BLE 服务端 = 广播 + GATT Server

GattServer 通过 BluetoothManager 创建(非 Adapter)

CCC 描述符 UUID 固定:00002902-xxxx(通知必须用)

MTU 最大 512,客户端主动请求,服务端仅监听

所有读写请求必须调用 sendResponse () 回复

高版本安卓必须动态申请蓝牙权限


三、第一步:配置权限

(在AndroidManifest.xml中添加)必须完整添加,缺少任何一个都会导致崩溃或功能异常:
<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android">    <!-- BLE 基础权限 -->    <uses-permissionandroid:name="android.permission.BLUETOOTH" />    <uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN" />    <!-- Android 12+ 蓝牙新权限(必须) -->    <uses-permissionandroid:name="android.permission.BLUETOOTH_ADVERTISE" />    <uses-permissionandroid:name="android.permission.BLUETOOTH_CONNECT" />    <!-- 声明设备支持 BLE -->    <uses-feature        android:name="android.hardware.bluetooth_le"        android:required="true" />    <application        ...>        <activity            android:name=".BleServerActivity"            android:exported="true">            <intent-filter>                <actionandroid:name="android.intent.action.MAIN" />                <categoryandroid:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

四、第二步:完整可运行代码

创建 BleServerActivity.kt,直接复制即可运行:

import android.annotation.SuppressLintimport android.bluetooth.*import android.bluetooth.le.*import android.content.Contextimport android.os.Bundleimport android.os.ParcelUuidimport android.widget.Toastimport androidx.appcompat.app.AppCompatActivityimport java.util.UUIDclass BleServerActivity : AppCompatActivity() {    // ======================== 1. BLE UUID 定义 ========================    // 主服务UUID(可自定义)    private val SERVICE_UUID = UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb")    // 读特征UUID    private val READ_CHAR_UUID = UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb")    // 写特征UUID    private val WRITE_CHAR_UUID = UUID.fromString("0000ffe2-0000-1000-8000-00805f9b34fb")    // CCC描述符UUID(固定标准!不可修改)    private val CCC_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")    // ======================== 2. BLE核心对象 ========================    private var bluetoothManager: BluetoothManager? = null    private var bluetoothAdapter: BluetoothAdapter? = null    private var bleAdvertiser: BluetoothLeAdvertiser? = null    private var gattServer: BluetoothGattServer? = null    override funonCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_ble_server)        // 执行三大核心步骤        initBluetooth()       // 1. 初始化蓝牙        startBleAdvertising() // 2. 开启广播        createGattServer()    // 3. 创建GATT服务端    }    // ======================== 3. 初始化蓝牙 ========================    private funinitBluetooth() {        // 获取系统蓝牙管理器        bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager        // 获取蓝牙适配器        bluetoothAdapter = bluetoothManager?.adapter        // 自动打开蓝牙        if (bluetoothAdapter?.isEnabled == false) {            bluetoothAdapter?.enable()        }        // 获取广播器        bleAdvertiser = bluetoothAdapter?.bluetoothLeAdvertiser    }    // ======================== 4. 开启BLE广播 ========================    @SuppressLint("MissingPermission")    private funstartBleAdvertising() {        // 广播参数配置        val settings = AdvertiseSettings.Builder()            .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY) // 低延迟            .setConnectable(true) // 允许连接            .setTimeout(0) // 持续广播            .build()        // 广播数据(携带服务UUID)        val data = AdvertiseData.Builder()            .setIncludeDeviceName(true) // 广播设备名            .addServiceUuid(ParcelUuid(SERVICE_UUID)) // 广播服务UUID            .build()        // 启动广播        bleAdvertiser?.startAdvertising(settings, data, object : AdvertiseCallback() {            override funonStartSuccess(settingsInEffect: AdvertiseSettings?) {                showToast("广播启动成功,等待设备连接")            }            override funonStartFailure(errorCode: Int) {                showToast("广播启动失败,错误码:$errorCode")            }        })    }    // ======================== 5. 创建GATT服务端(核心!) ========================    @SuppressLint("MissingPermission")    private funcreateGattServer() {        // ✅ 正确方式:通过BluetoothManager创建GattServer        gattServer = bluetoothManager?.openGattServer(this, gattServerCallback)        // 1. 创建主服务        val service = BluetoothGattService(            SERVICE_UUID,            BluetoothGattService.SERVICE_TYPE_PRIMARY        )        // 2. 创建【可读特征】        val readChar = BluetoothGattCharacteristic(            READ_CHAR_UUID,            BluetoothGattCharacteristic.PROPERTY_READ, // 属性:可读            BluetoothGattCharacteristic.PERMISSION_READ // 权限:可读        )        // 3. 创建【可写+可通知特征】        val writeChar = BluetoothGattCharacteristic(            WRITE_CHAR_UUID,            BluetoothGattCharacteristic.PROPERTY_WRITE or BluetoothGattCharacteristic.PROPERTY_NOTIFY,            BluetoothGattCharacteristic.PERMISSION_WRITE        )        // 4. 添加标准CCC描述符(通知功能必须)        val descriptor = BluetoothGattDescriptor(            CCC_DESCRIPTOR_UUID,            BluetoothGattDescriptor.PERMISSION_READ or BluetoothGattDescriptor.PERMISSION_WRITE        )        writeChar.addDescriptor(descriptor)        // 5. 层级组装:特征 → 服务 → 服务端        service.addCharacteristic(readChar)        service.addCharacteristic(writeChar)        gattServer?.addService(service)        showToast("GATT服务创建完成")    }    // ======================== 6. GATT服务端回调(通信核心) ========================    private val gattServerCallback = object : BluetoothGattServerCallback() {        // 1. 连接状态改变(连接/断开)        override funonConnectionStateChange(device: BluetoothDevice?, status: Int, newState: Int) {            super.onConnectionStateChange(device, status, newState)            when (newState) {                BluetoothProfile.STATE_CONNECTED -> runOnUiThread { showToast("设备已连接") }                BluetoothProfile.STATE_DISCONNECTED -> runOnUiThread { showToast("设备已断开") }            }        }        // 2. MTU协商结果(服务端只能接收,不能主动设置)        override funonMtuChanged(device: BluetoothDevice?, mtu: Int, status: Int) {            super.onMtuChanged(device, mtu, status)            runOnUiThread { showToast("MTU=$mtu,有效数据=${mtu-3}") }        }        // 3. 客户端【读】请求        override funonCharacteristicReadRequest(            device: BluetoothDevice?, requestId: Int, offset: Int, characteristic: BluetoothGattCharacteristic?        ) {            super.onCharacteristicReadRequest(device, requestId, offset, characteristic)            // 回复数据给客户端(必须调用)            val responseData = "我是BLE服务端的读数据".toByteArray()            gattServer?.sendResponse(                device,                requestId,                BluetoothGatt.GATT_SUCCESS,                offset,                responseData            )        }        // 4. 客户端【写】请求        override funonCharacteristicWriteRequest(            device: BluetoothDevice?, requestId: Int, characteristic: BluetoothGattCharacteristic?,            preparedWrite: Boolean, responseNeeded: Boolean, offset: Int, value: ByteArray?        ) {            super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value)            // 接收客户端发来的数据            val receiveMsg = String(value ?: byteArrayOf())            runOnUiThread { showToast("收到数据:$receiveMsg") }            // 必须回复响应            gattServer?.sendResponse(                device,                requestId,                BluetoothGatt.GATT_SUCCESS,                offset,                null            )        }    }    // ======================== 7. 释放资源(必须) ========================    @SuppressLint("MissingPermission")    override funonDestroy() {        super.onDestroy()        // 停止广播        bleAdvertiser?.stopAdvertising(object : AdvertiseCallback() {})        // 关闭GattServer        gattServer?.close()    }    // 工具方法:显示Toast    private funshowToast(msg: String) {        runOnUiThread { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show() }    }}

五、代码流程详细讲解(对应代码编写步骤)

1. 初始化蓝牙

获取BluetoothManager(系统服务)

获取BluetoothAdapter(蓝牙核心)

自动开启蓝牙

2. 开启广播

使用BluetoothLeAdvertiser开启广播

配置为可连接、持续广播

广播设备名 + 服务 UUID,让客户端能扫描识别

3. 创建 GattServer(最关键)

通过 BluetoothManager 调用 openGattServer ()传入回调,处理所有通信,系统自动启动

4. 构建服务结构

Service(服务):功能集合

Characteristic(特征):通信通道(读 / 写)

Descriptor(描述符):固定 2902,用于开启通知

5. 通信回调处理

onConnectionStateChange:监听连接

onMtuChanged:获取 MTU

onCharacteristicReadRequest:处理读

onCharacteristicWriteRequest:处理写

所有请求必须 sendResponse ()

6. 资源释放

页面销毁时:停止广播 + 关闭 GattServer

避免内存泄漏、蓝牙异常


六、必须记住的注意事项

CCC 描述符 UUID 固定 00002902,不能自定义

GattServer 只能由 BluetoothManager 创建

MTU 最大 512,客户端请求,服务端不可主动设置

读写请求必须回复 sendResponse ()

Android 12+ 必须申请BLUETOOTH_ADVERTISE、BLUETOOTH_CONNECT

BLE 服务运行在主线程,耗时操作需开子线程

退出页面必须释放资源


七、测试方法

安装本 App 到一部安卓手机

打开另一部手机,使用BLE 调试助手

扫描到当前设备,连接

可进行读、写操作,验证通信

最新文章

随机文章