1

i have following function run on start and work perfectly.

i would like to add if condition in other function in order to check if device is still connected.

here is code

private final  Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case BluetoothService.MESSAGE_STATE_CHANGE:
            switch (msg.arg1) {
            case BluetoothService.STATE_CONNECTED:   
                Toast.makeText(getApplicationContext(), "Connect successful",
                        Toast.LENGTH_SHORT).show();
                btnClose.setEnabled(true);
                btnSend.setEnabled(true);
                btnSendDraw.setEnabled(true);
                break;
            case BluetoothService.STATE_CONNECTING:  
                Log.d("À¶ÑÀµ÷ÊÔ","ÕýÔÚÁ¬½Ó.....");
                break;
            case BluetoothService.STATE_LISTEN:     
            case BluetoothService.STATE_NONE:
                Log.d("À¶ÑÀµ÷ÊÔ","µÈ´ýÁ¬½Ó.....");
                break;
            }
            break;
        case BluetoothService.MESSAGE_CONNECTION_LOST: 
            Toast.makeText(getApplicationContext(), "Device connection was lost",
                           Toast.LENGTH_SHORT).show();
            btnClose.setEnabled(false);
            btnSend.setEnabled(false);
            btnSendDraw.setEnabled(false);
            break;
        case BluetoothService.MESSAGE_UNABLE_CONNECT:  
            Toast.makeText(getApplicationContext(), "Unable to connect device",
                    Toast.LENGTH_SHORT).show();
            break;
        }
    }

};

other function is start like this

     @Override
     protected void onPostExecute(String result){

please help me!!

Thanks

bjiang
  • 6,068
  • 2
  • 22
  • 35

1 Answers1

0

ACL_CONNECTED/DISCONNECTED is not exactly reliable, I have learned by experience, because this might happen several times during a device's connection (example, if pin is required, you will get "connected", and then "disconnected" if timeout/wrong pin supplied. This does not necessarily indicate connection proper). This indicates the lower layer connection.

If you want to use a broadcast receiver, it would be better to use the broadcast that is specific to the profile that you have used (example A2DP). Each has it's own broadcast. Another thing you can listen to is Connectivity_state_changed from ConnectivityManager, for the type BLUETOOTH (haven't really tried this one).

Also, the way to check if it is connected, without a broadcast receiver, say, when you want to check in the background before updating an activity, would be to obtain the profile object, via something like:

mBluetoothAdapter.getProfileProxy(mContext, mA2DPProfileListener, BluetoothProfile.A2DP);

where mA2DPProfileListener is a ServiceListener object:

<code>
private ServiceListener mA2DPProfileListener = new ServiceListener(){
//anonymous inner type. etc.
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        //cast the BluetoothProfile object to the profile you need, say 
        //BluetoothA2DP proxyA2DP = (BluetoothA2DP) proxy;
        int currentState = proxyA2DP.getConnectionState(mDevice);
        //mDevice is the BluetoothDevice object you can get from
        //BluetoothAdapter.getRemote...
    }

    public void onServiceDisconnected(int profile) {
    }
}
</code>

you can check what currentState points to, and determine if the device is connected/disconnected/connecting, etc.

HTH, Sreedevi.

Sreedevi J
  • 673
  • 1
  • 9
  • 15