1

私は次の設定をしています:

Android デバイスは「クライアント」ソケットを使用してリモートの組み込みデバイスに接続します。Android アプリケーションは次のコード スニペットを使用して組み込みデバイスに接続します。

組み込みデバイスでは、Android アプリケーションが使い慣れたデバイス内のいくつかのプロパティに従ってサーバー シリアル ソケットが準備される MindTree BT スタックを使用しますが、組み込みデバイスで定義された接続は保護されません!!

両方のアプリケーションの組み合わせは、以下で機能します。

  • 2 つの LG 電話の異なるモデル (バージョン コード < 10は「通常の方法」を使用)
  • 2 HTC の異なるモデル (バージョン コード < 10は「回避方法」を使用)
  • Pantech Tablet (バージョンコード < 13は「回避方法」を使用)

今日、Samsung S3、Motorola MB886、Nexus 7 でアプリケーションを試してみました... socket.connect()を呼び出すと、すべて「アクセス許可が拒否されました」という結果になりました... (私はマニフェストに適切なアクセス許可を持っています)そうしないと、他のデバイスでは機能しません。)

私がテストしたすべての新しいデバイスは、バージョン コードが 4.0 を超えているため、疑問に思っています。

APIの変更について知っている人はいますか? おそらく Android 4.0+ はセキュリティを強制しますか?

組み込みプログラムのログを見ると、Bonding 状態でエラーが発生しているようです...

洞察はありますか?

コード:

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

問題を調査するのに非常に長い時間を費やした後、エラーの1つの理由が見つかりました...一部のAndroidデバイスでは、自動Bluetoothピアリングが有効になっていない/許可されていません。

したがって、どうやら 2 つの接続方法を除いて、Bluetooth アダプターを有効にする方法も 2 つあります。 Bluetooth を静かに有効にします。

最初の方法では、確認ダイアログが表示され、ユーザーの操作が必要になりますが、もう 1 つは表示されず、Bluetooth 有効化の確認ダイアログが表示されず、ピアリングの確認も表示されず、接続エラーが発生します。

最初のアダプター有効化方法を使用すると、Nexus 7、Samsung S3、およびその他のいくつかのデバイスなど、ほとんどのデバイスで問題が解決しますが、一部のデバイスではまだ問題があり、その理由はよくわかりませんが、これは多くのデバイスが新しい実装で動作するようになったため、はるかに優れています。

于 2012-12-06T21:37:51.063 に答える