@@ -70,6 +70,7 @@ public class BluetoothPbap {
7070 private IBluetoothPbap mService ;
7171 private final Context mContext ;
7272 private ServiceListener mServiceListener ;
73+ private BluetoothAdapter mAdapter ;
7374
7475 /** There was an error trying to obtain the state */
7576 public static final int STATE_ERROR = -1 ;
@@ -96,7 +97,7 @@ public interface ServiceListener {
9697 * this callback before making IPC calls on the BluetoothPbap
9798 * service.
9899 */
99- public void onServiceConnected ();
100+ public void onServiceConnected (BluetoothPbap proxy );
100101
101102 /**
102103 * Called to notify the client that this proxy object has been
@@ -108,12 +109,54 @@ public interface ServiceListener {
108109 public void onServiceDisconnected ();
109110 }
110111
112+ final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
113+ new IBluetoothStateChangeCallback .Stub () {
114+ public void onBluetoothStateChange (boolean up ) {
115+ if (DBG ) Log .d (TAG , "onBluetoothStateChange: up=" + up );
116+ if (!up ) {
117+ if (DBG ) Log .d (TAG ,"Unbinding service..." );
118+ synchronized (mConnection ) {
119+ try {
120+ mService = null ;
121+ mContext .unbindService (mConnection );
122+ } catch (Exception re ) {
123+ Log .e (TAG ,"" ,re );
124+ }
125+ }
126+ } else {
127+ synchronized (mConnection ) {
128+ try {
129+ if (mService == null ) {
130+ if (DBG ) Log .d (TAG ,"Binding service..." );
131+ if (!mContext .bindService (
132+ new Intent (IBluetoothPbap .class .getName ()),
133+ mConnection , 0 )) {
134+ Log .e (TAG , "Could not bind to Bluetooth PBAP Service" );
135+ }
136+ }
137+ } catch (Exception re ) {
138+ Log .e (TAG ,"" ,re );
139+ }
140+ }
141+ }
142+ }
143+ };
144+
111145 /**
112146 * Create a BluetoothPbap proxy object.
113147 */
114148 public BluetoothPbap (Context context , ServiceListener l ) {
115149 mContext = context ;
116150 mServiceListener = l ;
151+ mAdapter = BluetoothAdapter .getDefaultAdapter ();
152+ IBluetoothManager mgr = mAdapter .getBluetoothManager ();
153+ if (mgr != null ) {
154+ try {
155+ mgr .registerStateChangeCallback (mBluetoothStateChangeCallback );
156+ } catch (RemoteException e ) {
157+ Log .e (TAG ,"" ,e );
158+ }
159+ }
117160 if (!context .bindService (new Intent (IBluetoothPbap .class .getName ()), mConnection , 0 )) {
118161 Log .e (TAG , "Could not bind to Bluetooth Pbap Service" );
119162 }
@@ -134,9 +177,25 @@ protected void finalize() throws Throwable {
134177 * are ok.
135178 */
136179 public synchronized void close () {
137- if (mConnection != null ) {
138- mContext .unbindService (mConnection );
139- mConnection = null ;
180+ IBluetoothManager mgr = mAdapter .getBluetoothManager ();
181+ if (mgr != null ) {
182+ try {
183+ mgr .unregisterStateChangeCallback (mBluetoothStateChangeCallback );
184+ } catch (Exception e ) {
185+ Log .e (TAG ,"" ,e );
186+ }
187+ }
188+
189+ synchronized (mConnection ) {
190+ if (mService != null ) {
191+ try {
192+ mService = null ;
193+ mContext .unbindService (mConnection );
194+ mConnection = null ;
195+ } catch (Exception re ) {
196+ Log .e (TAG ,"" ,re );
197+ }
198+ }
140199 }
141200 mServiceListener = null ;
142201 }
@@ -240,7 +299,7 @@ public void onServiceConnected(ComponentName className, IBinder service) {
240299 if (DBG ) log ("Proxy object connected" );
241300 mService = IBluetoothPbap .Stub .asInterface (service );
242301 if (mServiceListener != null ) {
243- mServiceListener .onServiceConnected ();
302+ mServiceListener .onServiceConnected (BluetoothPbap . this );
244303 }
245304 }
246305 public void onServiceDisconnected (ComponentName className ) {
0 commit comments