diff options
author | Andrey Vostrikov <andrey.vostrikov@cogentembedded.com> | 2012-08-17 18:05:06 +0400 |
---|---|---|
committer | Andrey Vostrikov <andrey.vostrikov@cogentembedded.com> | 2012-08-17 18:05:06 +0400 |
commit | b676e2c2e36e8cf01fa4d352fa185d3bbc024741 (patch) | |
tree | 843a8bb686bf554ce719aa4aae7dabb4b30bc527 | |
parent | b329e305cfd08c1bcacd47745e33b71c5384ae17 (diff) | |
download | android_packages_apps_Bluetooth-ics-release.tar.gz android_packages_apps_Bluetooth-ics-release.tar.bz2 android_packages_apps_Bluetooth-ics-release.zip |
Bluetooth MAP profile fixics-release
Fixed misspelled name of intent action from 'bleutooth' to appropriate string
constant in BluetoothDevice class.
Cleaned up code from Java reflection, added exceptions to log function
calls in catch{} handlers.
Change-Id: I0b563ebc66a648388957fd45864efadd95203a35
-rw-r--r-- | src/com/android/bluetooth/map/BluetoothMasAppIf.java | 23 | ||||
-rw-r--r-- | src/com/android/bluetooth/map/BluetoothMasService.java | 70 | ||||
-rw-r--r-- | src/com/android/bluetooth/map/BluetoothMns.java | 207 | ||||
-rw-r--r-- | src/com/android/bluetooth/map/BluetoothMnsObexSession.java | 112 |
4 files changed, 117 insertions, 295 deletions
diff --git a/src/com/android/bluetooth/map/BluetoothMasAppIf.java b/src/com/android/bluetooth/map/BluetoothMasAppIf.java index e95fad353..3f8f36b90 100644 --- a/src/com/android/bluetooth/map/BluetoothMasAppIf.java +++ b/src/com/android/bluetooth/map/BluetoothMasAppIf.java @@ -34,8 +34,6 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -549,26 +547,7 @@ public class BluetoothMasAppIf { String sLocalPhoneNum = tm.getLine1Number(); String sLocalPhoneName = ""; - Method m; - try { - m = tm.getClass().getMethod("getLine1AlphaTag", new Class[] { }); - sLocalPhoneName = (String) m.invoke(tm); - } catch (SecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchMethodException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + sLocalPhoneName = tm.getLine1AlphaTag(); if (TextUtils.isEmpty(sLocalPhoneNum)) { sLocalPhoneNum = "0000000000"; diff --git a/src/com/android/bluetooth/map/BluetoothMasService.java b/src/com/android/bluetooth/map/BluetoothMasService.java index 9028499a3..83d4d8b6d 100644 --- a/src/com/android/bluetooth/map/BluetoothMasService.java +++ b/src/com/android/bluetooth/map/BluetoothMasService.java @@ -31,8 +31,6 @@ package com.android.bluetooth.map; import com.android.bluetooth.R; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import android.app.Notification; import android.app.NotificationManager; @@ -323,64 +321,38 @@ public class BluetoothMasService extends Service { if (VERBOSE) Log.e(TAG, "Map Service initSocket"); - boolean initSocketOK = true; final int CREATE_RETRY_TIME = 10; // It's possible that create will fail in some cases. retry for 10 times - for (int i = 0; i < CREATE_RETRY_TIME && !mInterrupted; i++) { + mInterrupted = false; + int i; + for (i = 0; i < CREATE_RETRY_TIME && !mInterrupted; i++) { try { - Method m = mAdapter.getClass().getMethod("listenUsingRfcommOn", - new Class[] { int.class }); - try { - mServerSocket = (BluetoothServerSocket) m.invoke(mAdapter, - PORT_NUM); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + mServerSocket = mAdapter.listenUsingRfcommOn(PORT_NUM); + + if (VERBOSE) Log.e(TAG, "Succeed to create listening socket on channel " + PORT_NUM); - } catch (SecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchMethodException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + return true; + + } catch (IOException e) { + if (VERBOSE) Log.e(TAG, "Error creating RFCOMM socket, retry: " + (i + 1), e); } - if (!initSocketOK) { - synchronized (this) { - try { - if (VERBOSE) - Log.e(TAG, "wait 3 seconds"); - Thread.sleep(3000); - } catch (InterruptedException e) { - Log - .e(TAG, - "socketAcceptThread thread was interrupted (3)"); - mInterrupted = true; - } + synchronized (this) { + try { + if (VERBOSE) Log.e(TAG, "wait 3 seconds"); + Thread.sleep(3000); + } catch (InterruptedException e) { + Log.e(TAG, "socketAcceptThread thread was interrupted (3)"); + mInterrupted = true; } - } else { - break; } } - if (initSocketOK) { - if (VERBOSE) - Log.e(TAG, "Succeed to create listening socket on channel " - + PORT_NUM); - - } else { - Log.e(TAG, "Error to create listening socket after " - + CREATE_RETRY_TIME + " try"); - } - return initSocketOK; + Log.e(TAG, "Error to create listening socket after " + + (i+1) + " try"); + + return false; } private final void closeSocket(boolean server, boolean accept) diff --git a/src/com/android/bluetooth/map/BluetoothMns.java b/src/com/android/bluetooth/map/BluetoothMns.java index d69ac3659..729c721ea 100644 --- a/src/com/android/bluetooth/map/BluetoothMns.java +++ b/src/com/android/bluetooth/map/BluetoothMns.java @@ -34,8 +34,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.net.Socket; import java.util.ArrayList; import java.util.List; @@ -43,6 +41,7 @@ import java.util.List; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; +import android.bluetooth.BluetoothUuid; import android.content.BroadcastReceiver; import android.content.ContentResolver; import android.content.Context; @@ -278,7 +277,7 @@ public class BluetoothMns { mTransport.close(); } } catch (IOException e) { - Log.e(TAG, "failed to close mTransport"); + Log.e(TAG, "failed to close mTransport", e); } if (V) Log.v(TAG, "mTransport closed "); @@ -2566,38 +2565,13 @@ public class BluetoothMns { * Else start sdp query */ private void sendMnsSdp() { - if (V) - Log.v(TAG, "Do Opush SDP request for address " + mDestination); + if (V) Log.v(TAG, "Do Opush SDP request for address " + mDestination); mTimestamp = System.currentTimeMillis(); - int channel = -1; - - Method m; - try { - m = mDestination.getClass().getMethod("getServiceChannel", - new Class[] { ParcelUuid.class }); - channel = (Integer) m.invoke(mDestination, BluetoothUuid_ObexMns); - } catch (SecurityException e2) { - // TODO Auto-generated catch block - e2.printStackTrace(); - } catch (NoSuchMethodException e2) { - // TODO Auto-generated catch block - e2.printStackTrace(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - // TODO: channel = mDestination.getServiceChannel(BluetoothUuid_ObexMns); + int channel = mDestination.getServiceChannel(BluetoothUuid_ObexMns); - if (channel != -1) { + if (channel >= 0) { if (D) Log.d(TAG, "Get MNS channel " + channel + " from SDP for " + mDestination); @@ -2607,50 +2581,24 @@ public class BluetoothMns { .sendToTarget(); return; - } else { + } - boolean result = false; - if (V) - Log.v(TAG, "Remote Service channel not in cache"); + if (V) Log.v(TAG, "Remote Service channel not in cache"); - Method m2; - try { - m2 = mDestination.getClass().getMethod("fetchUuidsWithSdp", - new Class[] {}); - result = (Boolean) m2.invoke(mDestination); + if (mDestination.fetchUuidsWithSdp()) { + // we expect framework send us Intent ACTION_UUID. otherwise we will fail + if (V) Log.v(TAG, "Start new SDP, wait for result"); - } catch (SecurityException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } catch (NoSuchMethodException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_UUID); - if (result == false) { - Log.e(TAG, "Start SDP query failed"); - } else { - // we expect framework send us Intent ACTION_UUID. otherwise we - // will fail - if (V) - Log.v(TAG, "Start new SDP, wait for result"); - IntentFilter intentFilter = new IntentFilter( - "android.bleutooth.device.action.UUID"); - mContext.registerReceiver(mReceiver, intentFilter); - return; - } + mContext.registerReceiver(mReceiver, intentFilter); + + return; } - Message msg = mSessionHandler.obtainMessage(SDP_RESULT, channel, -1, - mDestination); + + Log.e(TAG, "Start SDP query failed"); + + Message msg = mSessionHandler.obtainMessage(SDP_RESULT, channel, -1, mDestination); mSessionHandler.sendMessageDelayed(msg, 2000); } @@ -2663,8 +2611,7 @@ public class BluetoothMns { Log.d(TAG, " MNS BROADCAST RECV intent: " + intent.getAction()); - if (intent.getAction().equals( - "android.bleutooth.device.action.UUID")) { + if (intent.getAction().equals(BluetoothDevice.ACTION_UUID)) { BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (V) @@ -2672,94 +2619,28 @@ public class BluetoothMns { if (device.equals(mDestination)) { int channel = -1; Parcelable[] uuid = intent - .getParcelableArrayExtra("android.bluetooth.device.extra.UUID"); + .getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID); if (uuid != null) { ParcelUuid[] uuids = new ParcelUuid[uuid.length]; for (int i = 0; i < uuid.length; i++) { uuids[i] = (ParcelUuid) uuid[i]; } - boolean result = false; - - // TODO Fix this error - result = true; - - try { - Class c = Class - .forName("android.bluetooth.BluetoothUuid"); - Method m = c.getMethod("isUuidPresent", - new Class[] { ParcelUuid[].class, - ParcelUuid.class }); - - Boolean bool = false; - bool = (Boolean) m.invoke(c, uuids, - BluetoothUuid_ObexMns); - result = bool.booleanValue(); - - } catch (ClassNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (SecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchMethodException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + boolean result = BluetoothUuid.isUuidPresent(uuids, BluetoothUuid_ObexMns); if (result) { - // TODO: Check if UUID IS PRESENT - if (V) - Log.v(TAG, "SDP get MNS result for device " - + device); - - // TODO: Get channel from mDestination - // TODO: .getServiceChannel(BluetoothUuid_ObexMns); - Method m1; - try { - - m1 = device.getClass().getMethod( - "getServiceChannel", - new Class[] { ParcelUuid.class }); - Integer chan = (Integer) m1.invoke(device, - BluetoothUuid_ObexMns); - - channel = chan.intValue(); - Log.d(TAG, " MNS SERVER Channel no " + channel); - if (channel == -1) { - channel = 2; - Log.d(TAG, " MNS SERVER USE TEMP CHANNEL " - + channel); - } - } catch (SecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchMethodException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + + if (V) Log.v(TAG, "SDP get MNS result for device " + device); + + channel = mDestination.getServiceChannel(BluetoothUuid_ObexMns); + Log.d(TAG, " MNS SERVER Channel no " + channel); + if (channel< 0) { + channel = 2; + Log.d(TAG, " MNS SERVER USE TEMP CHANNEL " + channel); } } } - mSessionHandler.obtainMessage(SDP_RESULT, channel, -1, - mDestination).sendToTarget(); + mSessionHandler.obtainMessage(SDP_RESULT, channel, -1, mDestination).sendToTarget(); } } } @@ -2803,27 +2684,7 @@ public class BluetoothMns { /* Use BluetoothSocket to connect */ try { - try { - Method m = device.getClass().getMethod( - "createInsecureRfcommSocket", - new Class[] { int.class }); - btSocket = (BluetoothSocket) m.invoke(device, channel); - } catch (SecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchMethodException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + btSocket = device.createInsecureRfcommSocket(channel); } catch (Exception e1) { // TODO Auto-generated catch block Log.e(TAG, "Rfcomm socket create error"); @@ -2849,7 +2710,7 @@ public class BluetoothMns { mSessionHandler.obtainMessage(RFCOMM_CONNECTED, transport) .sendToTarget(); } catch (IOException e) { - Log.e(TAG, "Rfcomm socket connect exception "); + Log.e(TAG, "Rfcomm socket connect exception ", e); BluetoothMnsPreference.getInstance(mContext).removeChannel( device, MNS_UUID16); markConnectionFailed(btSocket); @@ -2864,7 +2725,7 @@ public class BluetoothMns { try { s.close(); } catch (IOException e) { - Log.e(TAG, "TCP socket close error"); + Log.e(TAG, "TCP socket close error", e); } mSessionHandler.obtainMessage(RFCOMM_ERROR).sendToTarget(); } @@ -2876,7 +2737,7 @@ public class BluetoothMns { try { s.close(); } catch (IOException e) { - if (V) Log.e(TAG, "Error when close socket"); + if (V) Log.e(TAG, "Error when close socket", e); } mSessionHandler.obtainMessage(RFCOMM_ERROR).sendToTarget(); return; diff --git a/src/com/android/bluetooth/map/BluetoothMnsObexSession.java b/src/com/android/bluetooth/map/BluetoothMnsObexSession.java index 6d3c663ee..1820eccbf 100644 --- a/src/com/android/bluetooth/map/BluetoothMnsObexSession.java +++ b/src/com/android/bluetooth/map/BluetoothMnsObexSession.java @@ -92,25 +92,25 @@ public class BluetoothMnsObexSession { if (mCs != null) { mCs.disconnect(null); } - mCs = null; - if (D) Log.d(TAG, "OBEX session disconnected"); - } catch (IOException e) { - Log.w(TAG, "OBEX session disconnect error" + e); + mCs = null; + if (D) Log.d(TAG, "OBEX session disconnected"); + } catch (IOException e) { + Log.w(TAG, "OBEX session disconnect error", e); + } + try { + if (mCs != null) { + if (D) Log.d(TAG, "OBEX session close mCs"); + mCs.close(); + if (D) Log.d(TAG, "OBEX session closed"); } - try { - if (mCs != null) { - if (D) Log.d(TAG, "OBEX session close mCs"); - mCs.close(); - if (D) Log.d(TAG, "OBEX session closed"); - } - } catch (IOException e) { - Log.w(TAG, "OBEX session close error" + e); + } catch (IOException e) { + Log.w(TAG, "OBEX session close error", e); } if (mTransport != null) { try { - mTransport.close(); + mTransport.close(); } catch (IOException e) { - Log.e(TAG, "mTransport.close error"); + Log.e(TAG, "mTransport.close error", e); } } @@ -124,7 +124,7 @@ public class BluetoothMnsObexSession { mCs = new ClientSession(mTransport); mConnected = true; } catch (IOException e1) { - Log.e(TAG, "OBEX session create error"); + Log.e(TAG, "OBEX session create error", e1); } if (mConnected) { mConnected = false; @@ -143,8 +143,16 @@ public class BluetoothMnsObexSession { hsConnect = mCs.connect(hs); if (D) Log.d(TAG, "OBEX session created"); mConnected = true; + + long id = mCs.getConnectionID(); + if (D) Log.d(TAG, "Connection ID: " + id); + if (id >= 0) + hsConnect.mConnectionID = ObexHelper.convertToByteArray(id); + else + hsConnect.mConnectionID = new byte[4]; + } catch (IOException e) { - Log.e(TAG, "OBEX session connect error"); + Log.e(TAG, "OBEX session connect error", e); } } synchronized (this) { @@ -175,39 +183,41 @@ public class BluetoothMnsObexSession { InputStream inputStream = null; try { synchronized (this) { - mWaitingForRemote = true; + mWaitingForRemote = true; } + // Send the header first and then the body try { if (V) Log.v(TAG, "Send headerset Event "); putOperation = (ClientOperation)mCs.put(request); // TODO - Should this be kept or Removed - } catch (IOException e) { - Log.e(TAG, "Error when put HeaderSet "); - error = true; - } - synchronized (this) { - mWaitingForRemote = false; - } - if (!error) { - try { - if (V) Log.v(TAG, "Send headerset Event "); - outputStream = putOperation.openOutputStream(); - inputStream = putOperation.openInputStream(); - } catch (IOException e) { - Log.e(TAG, "Error when opening OutputStream"); - error = true; - } - } - - if (!error) { - int position = 0; - int readLength = 0; - boolean okToProceed = true; - long timestamp = 0; - int outputBufferSize = putOperation.getMaxPacketSize(); - byte[] buffer = new byte[outputBufferSize]; + } catch (IOException e) { + Log.e(TAG, "Error when put HeaderSet ", e); + error = true; + } + + synchronized (this) { + mWaitingForRemote = false; + } + if (!error) { + try { + if (V) Log.v(TAG, "Send headerset Event "); + outputStream = putOperation.openOutputStream(); + inputStream = putOperation.openInputStream(); + } catch (IOException e) { + Log.e(TAG, "Error when opening OutputStream", e); + error = true; + } + } + + if (!error) { + int position = 0; + int readLength = 0; + boolean okToProceed = true; + long timestamp = 0; + int outputBufferSize = putOperation.getMaxPacketSize(); + byte[] buffer = new byte[outputBufferSize]; FileInputStream fileInputStream = new FileInputStream(file); @@ -226,27 +236,27 @@ public class BluetoothMnsObexSession { if (V) Log.v(TAG, "Response code is " + responseCode); if (responseCode != ResponseCodes.OBEX_HTTP_CONTINUE && responseCode != ResponseCodes.OBEX_HTTP_OK) { - /* abort happens */ + /* abort happens */ okToProceed = false; } else { position += readLength; if (V) { Log.v(TAG, "Sending file position = " + position - + " readLength " + readLength + " bytes took " - + (System.currentTimeMillis() - timestamp) + " ms"); + + " readLength " + readLength + " bytes took " + + (System.currentTimeMillis() - timestamp) + " ms"); } } } if (position == file.length()) { - Log.i(TAG, "SendFile finished send out file " + file.length() - + " length " + file.length()); - outputStream.close(); + Log.i(TAG, "SendFile finished send out file " + file.length() + + " length " + file.length()); + outputStream.close(); } else { error = true; // TBD - Is Output stream close needed here putOperation.abort(); Log.i(TAG, "SendFile interrupted when send out file " - + " at " + position + " of " + file.length()); + + " at " + position + " of " + file.length()); } } } catch (IOException e) { @@ -270,10 +280,10 @@ public class BluetoothMnsObexSession { inputStream.close(); } if (putOperation != null) { - putOperation.close(); + putOperation.close(); } } catch (IOException e) { - Log.e(TAG, "Error when closing stream after send"); + Log.e(TAG, "Error when closing stream after send", e); } } |