Android设备蓝牙配对2

时间:2014-06-12 19:48:16

标签: android bluetooth

我想在点击时与他们配对。但我的应用程序崩溃了。

我已经给予蓝牙许可。请帮助我!

导致错误:

06-12 19:34:40.997:E / AndroidRuntime(4172):引起:java.lang.NullPointerException 06-12 19:34:40.997:E / AndroidRuntime(4172):at com.example.bluetooth_tutorialplspoint.MainActivity.getPairedDevices(MainActivity.java:158)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    on = (Button) findViewById(R.id.btnON);
    off = (Button) findViewById(R.id.btnOFF);
    Visible = (Button) findViewById(R.id.btnVisible);
    list = (Button) findViewById(R.id.btnList);
    lv = (ListView) findViewById(R.id.listView1);
    arrayListpaired=new ArrayList<String>();
    getPairedDevices();
    listeamclik=new ListIteamClick();


    BA = BluetoothAdapter.getDefaultAdapter();

    on.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (!BA.isEnabled()) {
                Intent turnOn = new Intent(
                        BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(turnOn, 0);
                Toast.makeText(getApplicationContext(), "Turned on",
                        Toast.LENGTH_LONG).show();

            } else {
                Toast.makeText(getApplicationContext(), "Already on",
                        Toast.LENGTH_LONG).show();
            }

        }
    });

    off.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            BA.disable();
            Toast.makeText(getApplicationContext(), "Turned off",
                    Toast.LENGTH_LONG).show();
        }
    });

    Visible.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent getVisible = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            startActivityForResult(getVisible, 0);

        }
    });

    list.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            pairedDeives = BA.getBondedDevices();

            if (pairedDeives != null) {

                ArrayList list = new ArrayList();
                for (BluetoothDevice bt : pairedDeives) {
                    list.add(bt.getName());

                    arraylistBloothdevice.add(bt);

                    Toast.makeText(getApplicationContext(),
                            "Showing paired Devices", Toast.LENGTH_LONG)
                            .show();
                     adapter = new ArrayAdapter(
                            getApplicationContext(),
                            R.layout.custom_textview, list);
                    lv.setAdapter(adapter);

                }
            }
        }

    });


}


public class ListIteamClick implements OnItemClickListener{

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        btDevice=arraylistBloothdevice.get(position);
        Boolean isBonded=false;
        try {
            isBonded=createBond(btDevice);
            if(isBonded){
                getPairedDevices();
                adapter.notifyDataSetChanged();
            }

        } catch (Exception e) {
            // TODO: handle exception
        }

    }

}



   public boolean createBond(BluetoothDevice btDevice)  
            throws Exception  
            { 
                Class class1 = Class.forName("android.bluetooth.BluetoothDevice");
                Method createBondMethod = class1.getMethod("createBond");  
                Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);  
                return returnValue.booleanValue();  
            }  


    private void getPairedDevices() {
        Set<BluetoothDevice> pairedDevice = BA.getBondedDevices();            
        if(pairedDevice.size()>0)
        {
            for(BluetoothDevice device : pairedDevice)
            {
                arrayListpaired.add(device.getName()+"\n"+device.getAddress());
                arraylistBloothdevice.add(device);
            }
        }
     adapter.notifyDataSetChanged();
    }

}

1 个答案:

答案 0 :(得分:0)

点击配对蓝牙设备是非正统的; Android会为您处理它。配对设备就像发现它一样简单!根据Android的官方API Guides

  

配对意味着两个设备知道彼此的存在,拥有可用于身份验证的共享链接密钥,并且能够建立彼此的加密连接。

除非您有一些特殊的(我的意思是 reeeaaaly 特殊)这样做的原因,否则无需手动配对设备,因为它是通过可发现性自动完成的。