Skip to content

Commit ca6c833

Browse files
pramod kotreshappaMocaRafee
authored andcommitted
Periodic Advertisment Sync Transfer feature support
CRs-fixed: 2814447 Change-Id: Ic77e8ddbe57e415f7fd9c11943da50e4d86230d2
1 parent 51e1c05 commit ca6c833

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

core/java/android/bluetooth/le/PeriodicAdvertisingCallback.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,13 @@ public void onPeriodicAdvertisingReport(PeriodicAdvertisingReport report) {
7878
*/
7979
public void onSyncLost(int syncHandle) {
8080
}
81+
82+
/**
83+
* Callback when periodic sync transfered.
84+
*
85+
* @param device
86+
* @param status
87+
*/
88+
public void onSyncTransfered(BluetoothDevice device, int status) {
89+
}
8190
}

core/java/android/bluetooth/le/PeriodicAdvertisingManager.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,62 @@ public void unregisterSync(PeriodicAdvertisingCallback callback) {
196196
}
197197
}
198198

199+
public void transferSync(BluetoothDevice bda, int service_data, int sync_handle) {
200+
IBluetoothGatt gatt;
201+
try {
202+
gatt = mBluetoothManager.getBluetoothGatt();
203+
} catch (RemoteException e) {
204+
Log.e(TAG, "Failed to get Bluetooth gatt - ", e);
205+
PeriodicAdvertisingCallback callback = null;
206+
for (PeriodicAdvertisingCallback cb : mCallbackWrappers.keySet()) {
207+
callback = cb;
208+
}
209+
if (callback != null) {
210+
callback.onSyncTransfered(bda,
211+
PeriodicAdvertisingCallback.SYNC_NO_RESOURCES);
212+
}
213+
return;
214+
}
215+
try {
216+
gatt.transferSync(bda, service_data , sync_handle);
217+
} catch (RemoteException e) {
218+
Log.e(TAG, "Failed to register sync - ", e);
219+
return;
220+
}
221+
}
222+
223+
public void transferSetInfo(BluetoothDevice bda, int service_data,
224+
int adv_handle, PeriodicAdvertisingCallback callback) {
225+
transferSetInfo(bda, service_data, adv_handle, callback, null);
226+
}
227+
228+
public void transferSetInfo (BluetoothDevice bda, int service_data,
229+
int adv_handle, PeriodicAdvertisingCallback callback, Handler handler) {
230+
if (callback == null) {
231+
throw new IllegalArgumentException("callback can't be null");
232+
}
233+
IBluetoothGatt gatt;
234+
try {
235+
gatt = mBluetoothManager.getBluetoothGatt();
236+
} catch (RemoteException e) {
237+
Log.e(TAG, "Failed to get Bluetooth gatt - ", e);
238+
return;
239+
}
240+
if (handler == null) {
241+
handler = new Handler(Looper.getMainLooper());
242+
}
243+
IPeriodicAdvertisingCallback wrapper = wrap(callback, handler);
244+
if (wrapper == null) {
245+
throw new IllegalArgumentException("callback was not properly registered");
246+
}
247+
try {
248+
gatt.transferSetInfo(bda, service_data , adv_handle, wrapper);
249+
} catch (RemoteException e) {
250+
Log.e(TAG, "Failed to register sync - ", e);
251+
return;
252+
}
253+
254+
}
199255
private IPeriodicAdvertisingCallback wrap(PeriodicAdvertisingCallback callback,
200256
Handler handler) {
201257
return new IPeriodicAdvertisingCallback.Stub() {
@@ -239,6 +295,18 @@ public void run() {
239295
}
240296
});
241297
}
298+
299+
public void onSyncTransfered(BluetoothDevice device, int status) {
300+
handler.post(new Runnable() {
301+
@Override
302+
public void run() {
303+
callback.onSyncTransfered(device, status);
304+
// App can still unregister the sync until notified it's lost.
305+
// Remove callback after app was notifed.
306+
//mCallbackWrappers.remove(callback);
307+
}
308+
});
309+
}
242310
};
243311
}
244312
}

0 commit comments

Comments
 (0)