@@ -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