简体   繁体   English

是否有系统动作来发现和配对Android中的蓝牙设备?

[英]Is there a system action to discover and pair Bluetooth devices in Android?

The discovery and pairing process described in the Android Bluetooth Documentation is quite complex. Android蓝牙文档中描述的发现和配对过程非常复杂。 Even a separate permission is needed for that: BLUETOOTH_ADMIN . 甚至需要一个单独的权限: BLUETOOTH_ADMIN

I wonder if there is a system action, that I just would call, that will handle the UI, return, and the I will just choose an already paired device. 我想知道是否有一个我将要调用的系统操作,该操作将处理UI,返回,然后我将选择一个已经配对的设备。 Or do I have to implement all that UI myself? 还是我必须自己实现所有UI?

if you want to get the list of paired bluetooth devices you can use this code 如果要获取已配对的蓝牙设备的列表,可以使用此代码

public class DeviceList extends ActionBarActivity
{
//widgets
   Button btnPaired;
   ListView devicelist;
//Bluetooth
    private BluetoothAdapter myBluetooth = null;
    private Set<BluetoothDevice> pairedDevices;
    public static String EXTRA_ADDRESS = "device_address";

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_device_list);

    //Calling widgets
    btnPaired = (Button)findViewById(R.id.button);
    devicelist = (ListView)findViewById(R.id.listView);

    //if the device has bluetooth
    myBluetooth = BluetoothAdapter.getDefaultAdapter();

    if(myBluetooth == null)
    {
        //Show a mensag. that the device has no bluetooth adapter
        Toast.makeText(getApplicationContext(), "Bluetooth Device Not Available", Toast.LENGTH_LONG).show();

        //finish apk
        finish();
    }
    else if(!myBluetooth.isEnabled())
    {
            //Ask to the user turn the bluetooth on
            Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(turnBTon,1);
    }

    btnPaired.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            pairedDevicesList();
        }
    });

}

private void pairedDevicesList()
{
    pairedDevices = myBluetooth.getBondedDevices();
    ArrayList list = new ArrayList();

    if (pairedDevices.size()>0)
    {
        for(BluetoothDevice bt : pairedDevices)
        {
            list.add(bt.getName() + "\n" + bt.getAddress()); //Get the device's name and the address
        }
    }
    else
    {
        Toast.makeText(getApplicationContext(), "No Paired Bluetooth Devices Found.", Toast.LENGTH_LONG).show();
    }

    final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
    devicelist.setAdapter(adapter);
    devicelist.setOnItemClickListener(myListClickListener); //Method called when the device from the list is clicked

}

private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener()
{
    public void onItemClick (AdapterView<?> av, View v, int arg2, long arg3)
    {
        // Get the device MAC address, the last 17 chars in the View
        String info = ((TextView) v).getText().toString();
        String address = info.substring(info.length() - 17);

        // Make an intent to start next activity.
        Intent i = new Intent(DeviceList.this, NextActivity.class);

        //Change the activity.
        i.putExtra(EXTRA_ADDRESS, address); //this will be received at NextActivity
        startActivity(i);
    }
};
}

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

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