2013-05-08 55 views
0

我需要一些建议或指导如何解决这个问题。我的应用有3个基于3个片段类和一个MainActivity的选项卡。当我启动应用程序时,在MainActivity中建立蓝牙连接。因为这个原因,我得到了建立连接的线程并开始监听数据,这些数据也保存在缓冲队列中的线程中。其他一些班级解析这些数据并将其放入公共班级,这些数据可从任何地方访问。标签片段中的事件?

所以我的问题是:第一个选项卡被称为INFO选项卡,并从信息片段或MainActivity我需要BT连接建立后立即发送BT信息请求。但我不能在info fragment中的onCreateView方法上做到这一点,因为连接需要几秒钟才能获得establisehd,所以它会崩溃。我必须指出已建立的连接变量布尔deviceConnected,它在设备连接时设置为true。因此,我认为我需要一些事件,它将测试变量deviceConnected,当它将成为true时,它将发送信息请求(通过BT发送数据的方法从MainActivity中调用)。我还需要一些事件,它将测试分析数据何时可用,以重新添加UI选项卡。请帮忙!如果你不明白我在找什么,我可以提供代码或其他解释。

这是片段选项卡的代码。当按下“按钮”时,我们从MainActivity调用方法来启动连接。我们也发送意向,但只是味精“等等”。我如何知道,我正在等待连接的蓝牙?

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; 
    } 

} 

这里是我的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); 
     } 
} 

如果我理解正确的,如果这是所有设置后,当BT将建立,的onReceive收作方法初探会被解雇?那么从那里我可以通过蓝牙发送信息请求?感谢帮助!

+0

你好,有点推动时间提供一个完整的答案今天上午,但你的想法是轰轰烈烈的使用事件。这是一个迷你指南; http://stackoverflow.com/a/10608083/1348379 – OceanLife 2013-05-08 08:47:00

+0

是的,这是我所需要的,我编辑我的帖子并添加一个代码,因为我不完全理解它,并且如果你帮助我会很高兴。谢谢! – anze87 2013-05-08 12:40:16

回答

0

您可以检查设备是否有喜欢这里连接:How to detect if bluetooth device is connected

为什么不实现自己的事件,告知您解析的结束?一个起点可能是这样的:Android Custom Event

+0

我试图检查设备是否连接,就像在这个例子中,但我得到这个错误:05-09 08:07:25.085:E/AndroidRuntime(12506):java.lang.SecurityException:权限拒绝:不允许发送广播android.bluetooth.device.action.ACL_CONNECTED从pid = 12506,uid = 10212 – anze87 2013-05-09 06:09:54

+0

您的清单中缺少一些权限丢失的权限。你添加了BLUETOOTH和BLUETOOTH_ADMIN吗? – sandkasten 2013-05-13 08:08:17

+0

是的,我添加了这个permitions。我现在使用不同的解决方案,所以我不需要检查ACL_CONNECTED。 – anze87 2013-05-14 07:15:57