我尝试在android中连接speakerBluetooth(我使用UE BOOM Speaker),该设备已配对,然后我将此设备连接到我的Android手机,通过蓝牙播放音乐。但我失败了,这段代码可以连接,但不能在mySpeaker中播放。我使用的是android 4.2(软糖)。
这是我的代码:
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();
}
}
}
}
}
我在舱单中的许可:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
有什么建议吗?
问候。