Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 项目上的蓝牙配对设备单击_Android_Bluetooth - Fatal编程技术网

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

Android 项目上的蓝牙配对设备单击,android,bluetooth,Android,Bluetooth,我一直在尝试创建一个应用程序,该应用程序将显示打开蓝牙的手机,当我在listview中点击其中一个手机时,它会将它们配对。我还在学习蓝牙,所以我有点麻烦 当我在listview上点击列表中显示的设备时,如何将其配对 如果有人决定投反对票,请留下我为什么被否决的评论。提前感谢您提供的任何帮助或见解!:D 这是我的活动: private BluetoothAdapter BA; private BluetoothDevice device; private Set<BluetoothDevic

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

当我在listview上点击列表中显示的设备时,如何将其配对

如果有人决定投反对票,请留下我为什么被否决的评论。提前感谢您提供的任何帮助或见解!: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();
}
}

首先,您需要一个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();
      }
   } 
});

问题是……?@Sergio抱歉,先生,更新了我的问题。当这两个设备在列表视图中显示时,我如何才能将其配对。很抱歉回复您的帖子太晚了,先生,哈哈。很好,先生,谢谢,我真的很感激!