Bluetooth Low Energy Connection Parameters for Android, iOS and Win8

前端 未结 3 1957
后悔当初
后悔当初 2020-12-23 23:02

I\'ve been looking all over the place for the required bluetooth connection parameters that will work for all three of these operating platforms. I\'m using the HOGP (Bluet

相关标签:
3条回答
  • 2020-12-23 23:22

    First, the connection interval defines a time window during which both devices use the same frequency to transfer data. There are 37 data channels in total, and connected devices hop through them every Connection Interval.

    Thus, both devices has to agree on precise values for these parameters from the beginning in order to be in sync, i.e., connected.

    Second, when connection is established the master (or Central) sends connection parameters it supports. The other device (or peripheral) just blindly takes them. iOS by default sets connection interval to 30 ms. After the connection is established the peripheral can request connection parameters update, by defining the min and max values, according to the guidelines apple has provide you with. The receiving part, read iOS in this case, will pick whatever it find best for it between [min;max], and will send back response with exact values it has picked. It also can reject the request, if the values do not comply with the guidelines.

    Lastly, 7.5ms is the minimum length of the connection interval defined by Bluetooth specification. The maximum value is 4 s. The lower it is, the higher bandwidth, but higher power consumption. And the opposite in the higher values. The best value depends on the specific application. Considering that you work with HID profile I assume latency is important to you.

    iOS says that it supports connection intervals down to 20ms (although I found it hard to achieve this some times), but in your case (HID profile) they also allow 11.25 ms.

    Hope that helps.

    0 讨论(0)
  • 2020-12-23 23:26

    To modify parameters in Android (requesting from Central to Peripheral) you can do something like this:

    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;
    }
    

    I must say though that it didn't work for me using Android 5 devices both as peripheral and central, because Generic Acces Service (0x1800) is not offering in my device Characteristic 0x2a04 for Preferred Connection Parameters. It's only offering 0x2a00 (device name) and 0x2a01 (appearance). References:

    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/

    0 讨论(0)
  • 2020-12-23 23:31

    I believe that this characteristic is only meant to provide information to the Central device. That is why it is generally read-only (for me, and cxphong). Bingen's answer does not universally work, and I am not certain it is meant to work that way. Has anybody actually got it to work on a specific device?

    It appears that Android and iOS do not consult the information in this read-only characteristic, and so I am not certain that it is very useful.

    What works for me is described below, for Cypress peripheral and Android central. A similar approach should work with other devices.

    1. On peripheral, define the preferred connection parameters in a CYBLE_GAP_CONN_UPDATE_PARAM_T structure "params".
    2. After GATT connection, in CYBLE_EVT_GATT_CONNECT_IND event handler (for example), call CyBle_L2capLeConnectionParamUpdateRequest(connHandle, &params).

    On the central side, there is nothing to do. After it receives the request, it will initiate the parameter update a bit later.

    Cheers,

    David

    0 讨论(0)
提交回复
热议问题