2013-06-13 73 views
1

好吧,我是新来的android,我试图创建一个应用程序,通过蓝牙与arduino接口。我已经看到了蓝牙示例,并看到它如何使用处理程序在“服务”,由它产生的线程和MainActivity之间进行通信。 我的问题是我有多个活动需要使用蓝牙服务。 对于每个活动我有一个这样的处理程序:处理程序和多个活动

 mHandler = new Handler(){ 
     @Override 
     public void handleMessage(Message message) { 
      switch (message.what){ 
      case BtService.CHANGE_STATE: 
       if (message.arg1 == BtService.STATE_CONNECTING){ 
        Intent i = new Intent (MainActivity.this,ConnectedActivity.class); 
        startActivity(i); 

       } 
       break; 
      } 
     } 

    }; 

,并在服务的构造我有这样的:

private BtService(){ 
    btm = BluetoothAdapter.getDefaultAdapter(); 
    mHandler= new Handler(Looper.getMainLooper()); 
} 

,当我需要发送一条消息我这样做:

private synchronized void setState(int state){ 
    mHandler.obtainMessage(CHANGE_STATE, state, -1).sendToTarget(); 
    mState = state; 
} 

但在各种其他处理程序中未收到消息。 在here中声明“特定线程的所有Handler对象都接收相同的消息。”所以我不明白这个问题。 每次开始活动时,我是否都需要传递给Handler在该Activity中声明的服务以使其接收消息?这似乎有效,但对我来说这似乎不是一个好习惯。

回答

0

您可以扩展应用层并使用它来维护线程以检索和管理通过蓝牙连接收集的数据,而不是通过蓝牙连接每个活动。然后,只需在每个活动中使用一个处理程序,以根据应用程序层收集的数据进行刷新。

我唯一使用btAdapter和套接字的活动是实际需要蓝牙信息(在菜单和bt配置活动之后)的第一个活动。

在我的第一个活动onRusume()看起来像这样用注释解释..:

@Override 
public void onResume() { 
super.onResume(); 

Log.d(TAG, "...onResume - try connect..."); 

// Set up a pointer to the remote node using it's address. 
BluetoothDevice device = btAdapter.getRemoteDevice(address); 

// Two things are needed to make a connection: 
// A MAC address, which we got above. 
// A Service ID or UUID. In this case we are using the 
//  UUID for SPP. 
try { 
    btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); 
} catch (IOException e) { 
    errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + "."); 
} 

// Discovery is resource intensive. Make sure it isn't going on 
// when you attempt to connect and pass your message. 
btAdapter.cancelDiscovery(); 

// Establish the connection. This will block until it connects. 
Log.d(TAG, "...Connecting..."); 
try { 
    btSocket.connect(); 
    Log.d(TAG, "....Connection ok..."); 
} catch (IOException e) { 
    try { 
    btSocket.close(); 
    } catch (IOException e2) { 
    errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + "."); 
    } 
} 

// Create a data stream so we can talk to server. 
Log.d(TAG, "...Create Socket..."); 

/** 
* **Here I am kicking off the thread in the application that retrieves all data 
* needed by all my activities. Then it stores the information in its member 
* variables. Each activity then refreshes as often as needed, gets the data from 
* the application layer it needs and does some logic on it.** 
*/ 
if(mConnectedThread == null) { 
    mConnectedThread = app.new ConnectedThread(btSocket); 
    mConnectedThread.start(); 
} 

// This kicks off the handler for this activity that refreshes the activity every 
// xxxx ms and checks the data retrieved from bt in the application layer. 
startUpdatingTicketView(); 
} 

这几乎是我如何得到它为我工作的核心。

只是一个额外的说明...我也尝试这样做与后台服务托管的BT通信,并不能得到它的工作。我确切地忘记了我遇到的问题是什么,并且很可能使用服务也是可行的,但我并没有最终走上这条路。

祝你好运。

+0

很抱歉,如果我没有做我自己很清楚,但是“真实”的问题是:“一个线程的所有处理程序是否都收到相同的消息,正如API培训中所述,或者如果我理解错误是真的。无论如何,谢谢你的回答 – Campig

3

如果你想在所有应用程序中发送消息,你应该使用BroadcastReceiver,我这是你的情况下最好的方法。

Intent intent = new Intent(ApplicationConstants.MY_MESSAGE); 
LocalBroadcastManager.getInstance(context).sendBroadcast(intent); 

的任何活动,接收信息(你CAND在不止一个活动使用)

BroadcastReceiver connectionUpdates = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context arg0, Intent intent) { 

        ...//TODO here 
     } 
    }; 
    LocalBroadcastManager.getInstance(this).registerReceiver(
      connectionUpdates , 
      new IntentFilter(ApplicationConstants.MY_MESSAGE)); 

希望这是有益的

干杯,