2727import android .bluetooth .BluetoothAdapter ;
2828import android .bluetooth .BluetoothClass ;
2929import android .bluetooth .BluetoothDevice ;
30- import android .bluetooth .BluetoothHeadset ;
3130import android .bluetooth .BluetoothDeviceProfileState ;
31+ import android .bluetooth .BluetoothHeadset ;
3232import android .bluetooth .BluetoothProfileState ;
3333import android .bluetooth .BluetoothSocket ;
3434import android .bluetooth .BluetoothUuid ;
6767import java .io .IOException ;
6868import java .io .InputStreamReader ;
6969import java .io .PrintWriter ;
70+ import java .io .RandomAccessFile ;
7071import java .io .UnsupportedEncodingException ;
7172import java .util .ArrayList ;
7273import java .util .Arrays ;
@@ -142,6 +143,11 @@ public class BluetoothService extends IBluetooth.Stub {
142143 private static String mDockAddress ;
143144 private String mDockPin ;
144145
146+ private static final String INCOMING_CONNECTION_FILE =
147+ "/data/misc/bluetooth/incoming_connection.conf" ;
148+ private HashMap <String , Pair <Integer , String >> mIncomingConnections ;
149+
150+
145151 private static class RemoteService {
146152 public String address ;
147153 public ParcelUuid uuid ;
@@ -209,6 +215,7 @@ public BluetoothService(Context context) {
209215
210216 filter .addAction (Intent .ACTION_DOCK_EVENT );
211217 mContext .registerReceiver (mReceiver , filter );
218+ mIncomingConnections = new HashMap <String , Pair <Integer , String >>();
212219 }
213220
214221 public static synchronized String readDockBluetoothAddress () {
@@ -733,8 +740,6 @@ public synchronized void setBondState(String address, int state, int reason) {
733740
734741 if (state == BluetoothDevice .BOND_BONDED ) {
735742 addProfileState (address );
736- } else if (state == BluetoothDevice .BOND_NONE ) {
737- removeProfileState (address );
738743 }
739744
740745 if (DBG ) log (address + " bond state " + oldState + " -> " + state + " (" +
@@ -1312,6 +1317,8 @@ public synchronized boolean removeBond(String address) {
13121317 }
13131318
13141319 public synchronized boolean removeBondInternal (String address ) {
1320+ // Unset the trusted device state and then unpair
1321+ setTrust (address , false );
13151322 return removeDeviceNative (getObjectPathFromAddress (address ));
13161323 }
13171324
@@ -2161,10 +2168,6 @@ private BluetoothDeviceProfileState addProfileState(String address) {
21612168 return state ;
21622169 }
21632170
2164- private void removeProfileState (String address ) {
2165- mDeviceProfileState .remove (address );
2166- }
2167-
21682171 private void initProfileState () {
21692172 String []bonds = null ;
21702173 String val = getPropertyInternal ("Devices" );
@@ -2213,6 +2216,11 @@ public boolean notifyIncomingConnection(String address) {
22132216 mA2dpService = a2dpService ;
22142217 }
22152218
2219+ /*package*/ Integer getAuthorizationAgentRequestData (String address ) {
2220+ Integer data = mEventLoop .getAuthorizationAgentRequestData ().remove (address );
2221+ return data ;
2222+ }
2223+
22162224 public void sendProfileStateMessage (int profile , int cmd ) {
22172225 Message msg = new Message ();
22182226 msg .what = cmd ;
@@ -2223,6 +2231,116 @@ public void sendProfileStateMessage(int profile, int cmd) {
22232231 }
22242232 }
22252233
2234+ private void createIncomingConnectionStateFile () {
2235+ File f = new File (INCOMING_CONNECTION_FILE );
2236+ if (!f .exists ()) {
2237+ try {
2238+ f .createNewFile ();
2239+ } catch (IOException e ) {
2240+ Log .e (TAG , "IOException: cannot create file" );
2241+ }
2242+ }
2243+ }
2244+
2245+ /** @hide */
2246+ public Pair <Integer , String > getIncomingState (String address ) {
2247+ if (mIncomingConnections .isEmpty ()) {
2248+ createIncomingConnectionStateFile ();
2249+ readIncomingConnectionState ();
2250+ }
2251+ return mIncomingConnections .get (address );
2252+ }
2253+
2254+ private void readIncomingConnectionState () {
2255+ synchronized (mIncomingConnections ) {
2256+ FileInputStream fstream = null ;
2257+ try {
2258+ fstream = new FileInputStream (INCOMING_CONNECTION_FILE );
2259+ DataInputStream in = new DataInputStream (fstream );
2260+ BufferedReader file = new BufferedReader (new InputStreamReader (in ));
2261+ String line ;
2262+ while ((line = file .readLine ()) != null ) {
2263+ line = line .trim ();
2264+ if (line .length () == 0 ) continue ;
2265+ String [] value = line .split ("," );
2266+ if (value != null && value .length == 3 ) {
2267+ Integer val1 = Integer .parseInt (value [1 ]);
2268+ Pair <Integer , String > val = new Pair (val1 , value [2 ]);
2269+ mIncomingConnections .put (value [0 ], val );
2270+ }
2271+ }
2272+ } catch (FileNotFoundException e ) {
2273+ log ("FileNotFoundException: readIncomingConnectionState" + e .toString ());
2274+ } catch (IOException e ) {
2275+ log ("IOException: readIncomingConnectionState" + e .toString ());
2276+ } finally {
2277+ if (fstream != null ) {
2278+ try {
2279+ fstream .close ();
2280+ } catch (IOException e ) {
2281+ // Ignore
2282+ }
2283+ }
2284+ }
2285+ }
2286+ }
2287+
2288+ private void truncateIncomingConnectionFile () {
2289+ RandomAccessFile r = null ;
2290+ try {
2291+ r = new RandomAccessFile (INCOMING_CONNECTION_FILE , "rw" );
2292+ r .setLength (0 );
2293+ } catch (FileNotFoundException e ) {
2294+ log ("FileNotFoundException: truncateIncomingConnectionState" + e .toString ());
2295+ } catch (IOException e ) {
2296+ log ("IOException: truncateIncomingConnectionState" + e .toString ());
2297+ } finally {
2298+ if (r != null ) {
2299+ try {
2300+ r .close ();
2301+ } catch (IOException e ) {
2302+ // ignore
2303+ }
2304+ }
2305+ }
2306+ }
2307+
2308+ /** @hide */
2309+ public void writeIncomingConnectionState (String address , Pair <Integer , String > data ) {
2310+ synchronized (mIncomingConnections ) {
2311+ mIncomingConnections .put (address , data );
2312+
2313+ truncateIncomingConnectionFile ();
2314+ BufferedWriter out = null ;
2315+ StringBuilder value = new StringBuilder ();
2316+ try {
2317+ out = new BufferedWriter (new FileWriter (INCOMING_CONNECTION_FILE , true ));
2318+ for (String devAddress : mIncomingConnections .keySet ()) {
2319+ Pair <Integer , String > val = mIncomingConnections .get (devAddress );
2320+ value .append (devAddress );
2321+ value .append ("," );
2322+ value .append (val .first .toString ());
2323+ value .append ("," );
2324+ value .append (val .second );
2325+ value .append ("\n " );
2326+ }
2327+ out .write (value .toString ());
2328+ } catch (FileNotFoundException e ) {
2329+ log ("FileNotFoundException: writeIncomingConnectionState" + e .toString ());
2330+ } catch (IOException e ) {
2331+ log ("IOException: writeIncomingConnectionState" + e .toString ());
2332+ } finally {
2333+ if (out != null ) {
2334+ try {
2335+ out .close ();
2336+ } catch (IOException e ) {
2337+ // Ignore
2338+ }
2339+ }
2340+ }
2341+ }
2342+ }
2343+
22262344 private static void log (String msg ) {
22272345 Log .d (TAG , msg );
22282346 }
@@ -2273,4 +2391,5 @@ private native int addRfcommServiceRecordNative(String name, long uuidMsb, long
22732391 short channel );
22742392 private native boolean removeServiceRecordNative (int handle );
22752393 private native boolean setLinkTimeoutNative (String path , int num_slots );
2394+ native boolean setAuthorizationNative (String address , boolean value , int data );
22762395}
0 commit comments