0

I try to connect speakerBluetooth (I using UE BOOM Speaker) in android, that device is paired and then I'll connect this device to my android phone for playing music via bluetooth. but I failed, this code can connect, but not play in mySpeaker. I using android 4.2 (jellybean).

it's my code :

Set<BluetoothDevice> pairedDevice;
BluetoothAdapter mBtAdapter;
BluetoothSocket socket;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    pairedDevice = mBtAdapter.getBondedDevices();
//if paired device > 0 
    if (pairedDevice.size() > 0) {
//show paired device
        for (BluetoothDevice device : pairedDevice) {
//try connect to UE BOOM Speaker
            if (device.getName().equals("UE BOOM")) {
                Method m;
                BluetoothDevice hxm = BluetoothAdapter.getDefaultAdapter()
                        .getRemoteDevice(device.getAddress());
                try {
                    m = hxm.getClass().getMethod("createRfcommSocket",
                            new Class[] { int.class });
                    socket = (BluetoothSocket) m.invoke(device, 1);

                    socket.connect();
                    Toast.makeText(this, "connect", Toast.LENGTH_SHORT)
                            .show();
                    socket.close();
                } catch (Exception ex) {
                    Toast.makeText(this, "not connect", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        }
    }
}

My Permission in manifest:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Any suggestion?

Regards.

Yossi
  • 37
  • 9
  • The android OS should handle everything automatically: it should allow the user to pair with the speaker, and then should automatically connect to the speaker and pipe audio through it whenever it detects it near by. It should not be necessary to code anything, unless I'm missing something? – Jodes Oct 07 '14 at 10:18
  • The bluetooth speakers, we have to connect the bluetoothSpeaker through settings. but I want to try more simple to create applications without having to go to settings. Thanks :) – Yossi Oct 07 '14 at 14:53
  • Try following this previous question: http://stackoverflow.com/questions/22226552/programmatically-connect-to-paired-bluetooth-speaker-and-play-audio – Jodes Oct 08 '14 at 16:40
  • Thanks! :) I've tried it. but have not found a solution. it can play a song when it is connected to SpeakerBluetooth. – Yossi Oct 09 '14 at 07:54

0 Answers0