项目上的蓝牙配对设备单击

时间:2017-10-21 22:04:56

标签: android bluetooth

我一直在尝试创建一个应用程序来显示打开蓝牙的手机,当我在列表视图中点击其中一个时,它会配对它们。我还在学习蓝牙,所以我有点麻烦

点击列表视图时,如何将列表中显示的设备配对?

如果有人决定进行投票,请发表评论为什么我被投票。在此先感谢您的帮助或见解! :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();
}
}

1 个答案:

答案 0 :(得分: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();
      }
   } 
});