【问题标题】:Problems with Android Bluetooth Server and ClientAndroid蓝牙服务器和客户端的问题
【发布时间】:2015-03-28 11:34:57
【问题描述】:

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

// 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传递什么变量?

感谢您的帮助:)

【问题讨论】:

    标签: android sockets bluetooth client server


    【解决方案1】:

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

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

    您的代码是从哪里获得的?完整代码可在 BluetoothChat 示例应用程序的 DeviceListActivity.java 中找到,该示例应用程序随 SDK 一起提供了多个版本。

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

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

    【讨论】:

    • 谢谢。我从 android 文档here 中获得了该代码。什么是蓝牙设备名称?
    • 这是 - 像往常一样 - DeviceListActivity 中使用的布局文件的名称。
    • 是否有任何预制布局文件,如android在设备设置中使用的等等?
    • 不好意思问了,不知道让你生气了!
    猜你喜欢
    • 2015-06-14
    • 2016-09-18
    • 2016-06-01
    • 2018-01-01
    • 2013-06-21
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多