从ListView访问蓝牙设备

时间:2016-01-11 16:05:04

标签: android bluetooth

这是我的代码。

//Get Bonded Devices
pbluetoothdevice = mbluetoothadapter.getBondedDevices();
    if(pbluetoothdevice.size()>0){
        for(BluetoothDevice bt : pbluetoothdevice){
            list.add(bt.getName()+"\n"+bt.getAddress());
        }
    }
    else
        msg("NO Paired Device Found!");
    arrayAdapter = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1,list);
    listView.setAdapter(arrayAdapter);

    //Connect to the device.
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ConnectThread connectThread = new ConnectThread(/*BluetoothDevice to be passed*/);
            connectThread.start();
        }
    });

我希望连接到OnItemClickListner()中的蓝牙设备,我已经创建了一个Thread,我将通过BluetoothDevice。我该怎么办?

1 个答案:

答案 0 :(得分:1)

获取设备地址并连接到它:

   // Get the device MAC address
    String address = <address of bluetooth device>//it can be get from your device object
    // Get the BluetoothDevice object
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    // Attempt to connect to the device
    mChatService.connect(device, secure);    //secure: Security type - Secure (true) , Insecure (false)

您可以参考android sample