简体   繁体   English

Android设备蓝牙配对2

[英]Android Device Bluetooth pairing 2

I want to pair to them on click. 我想与他们配对。 But My Apps is Crashed. 但是我的应用程序崩溃了。

I have given permission Bluetooth.Please help me! 我已授予蓝牙许可。请帮帮我!

cause by error: 错误原因:

06-12 19:34:40.997: E/AndroidRuntime(4172): Caused by: java.lang.NullPointerException 06-12 19:34:40.997: E/AndroidRuntime(4172): at com.example.bluetooth_tutorialplspoint.MainActivity.getPairedDevices(MainActivity.java:158) 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();
    }

} }

Pairing Bluetooth devices on click is unorthodox; 单击时配对蓝牙设备是不合常规的; Android takes care of it for you. Android会帮您处理它。 Pairing a device is as simple as discovering it! 配对设备就像发现它一样简单! According to Android's official API Guides : 根据Android的官方API指南

To be paired means that two devices are aware of each other's existence, have a shared link-key that can be used for authentication, and are capable of establishing an encrypted connection with each other. 配对意味着两个设备知道彼此的存在,具有可以用于身份验证的共享链接密钥,并且能够彼此建立加密连接。

Unless you have some special (I mean reeeaaaly special) reason for doing so, there is no need to manually pair devices because it's done automatically through discoverability. 除非你有一些特殊的(我的意思是reeeaaaly特)这样做的理由,也没有必要手动对设备,因为它是通过曝光率自动完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM