Cách đây một thời gian ngắn, tôi đã gặp sự cố tương tự khi thử kết nối thiết bị bluetooth với điện thoại Android. Mặc dù hồ sơ thiết bị của bạn khác nhau, tôi nghĩ rằng giải pháp là như nhau.
Trước tiên, bạn cần tạo ra một gói phần mềm trong dự án của bạn được đặt tên android.bluetooth
và đặt sau IBluetoothA2dp.aidl trong đó:
package android.bluetooth;
import android.bluetooth.BluetoothDevice;
/**
* System private API for Bluetooth A2DP service
*
* {@hide}
*/
interface IBluetoothA2dp {
boolean connectSink(in BluetoothDevice device);
boolean disconnectSink(in BluetoothDevice device);
boolean suspendSink(in BluetoothDevice device);
boolean resumeSink(in BluetoothDevice device);
BluetoothDevice[] getConnectedSinks();
BluetoothDevice[] getNonDisconnectedSinks();
int getSinkState(in BluetoothDevice device);
boolean setSinkPriority(in BluetoothDevice device, int priority);
int getSinkPriority(in BluetoothDevice device);
boolean connectSinkInternal(in BluetoothDevice device);
boolean disconnectSinkInternal(in BluetoothDevice device);
}
Sau đó, để truy cập vào các chức năng, đặt lớp sau trong dự án của bạn:
public class BluetoothA2dpConnection {
private IBluetoothA2dp mService = null;
public BluetoothA2dpConnection() {
try {
Class<?> classServiceManager = Class.forName("android.os.ServiceManager");
Method methodGetService = classServiceManager.getMethod("getService", String.class);
IBinder binder = (IBinder) methodGetService.invoke(null, "bluetooth_a2dp");
mService = IBluetoothA2dp.Stub.asInterface(binder);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public boolean connect(BluetoothDevice device) {
if (mService == null || device == null) {
return false;
}
try {
mService.connectSink(device);
} catch (RemoteException e) {
e.printStackTrace();
return false;
}
return true;
}
public boolean disconnect(BluetoothDevice device) {
if (mService == null || device == null) {
return false;
}
try {
mService.disconnectSink(device);
} catch (RemoteException e) {
e.printStackTrace();
return false;
}
return true;
}
}
Cuối cùng, để kết nối điện thoại của bạn A2DP, chọn một Blu etoothDevice từ danh sách các thiết bị được ghép đôi và gửi nó làm tham số của phương thức connect
. Đảm bảo chọn thiết bị có cấu hình chính xác, nếu không bạn sẽ có ngoại lệ.
Tôi đã thử nghiệm giải pháp này trong điện thoại có phiên bản Android 2.3 và nó hoạt động tốt.
Xin lỗi vì bất kỳ lỗi nào về tiếng Anh. Tôi hy vọng điều này có thể giúp bạn.
điện thoại này là gì, cài đặt có xuất hiện trên dòng thiết bị nexus không? – nandeesh
Chiếc điện thoại tôi đang sử dụng là Galaxy S3 w/AT & T. Tôi không thể chắc chắn liệu nó sẽ có mặt ở đó, nhưng tôi sẽ tưởng tượng nó sẽ xảy ra. – JamesB41