1

我有以下设置:

Android 设备使用“客户端”套接字连接到远程嵌入式设备,Android 应用程序使用以下代码片段连接到嵌入式设备。

在嵌入式设备上使用MindTree BT堆栈,其中服务器串行套接字是根据Android应用程序熟悉的设备中的一些属性准备的,嵌入式设备上定义的连接是不安全的!

两种应用程序的组合适用于:

  • 2 款 LG 手机不同型号(版本代码 < 10使用“普通方法”
  • 2 HTC 的不同型号(版本代码<10使用“解决方法”
  • Pantech 平板电脑(版本代码 < 13使用“解决方法”

今天,我在三星 S3、摩托罗拉 MB886 和 Nexus 7 上尝试了该应用程序...调用socket.connect()时都导致“权限被拒绝” ...(我在清单中有适当的权限,否则它将无法在其他设备上运行。)

我测试过的所有新设备都是版本代码> 4.0,所以我想知道:

有人知道 API 的任何变化吗?也许 Android 4.0+ 会强制安全?

似乎错误发生在绑定状态,因为我可以在嵌入式程序日志中看到......

有什么见解吗?

编码:

public final synchronized int connectToDevice(int connectingMethod)
        throws BluetoohConnectionException {
    if (socket != null)
        throw new BadImplementationException("Error socket is not null!!");
    connecting = true;
    logInfo("+---+ Connecting to device...");

    try {
        lastException = null;
        lastPacket = null;
        if (connectingMethod == BluetoothModule.BT_StandardConnection
                || connectingMethod == BluetoothModule.BT_ConnectionTBD)
            try {

                socket = fetchBT_Socket_Normal();
                connectToSocket(socket);
                listenForIncomingSPP_Packets();
                onConnetionEstablished();
                return BluetoothModule.BT_StandardConnection;
            } catch (BluetoohConnectionException e) {
                socket = null;
                if (connectingMethod == BluetoothModule.BT_StandardConnection) {
                    throw e;
                }
                logWarning("Error creating socket!", e);
            }
        if (connectingMethod == BluetoothModule.BT_ReflectiveConnection
                || connectingMethod == BluetoothModule.BT_ConnectionTBD)
            try {
                socket = fetchBT_Socket_Reflection(1);
                connectToSocket(socket);
                listenForIncomingSPP_Packets();
                onConnetionEstablished();
                return BluetoothModule.BT_ReflectiveConnection;
            } catch (BluetoohConnectionException e) {
                socket = null;
                if (connectingMethod == BluetoothModule.BT_ReflectiveConnection) {
                    throw e;
                }
                logWarning("Error creating socket!", e);
            }
        throw new BluetoohConnectionException("Error creating RFcomm socket for BT Device:" + this
                + "\n BAD connectingMethod==" + connectingMethod);
    } finally {
        connecting = false;
    }
}

protected void onConnetionEstablished() {
    logInfo("+---+ Connection established");
}

private synchronized void listenForIncomingSPP_Packets() {
    if (socketListeningThread != null)
        throw new BadImplementationException("Already lisening on Socket for BT Device" + this);
    logInfo("+---+ Listening for incoming packets");
    socketListeningThread = new Thread(socketListener, "Packet Listener - " + bluetoothDevice.getName());
    socketListeningThread.start();
}

private BluetoothSocket fetchBT_Socket_Normal()
        throws BluetoohConnectionException {
    try {
        logInfo("+---+ Fetching BT RFcomm Socket standard for UUID: " + uuid + "...");
        return bluetoothDevice.createRfcommSocketToServiceRecord(UUID.fromString(uuid));
    } catch (Exception e) {
        throw new BluetoohConnectionException("Error Fetching BT RFcomm Socket!", e);
    }
}

private BluetoothSocket fetchBT_Socket_Reflection(int connectionIndex)
        throws BluetoohConnectionException {
    Method m;
    try {
        logInfo("+---+ Fetching BT RFcomm Socket workaround index " + connectionIndex + "...");
        m = bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
        return (BluetoothSocket) m.invoke(bluetoothDevice, connectionIndex);
    } catch (Exception e) {
        throw new BluetoohConnectionException("Error Fetching BT RFcomm Socket!", e);
    }
}

private void connectToSocket(BluetoothSocket socket)
        throws BluetoohConnectionException {
    try {
        logInfo("+---+ Connecting to socket...");
        socket.connect();
        logInfo("+---+ Connected to socket");
    } catch (IOException e) {
        try {
            socket.close();
        } catch (IOException e1) {
            logError("Error while closing socket", e1);
        } finally {
            socket = null;
        }
        throw new BluetoohConnectionException("Error connecting to socket with Device" + this, e);
    }
}
4

1 回答 1

0

经过很长一段时间的调查,我发现了错误的一个原因......在某些 Android 设备上,自动蓝牙对等连接未被启用/允许。

所以,显然除了两种连接方法外,还有两种蓝牙适配器启用方法,一种是抛出一个意图要求系统打开适配器,另一种是调用 BluetoothAdapter.enable() 方法,以静默方式启用蓝牙。

第一种方法,弹出确认对话框,需要用户交互,另一种则不需要,在没有显示蓝牙启用确认对话框的同时,也没有显示对等确认,导致连接错误。

使用第一个适配器启用方法可以解决大多数设备上的问题,例如 Nexus 7、Samsung S3 和其他一些设备,但在某些设备上仍然存在问题,我不太确定为什么,但这是好多了,因为许多设备现在都在使用新的实现。

于 2012-12-06T21:37:51.063 回答