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.