2017-10-21 5 views
0

나는 블루투스가 연결된 전화기를 표시하는 응용 프로그램을 만들려고 시도해 왔습니다. 목록 뷰에서 해당 장치를 쌍으로 연결하는 응용 프로그램을 만들려고했습니다. 나는 아직도 일종의 블루투스를 배우기 때문에 문제가 생깁니다.항목의 Bluetooth 쌍 장치 클릭

목록보기에서 목록에 표시된 장치를 어떻게 페어링 할 수 있습니까?

누군가가 downvote로 결정한다면, 왜 내가 downvoted되었습니다 코멘트를 남겨주세요. 도움이나 통찰력에 미리 감사드립니다! :

private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 

     if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { 
      final int state  = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR); 
      final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR); 

      if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING) { 
       showToast("Paired"); 
      } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED){ 
       showToast("Unpaired"); 
      } 
     } 
    } 
}; 

그리고 당신이 청취자의 :

private BluetoothAdapter BA; 
private BluetoothDevice device; 
private Set<BluetoothDevice>pairedDevices; 
private ArrayList list; 
private ArrayAdapter adapter; 
private ListView lv; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    BA = BluetoothAdapter.getDefaultAdapter(); 
    lv = (ListView)findViewById(R.id.listViewPaired); 

    list = new ArrayList(); 

    lv.setOnItemClickListener(this); 
} 


final BroadcastReceiver bReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (BluetoothDevice.ACTION_FOUND.equals(action)){ 
      device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      adapter.add(device.getName() + "\n" + device.getAddress()); 
      adapter.notifyDataSetChanged(); 
     } 
    } 
}; 

public void find(View v) 
{ 
    txtTitle.setText("SEARCHING NEW DEVICES"); 

    adapter.clear(); 

    if (BA.isDiscovering()) { 
     BA.cancelDiscovery(); 
    } 
    else { 
     BA.startDiscovery(); 

     registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 
    } 
} 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    String value = String.valueOf(list.get(position)); 

    Toast.makeText(MainActivity.this, value , Toast.LENGTH_LONG).show(); 
} 
} 
+0

그리고 질문은 ......? – Sergio

+0

@Sergio 죄송합니다. 내 질문이 업데이트되었습니다. 두 장치가 목록보기에 표시 될 때 어떻게 페어링 할 수 있습니까? –

답변

1

, 당신은 페어링이 발생 후 을 실행하는 브로드 캐스트 리시버를 먼저해야합니다 : D

이 내 활동입니다 이 :

lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     BluetoothDevice device = listViewDetected.getItemAtPosition(position); 
     try { 
      Method method = device.getClass().getMethod("createBond", (Class[]) null); 
      method.invoke(device, (Object[]) null); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
}); 
+0

죄송합니다 귀하의 게시물 하하 하하 아주 늦게 회신. 글쎄 선생님, 덕분에 정말 고마워! –