1

我正在尝试从我的 HTC Wildfire 连接蓝牙设备,几个月前它工作正常并且能够与蓝牙设备建立连接,但是在 HTC 上更新软件后,事情并不顺利

当手机没有更新时,代码就像一个魅力一样工作

bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(UUID_STRING);

更新手机后,我探索并发现了以下代码

 Method m = bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
 bluetoothSocket = (BluetoothSocket) m.invoke(bluetoothDevice, Integer.valueOf(1));
 bluetoothSocket.connect();

但是我的蓝牙连接被阻塞了 bluetoothSocket.connect()。此外,代码没有达到 bluetoothSocket.getInputStream()and bluetoothSocket.getOutputStream()

有没有人解决这个问题,

我目前的 HTC 野火状态是

  1. 安卓操作系统 2.2.1
  2. 内部版本号 2.25.720.4CL299259 发布密钥
4

1 回答 1

0

由于蓝牙连接过程非常耗时,并且无法预测某个时间限制。

最好把connect放进去

背景线程。

private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {
    // Use a temporary object that is later assigned to mmSocket,
    // because mmSocket is final
    BluetoothSocket tmp = null;
    mmDevice = device;

    // Get a BluetoothSocket to connect with the given BluetoothDevice
    try {
        // MY_UUID is the app's UUID string, also used by the server code
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) { }
    mmSocket = tmp;
   }

   public void run() {
    // Cancel discovery because it will slow down the connection
    mBluetoothAdapter.cancelDiscovery();

    try {
        // Connect the device through the socket. This will block
        // until it succeeds or throws an exception
        mmSocket.connect();
    } catch (IOException connectException) {
        // Unable to connect; close the socket and get out
        try {
            mmSocket.close();
        } catch (IOException closeException) { }
        return;
     }

    // Do work to manage the connection (in a separate thread)
    manageConnectedSocket(mmSocket);
   }

   /** Will cancel an in-progress connection, and close the socket */
   public void cancel() {
    try {
        mmSocket.close();
    } catch (IOException e) { }
   }
   }


   private class ConnectedThread extends Thread {
   private final BluetoothSocket mmSocket;
   private final InputStream mmInStream;
   private final OutputStream mmOutStream;

   public ConnectedThread(BluetoothSocket socket) {
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the input and output streams, using temp objects because
    // member streams are final
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) { }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
   }

   public void run() {
    byte[] buffer = new byte[1024];  // buffer store for the stream
    int bytes; // bytes returned from read()

    // Keep listening to the InputStream until an exception occurs
    while (true) {
        try {
            // Read from the InputStream
            bytes = mmInStream.read(buffer);
            // Send the obtained bytes to the UI Activity
            mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                    .sendToTarget();
        } catch (IOException e) {
            break;
        }
      }
    }

    /* Call this from the main Activity to send data to the remote device */
    public void write(byte[] bytes) {
    try {
        mmOutStream.write(bytes);
       } catch (IOException e) { }
    }

   /* Call this from the main Activity to shutdown the connection */
    public void cancel() {
    try {
        mmSocket.close();
      } catch (IOException e) { }
     }
    }
于 2012-12-30T07:29:20.363 回答