gpt4 book ai didi

java - 在android中通过蓝牙发送文件时出错?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:55:00 26 4
gpt4 key购买 nike

我跟进了this问题,但所有提到的解决方案都不适合我。

我正在制作一个类似于彩虹应用程序的应用程序。此应用程序将安装在必须将所有联系人发送到其他设备的设备中。该应用程序只能安装在一个设备中。我可以通过这段代码连接到远程设备

//蓝牙连接器(Complete Code)

Class<?> clazz = tmp.getRemoteDevice().getClass();
Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE};
Method m = clazz.getMethod("createRfcommSocket", paramTypes);
Object[] params = new Object[] {Integer.valueOf(1)};
fallbackSocket = (BluetoothSocket) m.invoke(tmp.getRemoteDevice(), params);

发出配对请求并完成连接后,我尝试通过这段代码通过输出流向其他设备发送数据。

//输出流代码(Complete code)

public void write(byte[] buffer) {
try {
Log.i(TAG, "write");
mmOutStream.write(buffer);
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}

但是我无法尽快发送数据 mmOutStream.write(buffer);被称为它给出以下错误。

//错误日志(Complete Log)

09-21 16:21:52.829    6262-6262/com.example.aadi.myapplication D/BT_app﹕ connection_done
09-21 16:21:52.829 6262-6871/com.example.aadi.myapplication I/BT_app﹕ BEGIN mConnectedThread
09-21 16:21:52.829 6262-6871/com.example.aadi.myapplication I/BT_app﹕ write
09-21 16:21:52.829 6262-6262/com.example.aadi.myapplication D/BT_app﹕ msg write :[B@4265cd70
09-21 16:22:50.149 6262-6823/com.example.aadi.myapplication W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
09-21 16:22:50.159 6262-6823/com.example.aadi.myapplication D/BluetoothSocket﹕ connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[81]}
09-21 16:22:50.679 6262-6823/com.example.aadi.myapplication W/BT_app﹕ Fallback failed. Cancelling.
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:482)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:324)
at com.example.aadi.myapplication.BluetoothConnector$FallbackBluetoothSocket.connect(BluetoothConnector.java:202)
at com.example.aadi.myapplication.BluetoothConnector.connect(BluetoothConnector.java:64)
at com.example.aadi.myapplication.BluetoothService$ConnectThread.run(BluetoothService.java:218)
09-21 16:22:50.679 6262-6823/com.example.aadi.myapplication I/BT_app﹕ Attempting to connect to Protocol: 0000112f-0000-1000-8000-00805f9b34fb

请告诉我我在上面的代码中做错了什么。是否可以在不实现服务器端代码的情况下通过蓝牙传输文件?

最佳答案

从您的日志来看,ConnectThread 似乎在您的已连接线程运行时再次运行。引用日志:

    09-21 16:21:47.329    6262-6822/com.example.gauravdubey.myapplication I/BT_app﹕ BEGIN mConnectedThread
09-21 16:21:47.329 6262-6822/com.example.gauravdubey.myapplication I/BT_app﹕ write
09-21 16:21:47.329 6262-6262/com.example.gauravdubey.myapplication D/BT_app﹕ msg write :[B@425c9958
09-21 16:21:47.329 6262-6763/com.example.gauravdubey.myapplication D/BT_app﹕ setState() 2 -> 3

after a while
09-21 16:21:47.359 6262-6262/com.example.gauravdubey.myapplication D/BT_app﹕ ConnectThread
09-21 16:21:47.359 6262-6262/com.example.gauravdubey.myapplication D/BT_app﹕ setState() 0 -> 2
09-21 16:21:47.359 6262-6262/com.example.gauravdubey.myapplication D/BT_app﹕ state is :null
09-21 16:21:47.359 6262-6823/com.example.gauravdubey.myapplication D/

BT_app﹕ ConnectThread---->run()

您的单线程 ConnectThread 似乎被多次调用。尝试检查您的代码,确保线程只运行一次。希望这会奏效。

关于java - 在android中通过蓝牙发送文件时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25958486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com