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

안드로이드 스튜디오 블루투스에 관하여 질문

0 추천

깃허브에서 코드를 구하여 실행하여 보았는데  for 부분의 for( BluetoothDevice device : pairedDeivce)에서 BluetoothDevice device이부분이 에러가 납니다.  왜 에러가 나오는지 잘 모르겠습니다.

에러는 error: incompatible types: Object cannot be converted to BluetoothDevice이라고 뜹니다.

public class DeviceListActivity extends Activity {
    
    public static String EXTRA_DEVICE_ADDRESS = "device_address";
    private BluetoothAdapter mBtAdapter;//블루투스 어뎁터
    private ArrayAdapter mPairedDevicesArrayAdapter;//페어링된 기기
    private ArrayAdapter mNewDevicesArrayAdapter;//새로 발견한 기기
    Set<BluetoothDevice> mPairedDevices;
    BluetoothDevice mBluetoothDevice;
    BluetoothSocket mBluetoothSocket;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.device_list);
        setResult(Activity.RESULT_CANCELED);

        Button scanButton = (Button) findViewById(R.id.button_scan);
        scanButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                doDiscovery();
                v.setVisibility(View.GONE);//버튼을 보이지 않게한다.
            }
        });

        mPairedDevicesArrayAdapter = new ArrayAdapter(this, R.layout.device_name);

        mNewDevicesArrayAdapter = new ArrayAdapter(this, R.layout.device_name);

        ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
        pairedListView.setAdapter(mPairedDevicesArrayAdapter);
        pairedListView.setOnItemClickListener(mDeviceClickListener);

        ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
        newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
        newDevicesListView.setOnItemClickListener(mDeviceClickListener);

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(mReceiver, filter);

        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(mReceiver, filter);

        mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        Set pairedDevices = mBtAdapter.getBondedDevices();

        if (pairedDevices.size() > 0) {           findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
            for (BluetoothDevice device : pairedDevices) {
                mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        } else {
            String noDevices = getResources().getText(R.string.none_paired).toString();
            mPairedDevicesArrayAdapter.add(noDevices);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Discovering 작업을 취소한다.
        if (mBtAdapter != null) {
            mBtAdapter.cancelDiscovery();
        }
        this.unregisterReceiver(mReceiver);
    }
    private void doDiscovery() {
        // 제목에 스케닝 상태 출력하기
        setProgressBarIndeterminateVisibility(true);
        setTitle(R.string.scanning);
        // Turn on sub-title for new devices
        findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
        // 이미 Discovering하고있다면 취소.
        if (mBtAdapter.isDiscovering()) {
            mBtAdapter.cancelDiscovery();
        }
        // Discovering 시작
        mBtAdapter.startDiscovery();
    }
    private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
        public void onItemClick(AdapterView av, View v, int arg2, long arg3) {

            mBtAdapter.cancelDiscovery();
       
            String info = ((TextView) v).getText().toString();
            String address = info.substring(info.length() - 17);

            Intent intent = new Intent();
            intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
           
            setResult(Activity.RESULT_OK, intent);
            finish();
        }
    };
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                  //새로운 장비라면 출력하기 위해서
                    mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                }
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                setProgressBarIndeterminateVisibility(false);
                setTitle(R.string.select_device);
                if (mNewDevicesArrayAdapter.getCount() == 0) {
                    String noDevices = getResources().getText(R.string.none_found).toString();
                    mNewDevicesArrayAdapter.add(noDevices);
                }
            }
        }
    };
}
jh7899 (160 포인트) 님이 11월 29일 질문

1개의 답변

0 추천

직접 실행해보지 않아서 확실하진 않으나 

아래 부분을

Set pairedDevices = mBtAdapter.getBondedDevices();

이렇게 해보실래요?

Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();

아니면

Set<BluetoothDevice> pairedDevices = (BluetoothDevice) mBtAdapter.getBondedDevices();
익명사용자 님이 5일 답변
...