Android 如何连接到配对的蓝牙设备?

Android 如何连接到配对的蓝牙设备?,android,bluetooth,Android,Bluetooth,我的代码可以扫描和配对蓝牙扬声器。但是,为了将音频流传输到配对设备,我必须通过设备设置配对,否则将无法工作。如何通过我的应用程序做到这一点 public class DeviceListActivity extends Activity { private ListView mListView; private DeviceListAdapter mAdapter; private ArrayList<BluetoothDevice> mDeviceList;

我的代码可以扫描和配对蓝牙扬声器。但是,为了将音频流传输到配对设备,我必须通过设备设置配对,否则将无法工作。如何通过我的应用程序做到这一点

public class DeviceListActivity extends Activity {
    private ListView mListView;
    private DeviceListAdapter mAdapter;
    private ArrayList<BluetoothDevice> mDeviceList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_paired_devices);

        mDeviceList     = getIntent().getExtras().getParcelableArrayList("device.list");

        mListView       = (ListView) findViewById(R.id.lv_paired);

        mAdapter        = new DeviceListAdapter(this);

        mAdapter.setData(mDeviceList);
        mAdapter.setListener(new DeviceListAdapter.OnPairButtonClickListener() {            
            @Override
            public void onPairButtonClick(int position) {
                BluetoothDevice device = mDeviceList.get(position);

                if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                    unpairDevice(device);
                } else {
                    showToast("Connecting..");
                    pairDevice(device);
                }
            }
        });

        mListView.setAdapter(mAdapter);

        registerReceiver(mPairReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED)); 
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(mPairReceiver);

        super.onDestroy();
    }


    private void showToast(String message) {
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
    }

    private void pairDevice(BluetoothDevice device) {
        try {
            Method method = device.getClass().getMethod("createBond", (Class[]) null);
            method.invoke(device, (Object[]) null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void unpairDevice(BluetoothDevice device) {
        try {
            Method method = device.getClass().getMethod("removeBond", (Class[]) null);
            method.invoke(device, (Object[]) null);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    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("Connected! now you can stream");
                 } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED){
                     showToast("Disconnected");
                 }

                 mAdapter.notifyDataSetChanged();
            }
        }
    };
}
公共类DeviceListActivity扩展活动{
私有列表视图;
私有设备适配器;
私人ArrayList mDeviceList;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u配对设备);
mDeviceList=getIntent().getExtras().getParcelableArrayList(“device.list”);
mListView=(ListView)findViewById(R.id.lv_配对);
mAdapter=新设备适配器(本);
mAdapter.setData(mDeviceList);
mAdapter.setListener(新的DeviceListAdapter.OnPairButtonClickListener(){
@凌驾
公共无效onPairButtonClick(内部位置){
BluetoothDevice=mDeviceList.get(位置);
if(device.getBondState()==BluetoothDevice.BOND\u BOND){
非配对装置(装置);
}否则{
showToast(“连接…”);
配对设备(装置);
}
}
});
mListView.setAdapter(mAdapter);
registerReceiver(mPairReceiver,新的意向过滤器(BluetoothDevice.ACTION\u BOND\u STATE\u CHANGED));
}
@凌驾
公共空间{
未注册接收人(MPAIReceiver);
super.ondestory();
}
私有void showtoos(字符串消息){
Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT.show();
}
专用无效配对设备(蓝牙设备){
试一试{
方法Method=device.getClass().getMethod(“createBond”,(Class[])null;
调用(设备,(对象[])null);
}捕获(例外e){
e、 printStackTrace();
}
}
私有无效未配对设备(蓝牙设备){
试一试{
方法Method=device.getClass().getMethod(“removeBond”,(Class[])null;
调用(设备,(对象[])null);
}捕获(例外e){
e、 printStackTrace();
}
}
private final BroadcastReceiver mPairReceiver=新的BroadcastReceiver(){
公共void onReceive(上下文、意图){
String action=intent.getAction();
如果(BluetoothDevice.ACTION\u BOND\u STATE\u CHANGED.equals(ACTION)){
final int state=intent.getIntExtra(BluetoothDevice.EXTRA\u BOND\u state,BluetoothDevice.ERROR);
final int prevState=intent.getIntExtra(BluetoothDevice.EXTRA\u PREVIOUS\u BOND\u STATE,BluetoothDevice.ERROR);
if(state==BluetoothDevice.BOND\u BONDING&&prevState==BluetoothDevice.BOND\u BONDING){
showToast(“已连接!现在您可以流媒体了”);
}else if(state==BluetoothDevice.BOND\u NONE&&prevState==BluetoothDevice.BOND\u BOND){
showToast(“断开”);
}
mAdapter.notifyDataSetChanged();
}
}
};
}