对于一个应用程序,我需要一个Android设备既是一个Ble Gatt外设和服务器,以接受传入和传出的邮件;然而,似乎除了在github上查看其他人的项目外,我无法找到关于建立和维护服务器的许多信息。 任何人都可以告诉我正确的解决方案或指导我有关设置BluetoothGattServer的更多信息。Android使用蓝牙收集服务器
0
A
回答
6
我想快速提及,大多数使用的Android设备不支持BluetoothGattServer。不过新型号的功能越来越多。
首先您可能想要宣传您的服务,以便其他设备了解服务器。为此,我们需要创建AdvertiseSettings,AdvertiseData和AdvertData for ScanResponses。
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 a 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从其他设备可以读取。 我希望这个简短的总结对你有帮助,否则我会尽力帮你澄清不清楚的事情。
相关问题
- 1. Android蓝牙服务禁用
- 2. 将蓝牙Android客户端连接到蓝牙Java服务器
- 3. Android蓝牙服务发现
- 4. Android中的蓝牙服务
- 5. Android的蓝牙客户端服务器
- 6. 蓝牙服务
- 7. 蓝牙服务器和BroadCastReceiver
- 8. 启动蓝牙服务器
- 9. Android蓝牙 - 用于记录附近蓝牙设备的服务
- 10. 如何使用Android中的蓝牙服务器Socket RFCOMM接收数据?
- 11. 蓝牙SPP和其他蓝牙服务
- 12. 使用反射方法创建蓝牙服务器android
- 13. 无法调用蓝牙广播接收器方法的服务
- 14. 使用HCITool广告蓝牙LE服务
- 15. Android打开服务中的蓝牙
- 16. 获取辅助蓝牙gatt服务android
- 17. Android内部的蓝牙服务
- 18. Android蓝牙:java.io.IOException:服务发现失败
- 19. 在Android服务中启动蓝牙
- 20. 意向服务与蓝牙 - Android Studio
- 21. 服务发现失败android蓝牙
- 22. 在Android服务上连接蓝牙
- 23. Android后台服务蓝牙扫描
- 24. Android蓝牙服务器客户端应用程序
- 25. 蓝牙适配器服务保持空
- 26. Windows上的蓝牙SPP服务器
- 27. 组织蓝牙LE服务
- 28. iOS蓝牙服务建议
- 29. 蓝牙服务问题
- 30. 使用android的蓝牙遥控器
服务器也可以成为gatt客户端吗?我似乎有一个问题,因为添加服务器与gatt客户端连接,但我假设当手机试图连接到已经连接到其他设备的问题发生。由于接口更改,并且equals方法仅基于更改的地址,并且在api23中返回不正确的地址,所以再次无法识别可更新设备。 – kdgwill
是的,设备可以同时充当外设和中央设备。至于你所遇到的问题,我不太确定,但据我所知它应该起作用。请记住,Android ble堆栈非常不稳定。 – p2pkit
对于识别,有一种方法可以识别设备。在广告设置中,您可以将附加数据添加到服务uuid,只需在ScanResponseData的构建中添加对addServiceData(...)的调用即可。但是ScanResponseData的大小不能超过31个字节。另一个缺陷是,在接收端,serviceData键是32位uuid而不是128位。 – p2pkit