小编And*_*ing的帖子

Android BluetoothSocket连接返回零

我们的设备通过蓝牙发送数据,在Android应用程序中我们需要读取这些数据.

我能够建立蓝牙连接,接下来我正在调用一个Thread来使用BluetoothDevice建立BluetoothSocket连接.在读取字节时,它返回0(零)此外,while循环仅运行一次.

我在下面的代码中使用的UUID也来自某些蓝牙代码段.我是否需要获取设备的正确UUID.

请有人帮忙吗?.如果你给我一个有用的答案,我将非常感谢.

   //Calling ConnectThread after Bluetooth is paired
    public class ConnectThread extends Thread {
            private final BluetoothSocket mmSocket;
            private final BluetoothDevice mmDevice;
            private final UUID MY_UUID = UUID
                    .fromString("00001101-0000-1000-8000-00805f9b34fb");

            public ConnectThread(BluetoothDevice device) {
                BluetoothSocket tmp = null;
                mmDevice = device;
                try {
                    String name = device.getName();
                    Log.e("Device Name ", name);
                    tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
                } catch (IOException e) {
                }
                mmSocket = tmp;
            }

            public void run() {
                // mAdapter.cancelDiscovery();
                try {
                    mmSocket.connect();

                    ConnectedThread mConnectedThread = new ConnectedThread(mmSocket);
                    mConnectedThread.start(); …
Run Code Online (Sandbox Code Playgroud)

android android-bluetooth bluetooth-socket

5
推荐指数
1
解决办法
811
查看次数