views:

31

answers:

1

Is there a way that when I start my app, it can figure out if any Bluetooth devices are actively connected to the phone? I want my app to be able to report on an active BT connection by supplying the BT device name.

I cannot find anything in the BT API that seems to be able to handle checking sockets for activity and reporting on active devices. Only seem to be able to track such devices if my app stops all active connections and then tries to establish one while it is running.

Any help here would be appreciated.

A: 

I have the same problem. I have an open source project here: http://code.google.com/p/a2dpvolume/ I need a way to know what remote device(s) are currently active. I would have thought getRemoteDevice([mac address]) would have only returned active remote devices but it actually returns any bonded device. I have searched the Android documentation and examples and still cannot find a way to know what is connected. I can only get the bonded devices.

I now figured out how to use the intent that is sent on the acl_connected intent to find the connected device:

First register to be notified when a bluetooth device connects:

       // create intent filter for a bluetooth stream connection
    IntentFilter filter = new IntentFilter(android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED);
    this.registerReceiver(mReceiver, filter);

Then see what connected

       private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            BluetoothDevice bt = (BluetoothDevice) intent.getExtras().get(BluetoothDevice.EXTRA_DEVICE);
            btConn = bt;                
            if(bt.getBluetoothClass().getDeviceClass() == BluetoothClass.Device.AUDIO_VIDEO_HIFI_AUDIO) 
            {

            //Toast.makeText(context, bt.getName() + bt.getAddress(), Toast.LENGTH_LONG).show();
            getOldvol();
            setVolume(am2.getStreamMaxVolume(AudioManager.STREAM_MUSIC), a2dp.Vol.service.this);                    
            }

          }
        };

Notice the onReceive function receives the context and intent. That intent contains the bluetooth device that just connected. The code above retrieves that device from the intent. The example toast (commented out here) shows how to see the address and name of that connected device. Your program will just need to keep track of that connected and disconnected. I am still looking for a way to query active connections. I think this can be done by looking at all the RFCOMM entries that are active, but not sure.

jroal