Android使用BluetoothGattServer

时间:2016-05-12 08:51:04

标签: android bluetooth-lowenergy gatt

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

1 个答案:

答案 0 :(得分:9)

我想快速提一下,大多数正在使用的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,其他设备可以从中读取。 我希望这个简短的总结可以帮助你,否则我会尽力帮助你澄清不清楚的事情。