要修改參數的Android(從中央請求外設),你可以做這樣的事情:
private String CONN_SERVICE_UUID = "00001800-0000-1000-8000-00805f9b34fb";
private static final UUID CONN_CHARACTERISTIC_UUID = UUID.fromString("00002a04-0000-1000-8000-00805F9B34FB");
private static final int CONN_INTERVAL = 0x0006;
private static final int SUPERVISION_TIMEOUT = 0x000A;
private void findServiceForConnectionParams(List<BluetoothGattService> gattServices){
BluetoothGattService connGattService = filterServices(gattServices, CONN_SERVICE_UUID);
if (connGattService != null) {
setConnectionInterval(connGattService);
}
}
private void setConnectionInterval(BluetoothGattService gattService) {
if (gattService == null) {
Log.e(TAG, "setConnectionInterval. Gatt service is null!");
return;
}
BluetoothGattCharacteristic connCharacteristic =
gattService.getCharacteristic(CONN_CHARACTERISTIC_UUID);
if (connCharacteristic != null) {
byte[] value = { (byte) (CONN_INTERVAL & 0x00FF), // gets LSB of 2 byte value
(byte) ((CONN_INTERVAL & 0xFF00) >> 8), // gets MSB of 2 byte value
(byte) (CONN_INTERVAL & 0x00FF),
(byte) ((CONN_INTERVAL & 0xFF00) >> 8),
0, 0,
(byte) (SUPERVISION_TIMEOUT & 0x00FF),
(byte) ((SUPERVISION_TIMEOUT & 0xFF00) >> 8)
};
connCharacteristic.setValue(value);
boolean status = mBluetoothGatt.writeCharacteristic(connCharacteristic);
Log.d(TAG, "setConnectionInterval. Change connection interval result: " + status);
} else {
Log.e(TAG, "setConnectionInterval. Connection characteristic is null!");
}
}
private BluetoothGattService filterServices(List<BluetoothGattService> gattServices, String targetUuid) {
for(BluetoothGattService gattService : gattServices){
String serviceUUID = gattService.getUuid().toString();
Log.i(TAG, "serviceUUID: " + serviceUUID);
if(serviceUUID.equals(targetUuid)){
Log.i(TAG, "serviceUUID matches! UUID: " + serviceUUID + " Type: " + gattService.getType());
// no needed, just to check which characteristics are offered
for(BluetoothGattCharacteristic characteristic : gattService.getCharacteristics()) {
Log.i(TAG, "serviceUUID characteristics: " + characteristic.getUuid().toString());
}
return gattService;
}
}
return null;
}
我必須說,它並沒有採用Android 5個設備都爲周圍對我的工作和中央,因爲通用訪問服務(0x1800)不提供我的設備特徵0x2a04爲首選連接參數。它只提供0x2a00(設備名稱)和0x2a01(外觀)。 參考文獻:
http://www.cumulations.com/blogs/7/Doing-firmware-upgrade-over-BLE-in-Android
https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.peripheral_preferred_connection_parameters.xml
https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.generic_access.xml
https://farwestab.wordpress.com/2011/02/05/some-tips-on-android-and-bluetooth/
您可能要問了三個獨立的平臺,三個獨立的問題。因爲只有一個平臺知道答案的人不可能回答,並且/或者你只能接受三個正確答案中的一個。 – 323go
@ 323go - 問題的一部分似乎是如何實現與平臺*衝突*細節的兼容性。 –
可能值得檢查7.5ms是否與期望15或22.5毫秒的內容兼容 –