지금은 상태는 스캔을 해서 선택을 하면 페어링이 되는 단계 까지 하였습니다. profilelistener 안에 오버라이드 되어있는 onserviceconnected까지 들어가지긴 하지만 연결을 어떻게 해야할지 모르겠습니다. 정식으로 자바나 안드로이드를 배운적이 없어서 말을 잘 못알아먹습니다. 밑에는 제가 거의 짜집기 해서 만든 코드입니다.
이 이후부터 어떻게 해야할까요??
리스트에서 선택하면 페어링합니다. 그리고 프록시머시기로 줘서 헤드셋에연결합니다.
listDevicesFound.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
BluetoothDevice device = bluetoothDevices.get(position);
try {
Method method = device.getClass().getMethod("createBond", (Class[]) null);
method.invoke(device, (Object[]) null);
connectHeadset(device);
BluetoothSocket mmSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
mmSocket.connect();
//Intent connectDevice = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//startActivityForResult(connectDevice, REQUEST_CONNECT_DEVICE);
bConnectDevice = bluetoothAdapter.getRemoteDevice(device.getAddress());
} catch (Exception e) {
e.printStackTrace();
}
}
});
BluetoothHeadset mBluetoothHeadset;
BluetoothA2dp mBluetoothA2dp;
private BluetoothDevice mDeviceToConnect;
private void connectHeadset(BluetoothDevice device) {
mDeviceToConnect = device;
bluetoothAdapter.getProfileProxy(this, mProfileListener, BluetoothProfile.A2DP);
}
private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
Log.e("profile","profile (1-HEADSET, 2-A2DP : " + profile);
if(profile == BluetoothProfile.A2DP) {
mBluetoothA2dp = (BluetoothA2dp) proxy;
try {
Method connect = BluetoothA2dp.class.getDeclaredMethod("connect", BluetoothDevice.class);
connect.invoke(proxy, mDeviceToConnect);
Log.e("profile", "a2dp connect called");
} catch (Exception e) {
Log.e("profile", "Error : " + e);
}
} else if(profile == BluetoothProfile.HEADSET){
mBluetoothHeadset = (BluetoothHeadset)proxy;
List<BluetoothDevice> list = mBluetoothHeadset.getConnectedDevices();
Log.e("profile", "Headset device num : " + list.size());
for(int i = 0 ; i < list.size() ; i++) {
BluetoothDevice dev = list.get(i);
}
}
}
public void onServiceDisconnected(int profile) {
if(profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null;
}
}
};