Android 使用 BluetoothGattServer
Android Using BluetoothGattServer
对于一个应用程序,我需要一个 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(...) 方法以 return 数据到读取特征的设备在你的 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 设备既是 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(...) 方法以 return 数据到读取特征的设备在你的 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,其他设备可以从中读取。 我希望这个简短的总结对你有帮助,否则我会尽力帮助你澄清不清楚的事情。