2017-10-21 75 views
0

我一直在试图创建一个应用程序,它将显示有蓝牙功能的手机,当我在列表视图中点击其中一个时,它们会将它们配对。我仍然在学习蓝牙,所以我有点麻烦项目上的蓝牙双设备点击

我怎么能配对列表中显示的设备,当我点击它的列表视图?

如果有人决定downvote,请留下评论为什么我downvoted。预先感谢任何帮助或见解! :d

这是我的活动:

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

首先,你需要一个BroadcastReceiver,将执行后配对发生:

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"); 
      } 
     } 
    } 
}; 

而且在你做听众这个:

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

对不起,对你的帖子先生哈哈很晚回复。它的工作先生,谢谢我真的很感激它! –