Android蓝牙服务器和客户端的问题

时间:2015-03-28 11:34:57

标签: android sockets bluetooth client server

我有一些关于为我正在制作的多人游戏创建蓝牙服务器和客户端的问题。 我遇到的第一个问题是搜索设备。 Google提供以下代码:

// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mReceiver = 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);
            // Add the name and address to an array adapter to show in a ListView
            mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        }
    }
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

但是我无法找到定义mArrayAdapter的位置或它应该是什么样子。 我的第二个问题是,当我在我的设备上运行服务器时,我是否也必须连接它,或者只有另一部手机必须连接而我的服务器充当客户端和服务器?

我的第三个问题是如何连接到服务器。同样,谷歌提供此代码:

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) { }
    }
}

如何创建和使用此类,以及我应该在构造函数中为BluetoothDevice device传递哪个变量?

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

您可以从mArrayAdapter.add(device.getName() + "\n" + device.getAddress());看到添加了一个字符串。所以它将被定义为:

ArrayAdapter<String> mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.bluetooth_device_name);

您从哪里获取代码?您可以在BluetoothChat示例应用程序的DeviceListActivity.java中找到完整的代码,该应用程序一次又一次地为许多版本的SDK附带。

对于您的第二个问题:设备上的一个应用程序使设备可见。另一台设备上的其他应用程序会扫描相邻设备并尝试连接。然后两个设备必须达成一致。

我建议您首先从示例中执行BluetoothChat应用程序。它开箱即用。