Android 蓝牙连接
BluetoothAdapter蓝牙适配器
BluetoothDevice 蓝牙设备
连接并实现通信过程
1. 打开蓝牙
//请求用户打开
Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler, RESULT_FIRST_USER);
//使蓝牙可见
Intent in = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);
startActivity(in);
//直接开启
//mBluetoothAdapter.enable();
2. 获取可配对或已经配对了的设备以及相关信息,包括名字,硬件地址。
private void findAvalibleDevice(){ //获取可配对蓝牙设备 Set<BluetoothDevice> device=mBluetoothAdapter.getBondedDevices(); if(mBluetoothAdapter!=null&&mBluetoothAdapter.isDiscovering()){ mList.clear(); mAdapter.notifyDataSetChanged(); } if(device.size()>0){