Android 使用 BluetoothGattServer

标签 android bluetooth-lowenergy gatt

对于一个应用程序,我需要一个 android 设备既是 Ble Gatt 外围设备又是服务器,以接受传入和发送传出消息;但是,除了在 github 中查看其他人的项目之外,我似乎找不到太多与设置和维护服务器有关的信息。
谁能告诉我正确的解决方案或指导我了解有关设置 BluetoothGattServer 的更多信息。

最佳答案

我想快速提一下,大多数使用的 Android 设备不支持 BluetoothGattServer。然而,较新的模型越来越具有这种能力。

首先,您可能想宣传您的服务,以便其他设备了解该服务器。为此,我们需要为 ScanResponses 创建 AdvertiseSettings、AdvertiseData 和 AdvertiseData。

AdvertiseSettings settings = new AdvertiseSettings.Builder()
                .setConnectable(true)
                .build();

AdvertiseData advertiseData = new AdvertiseData.Builder()
                    .setIncludeDeviceName(true)
                    .setIncludeTxPowerLevel(true)
                    .build();

AdvertiseData scanResponseData = new AdvertiseData.Builder()
                    .addServiceUuid(new ParcelUuid(your_service_uuid))
                    .setIncludeTxPowerLevel(true)
                    .build();

之后,您需要为广告状态创建回调:
AdvertiseCallback callback = new AdvertiseCallback() {
                @Override
                public void onStartSuccess(AdvertiseSettings settingsInEffect) {
                    Log.d(TAG, "BLE advertisement added successfully");
                }

                @Override
                public void onStartFailure(int errorCode) {
                    Log.e(TAG, "Failed to add BLE advertisement, reason: " + errorCode);
                }
            };

现在您可以开始宣传您的服务了:
BluetoothLeAdvertiser bluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
bluetoothLeAdvertiser.startAdvertising(settings, advertiseData, scanResponseData, callback);

对于 BluetoothGattServer,我们首先需要创建一个 BluetoothGattServerCallback:
BluetoothGattServerCallback callback = new BluetoothGattServerCallback() {
            @Override
            public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
                super.onConnectionStateChange(device, status, newState);
            }

            @Override
            public void onServiceAdded(int status, BluetoothGattService service) {
                super.onServiceAdded(status, service);
            }

            @Override
            public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
                super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
            }

            @Override
            public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
                super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);
            }

            @Override
            public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
                super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded, offset, value);
            }

            @Override
            public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor) {
                super.onDescriptorReadRequest(device, requestId, offset, descriptor);
            }

            @Override
            public void onNotificationSent(BluetoothDevice device, int status) {
                super.onNotificationSent(device, status);
            }

            @Override
            public void onMtuChanged(BluetoothDevice device, int mtu) {
                super.onMtuChanged(device, mtu);
            }

            @Override
            public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) {
                super.onExecuteWrite(device, requestId, execute);
            }
        };

您不需要实现所有这些方法,只需实现您感兴趣的那些。例如,您可以实现 onCharacteristicReadRequest(...) 方法将数据返回到读取 BluetoothGattServer 上的特征的设备。

之后,您可以打开 GattServer,创建服务并将服务添加到服务器:
BluetoothGattServer bluetoothGattServer = mBluetoothManager.openGattServer(Context, callback);
BluetoothGattService service = new BluetoothGattService(your_service_uuid, BluetoothGattService.SERVICE_TYPE_PRIMARY);

//add a read characteristic.
BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(your_characteristic_uuid, BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ);

service.addCharacteristic(characteristic)

bluetoothGattServer.addService(service);

现在您已经设置了具有读取特性的 BluetoothGattServer,其他设备可以从中读取。
我希望这个简短的摘要对您有所帮助,否则我会尽力帮助您澄清不清楚的事情。

关于Android 使用 BluetoothGattServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37181843/

相关文章:

ios - 是否可以使用经典蓝牙定位估计信标?

bluetooth - 如何使用 hci 命令将我的 Linux 笔记本电脑设置为 BLE 外设以通告具有指定 UUID 的服务?

android - 在 fragment 之间传递对象

android - 如何依次请求多个权限?

Android BLE 外围设备断开连接,状态代码为 BLE_HCI_INSTANT_PASSED(0x28)

c# - 如何配置 WPF 项目以使用 BLE?

python - BlueZ 示例 GATT 服务器

android - 出现错误,例如 APK META-INF/LICENSE 中复制的重复文件

android - 如何在 TextView 的文本上设置多个跨度(可点击和粗体)

c# - Windows 10 API 中的蓝牙低功耗广告数据限制