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 ;
@@ -143,6 +144,11 @@ public class BluetoothService extends IBluetooth.Stub {
143144 private static String mDockAddress ;
144145 private String mDockPin ;
145146
147+ private static final String INCOMING_CONNECTION_FILE =
148+ "/data/misc/bluetooth/incoming_connection.conf" ;
149+ private HashMap <String , Pair <Integer , String >> mIncomingConnections ;
150+
151+
146152 private static class RemoteService {
147153 public String address ;
148154 public ParcelUuid uuid ;
@@ -210,6 +216,7 @@ public BluetoothService(Context context) {
210216
211217 filter .addAction (Intent .ACTION_DOCK_EVENT );
212218 mContext .registerReceiver (mReceiver , filter );
219+ mIncomingConnections = new HashMap <String , Pair <Integer , String >>();
213220 }
214221
215222 public static synchronized String readDockBluetoothAddress () {
@@ -743,8 +750,6 @@ public synchronized void setBondState(String address, int state, int reason) {
743750
744751 if (state == BluetoothDevice .BOND_BONDED ) {
745752 addProfileState (address );
746- } else if (state == BluetoothDevice .BOND_NONE ) {
747- removeProfileState (address );
748753 }
749754
750755 if (DBG ) log (address + " bond state " + oldState + " -> " + state + " (" +
@@ -1326,6 +1331,8 @@ public synchronized boolean removeBond(String address) {
13261331 }
13271332
13281333 public synchronized boolean removeBondInternal (String address ) {
1334+ // Unset the trusted device state and then unpair
1335+ setTrust (address , false );
13291336 return removeDeviceNative (getObjectPathFromAddress (address ));
13301337 }
13311338
@@ -2175,10 +2182,6 @@ private BluetoothDeviceProfileState addProfileState(String address) {
21752182 return state ;
21762183 }
21772184
2178- private void removeProfileState (String address ) {
2179- mDeviceProfileState .remove (address );
2180- }
2181-
21822185 private void initProfileState () {
21832186 String []bonds = null ;
21842187 String val = getPropertyInternal ("Devices" );
@@ -2227,6 +2230,11 @@ public boolean notifyIncomingConnection(String address) {
22272230 mA2dpService = a2dpService ;
22282231 }
22292232
2233+ /*package*/ Integer getAuthorizationAgentRequestData (String address ) {
2234+ Integer data = mEventLoop .getAuthorizationAgentRequestData ().remove (address );
2235+ return data ;
2236+ }
2237+
22302238 public void sendProfileStateMessage (int profile , int cmd ) {
22312239 Message msg = new Message ();
22322240 msg .what = cmd ;
@@ -2237,6 +2245,116 @@ public void sendProfileStateMessage(int profile, int cmd) {
22372245 }
22382246 }
22392247
2248+ private void createIncomingConnectionStateFile () {
2249+ File f = new File (INCOMING_CONNECTION_FILE );
2250+ if (!f .exists ()) {
2251+ try {
2252+ f .createNewFile ();
2253+ } catch (IOException e ) {
2254+ Log .e (TAG , "IOException: cannot create file" );
2255+ }
2256+ }
2257+ }
2258+
2259+ /** @hide */
2260+ public Pair <Integer , String > getIncomingState (String address ) {
2261+ if (mIncomingConnections .isEmpty ()) {
2262+ createIncomingConnectionStateFile ();
2263+ readIncomingConnectionState ();
2264+ }
2265+ return mIncomingConnections .get (address );
2266+ }
2267+
2268+ private void readIncomingConnectionState () {
2269+ synchronized (mIncomingConnections ) {
2270+ FileInputStream fstream = null ;
2271+ try {
2272+ fstream = new FileInputStream (INCOMING_CONNECTION_FILE );
2273+ DataInputStream in = new DataInputStream (fstream );
2274+ BufferedReader file = new BufferedReader (new InputStreamReader (in ));
2275+ String line ;
2276+ while ((line = file .readLine ()) != null ) {
2277+ line = line .trim ();
2278+ if (line .length () == 0 ) continue ;
2279+ String [] value = line .split ("," );
2280+ if (value != null && value .length == 3 ) {
2281+ Integer val1 = Integer .parseInt (value [1 ]);
2282+ Pair <Integer , String > val = new Pair (val1 , value [2 ]);
2283+ mIncomingConnections .put (value [0 ], val );
2284+ }
2285+ }
2286+ } catch (FileNotFoundException e ) {
2287+ log ("FileNotFoundException: readIncomingConnectionState" + e .toString ());
2288+ } catch (IOException e ) {
2289+ log ("IOException: readIncomingConnectionState" + e .toString ());
2290+ } finally {
2291+ if (fstream != null ) {
2292+ try {
2293+ fstream .close ();
2294+ } catch (IOException e ) {
2295+ // Ignore
2296+ }
2297+ }
2298+ }
2299+ }
2300+ }
2301+
2302+ private void truncateIncomingConnectionFile () {
2303+ RandomAccessFile r = null ;
2304+ try {
2305+ r = new RandomAccessFile (INCOMING_CONNECTION_FILE , "rw" );
2306+ r .setLength (0 );
2307+ } catch (FileNotFoundException e ) {
2308+ log ("FileNotFoundException: truncateIncomingConnectionState" + e .toString ());
2309+ } catch (IOException e ) {
2310+ log ("IOException: truncateIncomingConnectionState" + e .toString ());
2311+ } finally {
2312+ if (r != null ) {
2313+ try {
2314+ r .close ();
2315+ } catch (IOException e ) {
2316+ // ignore
2317+ }
2318+ }
2319+ }
2320+ }
2321+
2322+ /** @hide */
2323+ public void writeIncomingConnectionState (String address , Pair <Integer , String > data ) {
2324+ synchronized (mIncomingConnections ) {
2325+ mIncomingConnections .put (address , data );
2326+
2327+ truncateIncomingConnectionFile ();
2328+ BufferedWriter out = null ;
2329+ StringBuilder value = new StringBuilder ();
2330+ try {
2331+ out = new BufferedWriter (new FileWriter (INCOMING_CONNECTION_FILE , true ));
2332+ for (String devAddress : mIncomingConnections .keySet ()) {
2333+ Pair <Integer , String > val = mIncomingConnections .get (devAddress );
2334+ value .append (devAddress );
2335+ value .append ("," );
2336+ value .append (val .first .toString ());
2337+ value .append ("," );
2338+ value .append (val .second );
2339+ value .append ("\n " );
2340+ }
2341+ out .write (value .toString ());
2342+ } catch (FileNotFoundException e ) {
2343+ log ("FileNotFoundException: writeIncomingConnectionState" + e .toString ());
2344+ } catch (IOException e ) {
2345+ log ("IOException: writeIncomingConnectionState" + e .toString ());
2346+ } finally {
2347+ if (out != null ) {
2348+ try {
2349+ out .close ();
2350+ } catch (IOException e ) {
2351+ // Ignore
2352+ }
2353+ }
2354+ }
2355+ }
2356+ }
2357+
22402358 private static void log (String msg ) {
22412359 Log .d (TAG , msg );
22422360 }
@@ -2287,4 +2405,5 @@ private native int addRfcommServiceRecordNative(String name, long uuidMsb, long
22872405 short channel );
22882406 private native boolean removeServiceRecordNative (int handle );
22892407 private native boolean setLinkTimeoutNative (String path , int num_slots );
2408+ native boolean setAuthorizationNative (String address , boolean value , int data );
22902409}
0 commit comments