安卓Wiimote套接字连接失败。

3
我想连接我的Wiimote到运行Android 4.1.2的OUYA。我已经成功地使用其他应用程序(如Bluez-IME)连接了Wiimotes,所以我知道这是可能的,但我想自己完成它。
这是我目前的进展:
private void bloothoothConnect(final BluetoothAdapter mBluetoothAdapter){

    // Register the BroadcastReceiver
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);               
                if(isWiiMote(device)){
                    mBluetoothAdapter.cancelDiscovery();
                    connectWiiMote(device);                     
                }
            }
        }
    }, filter);

    mBluetoothAdapter.startDiscovery();
}

private boolean isWiiMote(BluetoothDevice device){
    String name = device.getName();                 
    return name.equalsIgnoreCase("Nintendo RVL-CNT-01");        
}

private void connectWiiMote(final BluetoothDevice device){
    final int tickInterval = 30;
    WiimoteRunnable nWiiMote = new WiimoteRunnable(device, tickInterval){

        @Override
        protected void onButtonDown() { // TODO                         
        }};

    Thread nThread = new Thread(nWiiMote);
    nThread.start();    
}

private abstract class WiimoteRunnable implements Runnable{

    private BluetoothSocket socket;
    private long tickInterval;
    private byte[] buffer = new byte[1024];

    WiimoteRunnable(BluetoothDevice device, long tickInterval) {
        socket = createL2CAPBluetoothSocket(device);
        this.tickInterval = tickInterval;           
    }

    @SuppressLint("NewApi")
    @Override
    public void run() {

        try {
            if(!socket.isConnected()){
                // blocks here!
                socket.connect();
            }
            InputStream iStream = socket.getInputStream();

            while(socket.isConnected()){

                iStream.read(buffer);

                // do stuff with byte buffer here

                Thread.sleep(tickInterval);                                 
            }

        } catch (IOException e2) {
            closeSocket();
        } catch (InterruptedException e) {
            closeSocket();
            return;
        }

    }

    private void closeSocket(){         
        try {
            socket.close();
        } catch (IOException e) {
            return;
        }
    }

    // to be implemented later
    protected abstract void onButtonDown();

}


// see https://dev59.com/BW7Xa4cB1Zd3GeqPnTYF

private static final int TYPE_L2CAP = 3;

/**
 * Create a BluetoothSocket using L2CAP protocol
 * Useful for HID Bluetooth devices
 * @param BluetoothDevice
 * @return BluetoothSocket
 */
@SuppressLint("NewApi")
private static BluetoothSocket createL2CAPBluetoothSocket(BluetoothDevice device){
  int type        = TYPE_L2CAP; // L2CAP protocol
  int fd          = -1;         // Create a new socket
  boolean auth    = false;      // No authentication
  boolean encrypt = false;      // Not encrypted
  int port        = 0;          // port to use (useless if UUID is given)

  ParcelUuid[] uuid = device.getUuids(); // Bluetooth UUID service
    if(uuid==null){
        if(!device.fetchUuidsWithSdp()){
            return null;
        } else {
            uuid = device.getUuids();
        }
    }


  try {
    Constructor<BluetoothSocket> constructor = BluetoothSocket.class.getDeclaredConstructor(
      int.class, int.class, boolean.class, boolean.class,
      BluetoothDevice.class, int.class, ParcelUuid.class);
    constructor.setAccessible(true);
    BluetoothSocket clientSocket = (BluetoothSocket) constructor.newInstance(
      type, fd, auth, encrypt, device, port, uuid[0]);
    return clientSocket;
  } catch (Exception e) {
    e.printStackTrace();
    return null;
  }
}

问题在于当我执行socket.connect()时,它会阻塞并永远不会返回。Socket对象已经存在,在调试器中我可以看到所有的数据。
我正在按照使用L2CAP连接蓝牙HID设备(鼠标)获取Socket。
1个回答

2

与其使用UUID作为L2CAP套接字,您需要使用通道号(或代码中的端口号)。要连接wiimote,您需要分别打开控制(从应用程序发送到wiimote的命令)和数据(从wiimote发送到应用程序的输入)通道的套接字。

private static final int CONTROL_CHANNEL = 0x11;
private static final int DATA_CHANNEL = 0x13;

private BluetoothSocket controlSocket;
private BluetoothSocket dataSocket;

private OutputStream os;
private InputStream is;

private static BluetoothSocket createL2CAPBluetoothSocket(BluetoothDevice device, final int channel) {
    int type = TYPE_L2CAP; // L2CAP protocol
    int fd = -1; // Create a new socket
    boolean auth = false; // No authentication
    boolean encrypt = false; // Not encrypted

    try {
        Constructor<BluetoothSocket> constructor = BluetoothSocket.class.getDeclaredConstructor(int.class,
                int.class, boolean.class, boolean.class, BluetoothDevice.class, int.class, ParcelUuid.class);
        constructor.setAccessible(true);
        BluetoothSocket clientSocket = (BluetoothSocket) constructor.newInstance(type, fd, auth, encrypt, device,
                channel, null);
        return clientSocket;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

void connect(BluetoothDevice device) {
    try {
        controlSocket = createL2CAPBluetoothSocket(device, CONTROL_CHANNEL);        
        controlSocket.connect();
        os = controlSocket.getOutputStream();

        dataSocket = createL2CAPBluetoothSocket(device, DATA_CHANNEL);
        dataSocket.connect();
        is = dataSocket.getInputStream();      

        // open transmit & receive threads for input and output streams appropriately

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

谢谢!按钮按下方面完美运作!我会尝试获取加速计数据,就像这样:http://wiibrew.org/wiki/Wiimote - James Coote
有人知道在安卓系统中是否支持L2CAP回声请求(例如L2ping的工作原理)以及如何支持吗?这个示例代码需要一个通道,但我认为回声请求应该没有通道。感谢帮助! - Benjamin Chodroff

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接