Android不安全的蓝牙连接

标签 android bluetooth

我想将 android 移动设备连接到一个电子设备,但我想不安全地连接它。但它给我的错误是“java.io.IOException:读取失败,套接字可能关闭或超时,读取 ret:-1 "

这是我的代码

包 net.londatiga.android.bluetooth;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.UUID;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

public class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;

    private final UUID WELL_KNOWN_UUID = UUID
            .fromString("00001101-0000-1000-8000-00805F9B34FB");

    public ConnectThread(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket,because
        // mmSocket is final
        BluetoothSocket tmp = null;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // tmp = device.createRfcommSocketToServiceRecord(WELL_KNOWN_UUID);
            tmp = device
                    .createRfcommSocketToServiceRecord(WELL_KNOWN_UUID);
            Object[] params = new Object[] {Integer.valueOf(1)};
            // This is the trick
            Method m = device.getClass().getMethod("createInsecureRfcommSocket",
                    new Class[] { int.class });
            tmp = (BluetoothSocket) m.invoke(device, params);
        } catch (Exception e) {
            e.printStackTrace();
        }

        mmSocket = tmp;
    }

    @SuppressLint("NewApi")
    public void run() {
        // DebugLog.i(TAG, "Trying to connect...");
        // Cancel discovery because it will slow down the connection
        MainActivity.mBluetoothAdapter.cancelDiscovery();

        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();

            boolean val=mmSocket.isConnected();
            Log.v("val", ""+val);
            // DebugLog.i(TAG, "Connection stablished");
        } catch (IOException connectException) {
            // Unable to connect; close the socket and get out
            // DebugLog.e(TAG, "Fail to connect!", connectException);
            try {
                mmSocket.close();
            } catch (IOException closeException) {
                // DebugLog.e(TAG, "Fail to close connection", closeException);
            }
            return;
        }
    }

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
        }
    }
}

最佳答案

你必须实现另一个线程,如下所示,它总是接收你的调用并执行操作。启动应用程序时启动此线程。

class AcceptThread extends
 Thread {
    private final BluetoothServerSocket serverSocket;
    private boolean flag = true;

    public AcceptThread() {
        BluetoothServerSocket tmp = null;

        try {
            tmp = bluetoothAdapter
                    .listenUsingInsecureRfcommWithServiceRecord(
                            NAME_UUID, uuid);

        } catch (IOException e) {
        }
        serverSocket = tmp;
    }

    public void run() {
        BluetoothSocket socket = null;
        while (true) {
            try {
                socket = serverSocket.accept();
                if (socket.isConnected()) {
                    Log.d(TAG, "run:  connection successfull");
                    Log.d(TAG, "run: " +     socket.getRemoteDevice().getName() + "  " +
                            socket.getRemoteDevice().getAddress());
                }

            } catch (IOException e) {

                try {
                    socket.close();
                } catch (Exception eee) {
                    Log.d(TAG, "run: " + eee.getMessage());
                }
                break;
            }
        }
    }

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

关于Android不安全的蓝牙连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28808470/

相关文章:

java - 以一定角度在 Canvas 上绘制文本

android - Android 中的顺时针和逆时针圆周运动

java - 检查是否所有复选框均已选中。安卓

一旦连接或断开蓝牙设备,Android 移动应用程序总是会重新启动

android - 如何增加 Android 中的蓝牙配对窗口超时

bluetooth - 是否可以获得附近蓝牙设备的唯一ID?

android - 优于 FileObserver

android - react- native : 'adb' is not recognized as an internal or external command, 可运行程序或批处理文件

error-handling - 蓝牙 HC-05 发送错误 1F 仅适用于 INQ 命令

objective-c - 核心蓝牙的限制?