Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ffe28c24 authored by pramod kotreshappa's avatar pramod kotreshappa Committed by Bruno Martins
Browse files

Periodic Advertisment Sync Transfer feature support

CRs-fixed: 2814447
Change-Id: Ic77e8ddbe57e415f7fd9c11943da50e4d86230d2
parent 022320c0
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -78,4 +78,13 @@ public abstract class PeriodicAdvertisingCallback {
     */
    public void onSyncLost(int syncHandle) {
    }

    /**
     * Callback when periodic sync transfered.
     *
     * @param device
     * @param status
     */
    public void onSyncTransfered(BluetoothDevice device, int status) {
    }
}
+69 −0
Original line number Diff line number Diff line
@@ -215,6 +215,63 @@ public final class PeriodicAdvertisingManager {
        }
    }

    public void transferSync(BluetoothDevice bda, int service_data, int sync_handle) {
        IBluetoothGatt gatt;
        try {
            gatt = mBluetoothManager.getBluetoothGatt();
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to get Bluetooth gatt - ", e);
            PeriodicAdvertisingCallback callback = null;
            for (PeriodicAdvertisingCallback cb : mCallbackWrappers.keySet()) {
                callback = cb;
            }
            if (callback != null) {
                callback.onSyncTransfered(bda,
                        PeriodicAdvertisingCallback.SYNC_NO_RESOURCES);
            }
            return;
        }
        try {
            gatt.transferSync(bda, service_data , sync_handle);
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to register sync - ", e);
            return;
        }
    }

    public void transferSetInfo(BluetoothDevice bda, int service_data,
                      int adv_handle, PeriodicAdvertisingCallback callback) {
        transferSetInfo(bda, service_data, adv_handle, callback, null);
    }

    public void transferSetInfo (BluetoothDevice bda, int service_data,
                        int adv_handle, PeriodicAdvertisingCallback callback, Handler handler) {
        if (callback == null) {
            throw new IllegalArgumentException("callback can't be null");
        }
        IBluetoothGatt gatt;
        try {
            gatt = mBluetoothManager.getBluetoothGatt();
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to get Bluetooth gatt - ", e);
            return;
        }
        if (handler == null) {
            handler = new Handler(Looper.getMainLooper());
        }
        IPeriodicAdvertisingCallback wrapper = wrap(callback, handler);
        if (wrapper == null) {
            throw new IllegalArgumentException("callback was not properly registered");
        }
        try {
            gatt.transferSetInfo(bda, service_data , adv_handle, wrapper);
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to register sync - ", e);
            return;
        }

    }

    @SuppressLint("AndroidFrameworkBluetoothPermission")
    private IPeriodicAdvertisingCallback wrap(PeriodicAdvertisingCallback callback,
            Handler handler) {
@@ -259,6 +316,18 @@ public final class PeriodicAdvertisingManager {
                    }
                });
            }

            public void onSyncTransfered(BluetoothDevice device, int status) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callback.onSyncTransfered(device, status);
                        // App can still unregister the sync until notified it's lost.
                        // Remove callback after app was notifed.
                        //mCallbackWrappers.remove(callback);
                    }
                });
            }
        };
    }
}