Thursday, October 31, 2013

[android-developers] BluetoothServerSocket.accept

I am trying to write a Bluetooth android application.  I have used many ideas from the samples and the bluetooth page in the android developers site.

So, I put up a dialog that has the available bluetooth devices available, there is only one, and when I pick it the dialog goes to a method to manage the results of the dialog.

What I want to do is spawn a thread from this method that sits on a call that blocks.  I have the Thread and everything seems good, except the dialog won't go away until the thread has been cancelled.  But I can't cancel it because I am stuck in a blocking dialog. 

I have tried to create a Broadcast receiver to send myself a message, hoping the dialog goes away before I get the message, but that doesn't work either, the dialog still blocks until I remove it with a new instance that doesn't fork the thread.

The question is, is there a good way to delay the thread running until the dialog is down, whenever that is?  Or is what I am doing supposed to work and I have something else wrong.

        //The dialog basically unchanged from the sample
 
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            String[] deviceNames = getArguments().getStringArray("names");
            int position = getArguments().getInt("position", -1);
            if (position == -1) position = 0;
            return new AlertDialog.Builder(getActivity())
                    .setTitle(R.string.select_device)
                    .setPositiveButton(R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                ((BrewActivity) getActivity()).connectChannel();
                            }
                        })
                    .setSingleChoiceItems(deviceNames, position,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                ((BrewActivity) getActivity()).setDevice(which);
                            }
                        }
                    )
                    .create();
        }
    }

    //The connect channel method where I send myself the message

    private void connectChannel() {
        Log.w(TAG, "Connect Channel.");
        mConnectIndicator.setText(mRes.getString(R.string.connected));
        mDataIndicator.setImageLevel(1);
        Intent intent = new Intent();
        intent.setAction("madigan.android.action.CONNECT");
        sendBroadcast(intent);
    }

     //The broadcast receiver that gets the message I send myself.

    private final BroadcastReceiver mConnector = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.w(TAG, "Connect Receiver.");
            final String action = intent.getAction();
            if (action.equals("madigan.android.action.CONNECT" ) ){
                connect();
            }
        }
    };

  // The method called with the success of the broadcast

   private void connect() {
        Log.w(TAG, "Connect.");
        // Starts connection Thread.
        AcceptThread aThread = new AcceptThread();
        aThread.run();
        BluetoothServerSocket socket = connectDevice(mBluetoothAdapter, mDevice);
        try {
            socket.accept();
        }
        catch(IOException e){
            Log.w(TAG, "Accept with Exception.");
        }
        Log.w(TAG, "Accept with No Exception.");
    }

  // and finally the thread

    private class AcceptThread extends Thread {
        private final BluetoothServerSocket mmServerSocket;
    
        public AcceptThread() {
            // Use a temporary object that is later assigned to mmServerSocket,
            // because mmServerSocket is final
            BluetoothServerSocket tmp = null;
            try {
                // MY_UUID is the app's UUID string, also used by the client code
                tmp = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(mDevice.getName(), UUID.randomUUID());
            } catch (IOException e) { }
            mmServerSocket = tmp;
        }
    
        public void run() {
            BluetoothSocket socket = null;
            // Keep listening until exception occurs or a socket is returned
            while (true) {
                try {
                    socket = mmServerSocket.accept();
                } catch (IOException e) {
                    break;
                }
                // If a connection was accepted
                if (socket != null) {
                    // Do work to manage the connection (in a separate thread)
                    manageConnectedSocket(socket);
                    try {
                      mmServerSocket.close();
                    }
                    catch (IOException e){
                        break;
                    }
                    break;
                }
            }
        }
    
        /** Will cancel the listening socket, and cause the thread to finish */
        public void cancel() {
            try {
                mmServerSocket.close();
            } catch (IOException e) { }
        }
    }

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate