마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

블루투스로 블루투스 이어폰이나 스피커에 연결해보려고 하고 있습니다.

0 추천

지금은 상태는 스캔을 해서 선택을 하면 페어링이 되는 단계 까지 하였습니다.  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;
        }
    }
};

 

익명사용자 님이 2020년 1월 22일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...