与蓝牙设备android连接

时间:2013-03-11 12:09:22

标签: android bluetooth

我正在尝试连接配对的蓝牙设备。我收到了getRemoteDevice(adress)的设备。我从配对设备中获取地址,该设备显示在ListView

我只想连接该设备,但我无法这样做。可能是我有错误的UUID还是别的什么?

lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View lv,int position, long id) {
            // When clicked, show a toast with the TextView text  

        Device neu = list.get(position);
        BluetoothDevice btDevice;
        BluetoothSocket tmp = null;
        final BluetoothSocket mmSocket;
        String adress = neu.getAdress();

        UUID uuid;

        uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

        btDevice = mBluetoothAdapter.getRemoteDevice(adress);
        try {
            tmp = btDevice.createRfcommSocketToServiceRecord(uuid);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            tv.setText("fail bei creating btDevice !!!");
            //e.printStackTrace();
        }
        mmSocket = tmp;

        mBluetoothAdapter.cancelDiscovery();

        // Make a connection to the BluetoothSocket
        try {
            // This is a blocking call and will only return on a

           if(mmSocket.isConnected()){
                tv.setText("Bereits Verbunden!!!");
           }else{
               mmSocket.connect();   
           }

        } catch (IOException e1) {
            // Close the socket
            tv.setText("fail bei connecting btDevice !!!  "+ adress);
            try {
                mmSocket.close();
            } catch (IOException e2) {

                tv.setText("fail bei disconnecting btDevice !!!");
            }

            }

          }
        });

}

0 个答案:

没有答案
相关问题