3
public class ConnectThread extends Thread 
{
    private final BluetoothSocket mmSocket;     
    private final BluetoothDevice mmDevice; 
    private final OutputStream mmOutStream; 
    private UUID uuid =  UUID.randomUUID();
    private byte[] temp;
    public ConnectThread(BluetoothDevice device,byte[] temp) 
    {
        BluetoothSocket socket = null; 
        OutputStream tmpOut = null;
        mmDevice = device; 
        this.temp = temp;       
        try 
        {
            Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
            socket = (BluetoothSocket) m.invoke(device, 1);
            tmpOut = socket.getOutputStream();
        }

        catch (Exception e) { } 
        mmSocket = socket;    
        mmOutStream = tmpOut;
    }

    public void run() 
    {         enter code here

        try 
        {             
            mmSocket.connect();
            write(temp);
        }
        catch (IOException connectException) 
        {
            try 
            {                 
                mmSocket.close(); 

            }
            catch (IOException closeException) 
            { }
            return;
        }

    }

    public void write(byte[] bytes) 
    {        
        try 
        {            
            mmOutStream.write(bytes);
            mmOutStream.flush();
            mmOutStream.close();
        } 
        catch (IOException e) { }    
    }

    public void cancel() 
    {
        try 
        {             
            mmSocket.close();      
        } 
        catch (IOException e) { } 
    }
}

我正在尝试将 txt 文件传输到另一台设备。我将蓝牙设备和消息(以字节为单位)传递给此类并启动线程表单主要活动。扫描后,设备显示和配对也正常工作。我在代码中没有任何异常。但我没有收到文件。

为什么我在其他手机上收不到文件?

有人可以帮我吗。

谢谢,法拉兹

4

0 回答 0