0

I need some advice or direction how to solve this problem. My app has got 3 tabs based on 3 fragment classes and one MainActivity. When I start app, Bluetooth connection is established in MainActivity. For that reason I have got threads which establish connection and start listen for incomming data, this data are also saving in thread in buffer queue. Some other class parses this data and put it in public class, that is accessiable from everywhere.

So my problem is: First tab is called INFO tab, and from info fragment or MainActivity I need to send over BT info request immediately after BT connection is established. But I can't do that on onCreateView method in info fragment, because connection needs few seconds to get establisehd, so it will crash. I have for indicating established connection variable boolean deviceConnected, which is set on true when device is connected.

So, I think that I need some event, that will test variable deviceConnected, and when it will be true, it will send info request (method for sending data over BT is called from MainActivity). I also need some event, that will test when parsed data is available, to referesh UI tab. Please, help! I can give code or another explanation, if you don't understand what I'm looking for.

This is code for fragment tab. When "button" is pressed, we call method from MainActivity for start connection. We also send Intention, but just with msg "blah". How do I tell, that I'm waiting on connected Bluetooth?

public class FragmentA extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
        Button button = (Button) myFragmentView.findViewById(R.id.button1);

        //when button connect is clicked, call method from MainActivity
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //start bluetooth connection in MainActivity    
            ((MainActivity)getActivity()).startConnectionFromActivity();
            final Intent intent = new Intent();
            intent.setAction(android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED);
            getActivity().sendBroadcast(intent);               
            }
        });

        return myFragmentView;
    }

}

Here are my parts of MainActivity:

 public class MainActivity extends Activity {
        private BroadcastReceiver mReceiver;

    @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            IntentFilter intentFilter = new IntentFilter(
                    android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED);
            mReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {

                    //from there should I send request info??

                }
            };
            //registering our receiver
            this.registerReceiver(mReceiver, intentFilter);
        }

        @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            //unregister our receiver
            this.unregisterReceiver(this.mReceiver);
        }
}

If I understand correct, when this is all setup up, when BT will be established, onReceive methd will be fired? So from there I can send info request over bluetooth? Thanks for help!

anze87
  • 253
  • 2
  • 8
  • 19
  • Hi there, bit pushed for time for providing a full answer this morning but your thoughts are bang on- use events. Here is a mini-guide; http://stackoverflow.com/a/10608083/1348379 – BrantApps May 08 '13 at 08:47
  • Yes, this is what I need, I edit my post and add a code, because I don't understand it perfectly, and will be very glad if you help me. Thanks! – anze87 May 08 '13 at 12:40

1 Answers1

0

You can check if a device is connected with like here: How to detect if bluetooth device is connected

And why not implement a own event which informs you about the end of parsing? A start point could be something like this: Android Custom Event

Community
  • 1
  • 1
sandkasten
  • 603
  • 1
  • 8
  • 21
  • I tried to check if device is connected like in this example, but I get this error: 05-09 08:07:25.085: E/AndroidRuntime(12506): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.bluetooth.device.action.ACL_CONNECTED from pid=12506, uid=10212 – anze87 May 09 '13 at 06:09
  • Looke like some permissions are missing in your manifest. Did you add BLUETOOTH and BLUETOOTH_ADMIN? – sandkasten May 13 '13 at 08:08
  • Yes, I add this permitions. I use different solution now, so I didn't need to check ACL_CONNECTED. – anze87 May 14 '13 at 07:15