Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过蓝牙编程连接两台Android设备_Android - Fatal编程技术网

通过蓝牙编程连接两台Android设备

通过蓝牙编程连接两台Android设备,android,Android,我正在做一个蓝牙项目,我想用blutooth编程连接两个设备 我遵循tf developer.android.com的指导原则和代码。有人能帮我解决这个问题吗?以下是我尝试过的代码 有人能告诉我构造函数从哪里接收设备对象吗 private class ConnectThread extends Thread { private final BluetoothSocket mmSocket; private final BluetoothDevice mmDevic

我正在做一个蓝牙项目,我想用blutooth编程连接两个设备

我遵循tf developer.android.com的指导原则和代码。有人能帮我解决这个问题吗?以下是我尝试过的代码

有人能告诉我构造函数从哪里接收设备对象吗

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

<!-- can anyone tell me from where device object comes here   --!>
        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) { }
        }
    }


Thanks inadvance
私有类ConnectThread扩展线程{
私人最终蓝牙插座mmSocket;
专用最终蓝牙设备;

下面是我完成任务的一些代码

    /**
     * Searches the list of paired devices for the device. Returns
     * if found, throws Exception otherwise.
     * 
     * @param sTargetDeviceName
     * @return BluetoothDevice 
     */
    private BluetoothDevice findDevice(String sTargetDeviceName) 
    throws Exception {

            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

            Set<BluetoothDevice> devices = adapter.getBondedDevices();

            for (BluetoothDevice device : devices) {

                    String sDeviceName = device.getName().trim();

                    if (sDeviceName.startsWith(sTargetDeviceName)) {
                            return device;
                    }
            }

            throw new Exception("Device " + sTargetDeviceName + " not found");
    }
/**
*在配对设备列表中搜索设备。返回
*如果找到,则抛出异常。
* 
*@param sTargetDeviceName
*@返回蓝牙设备
*/
专用蓝牙设备findDevice(字符串sTargetDeviceName)
抛出异常{
BluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
Set devices=adapter.getBondedDevices();
用于(蓝牙设备:设备){
字符串sDeviceName=device.getName().trim();
if(sDeviceName.startsWith(sTargetDeviceName)){
返回装置;
}
}
抛出新异常(“设备”+sTargetDeviceName+“未找到”);
}
要使用上述代码,设备必须已配对

请参阅文档:


特别是“查找设备”部分。

您已经完成研究了吗?请查看[Blutooth)你的家庭作业在提出问题之前,你有没有彻底地寻找答案?分享你的研究成果对每个人都有帮助。告诉我们你发现了什么,以及为什么它不能满足你的需要。这表明你花了时间试着帮助自己,这让我们避免重复显而易见的答案,最重要的是它帮助你获得答案一个更具体和相关的答案!我不知道为什么人们会投票?