You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android端启动GATT Server的最低SDK要求及无BluetoothLeAdvertiser实现咨询

Android GATT Server SDK版本与API 19兼容连接方案

1. 启动GATT Server的最低SDK版本

启动BluetoothGattServer的最低要求是Android 4.3(API级别18),BLE核心API(包括BluetoothManager和BluetoothGattServer类)都是在这个版本正式引入的。不过API 18的BLE实现稳定性不如后续版本,但基础的GATT Server创建、服务添加、连接处理功能都是完整支持的。

2. API 19下无需BluetoothLeAdvertiser实现客户端连接的方案

你提到的BluetoothLeAdvertiser是Android 5.0(API21)才新增的主动广播控制API,但在API18-20中,我们完全可以通过客户端主动扫描+设备标识匹配的方式实现连接,具体步骤如下:

第一步:正确初始化并启动GATT Server

首先要确保在API19设备上正确创建GATT Server并添加自定义服务,示例代码如下:

// 获取蓝牙管理器
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
// 打开GATT Server并设置回调
BluetoothGattServer gattServer = bluetoothManager.openGattServer(this, new BluetoothGattServerCallback() {
    @Override
    public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
        super.onConnectionStateChange(device, status, newState);
        // 处理连接状态变化:比如连接成功时记录设备,断开时清理资源
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.d("GattServer", "设备已连接: " + device.getName());
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.d("GattServer", "设备已断开");
        }
    }

    @Override
    public void onServiceAdded(int status, BluetoothGattService service) {
        super.onServiceAdded(status, service);
        // 确认服务是否成功添加,status为BluetoothGatt.GATT_SUCCESS表示添加成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.d("GattServer", "服务添加成功");
        }
    }
});

// 创建并添加自定义GATT服务(这里示例一个带可读特征的主服务)
UUID serviceUuid = UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb");
BluetoothGattService service = new BluetoothGattService(serviceUuid, BluetoothGattService.SERVICE_TYPE_PRIMARY);

UUID charUuid = UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb");
BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(
        charUuid,
        BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
        BluetoothGattCharacteristic.PERMISSION_READ
);
service.addCharacteristic(characteristic);

// 将服务添加到GATT Server
gattServer.addService(service);

第二步:客户端通过扫描匹配并发起连接

在API18-20中,Android设备的BLE模块会默认发送基础广播(包含设备名称、MAC地址等信息)。客户端可以通过两种方式筛选你的GATT Server设备:

方式一:通过设备名称筛选

先给你的GATT Server设备设置一个唯一的蓝牙名称(可以在系统蓝牙设置中修改,或者通过代码设置):

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.setName("MyGattServerDevice");

然后客户端扫描时匹配这个名称:

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 启动BLE扫描
bluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
        // 匹配目标设备名称
        if ("MyGattServerDevice".equals(device.getName())) {
            // 停止扫描,避免资源浪费
            bluetoothAdapter.stopLeScan(this);
            // 发起GATT连接
            BluetoothGatt gatt = device.connectGatt(context, false, new BluetoothGattCallback() {
                @Override
                public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                    if (newState == BluetoothProfile.STATE_CONNECTED) {
                        Log.d("GattClient", "连接成功,开始发现服务");
                        // 连接成功后发现服务
                        gatt.discoverServices();
                    }
                }

                @Override
                public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                    super.onServicesDiscovered(gatt, status);
                    if (status == BluetoothGatt.GATT_SUCCESS) {
                        // 找到目标服务,进行数据交互
                        BluetoothGattService targetService = gatt.getService(serviceUuid);
                        // ...后续数据读写逻辑
                    }
                }
            });
        }
    }
});

方式二:通过服务UUID筛选(更精准)

客户端可以在扫描时指定要查找的服务UUID,这样扫描结果只会返回包含该UUID的设备,避免无效匹配:

UUID targetServiceUuid = UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb");
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 传入要扫描的服务UUID数组,只返回包含该服务的设备
bluetoothAdapter.startLeScan(new UUID[]{targetServiceUuid}, new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
        // 找到目标设备,直接发起连接
        bluetoothAdapter.stopLeScan(this);
        device.connectGatt(context, false, gattCallback);
    }
});

重要注意事项

  • API19兼容性坑点:API18-20的BLE实现存在一些稳定性问题,比如扫描可能出现延迟、连接断开后重连失败等,建议在测试时覆盖多个品牌的API19设备,必要时添加重连逻辑。
  • 权限配置:确保你的应用已经申请了必要的BLE权限:
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <!-- API23+需要位置权限,API19-22不需要,但部分设备扫描可能需要 -->
    

总结来说,API19下不需要依赖BluetoothLeAdvertiser,只要正确启动GATT Server并添加服务,客户端通过扫描设备名称或服务UUID就能找到并连接你的GATT Server。

内容的提问来源于stack exchange,提问作者Ilya Demidov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:56:04