9

Android用のアプリケーションを開発しています。このアプリは、Bluetooth (BT) デバイスと通信する必要があります (いくつかのバイトを送信します)。デバイス (Samsung Galaxy mini) でのこのアプリのデバッグ/実行に問題があります。BT ソケットを作成してデバッグを停止すると、電話がフリーズし、バッテリーを取り出して再起動する必要があります。このアプリを(Eclipseから)実行する場合、すべて問題ありませんが、再度実行しようとすると、電話がフリーズし、アプリがインストールされません。2回目の実行前にこのアプリを手動でアンインストールしようとすると、電話が再びフリーズします。問題のあるコードは次のとおりです。

private final BluetoothDevice mmDevice;
private UUID uuid;

public ConnectionThread(BluetoothDevice device) {
    Log.d(TAG, "create ConnectionThread");

    uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
    BluetoothSocket tmp = null;
    mmDevice = device;

    try {
        tmp = mmDevice.createRfcommSocketToServiceRecord(uuid);
    } catch (IOException e) { }
    mmSocket = tmp;
    socketConnected = true;
}

これはスレッドのコンストラクタです。行にコメントすると

    tmp = mmDevice.createRfcommSocketToServiceRecord(uuid);

電話はフリーズしないので、ソケットの作成に問題があります(接続していません)。デバッグや実行のたびに電話を再起動するのはかなり面倒で、まだ多くの作業を行う必要があります。

このアプリを電話から (Eclipse から切断して) 実行すると、問題なく動作します。どこに問題があるのか​​、またはそれを修正する方法はありますか? ありがとうございました。

4

2 に答える 2

0

開発にはSGSIII miniも使用しています。次のコードは私にとってはうまくいきます:

    private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        mmDevice = device;

        BluetoothSocket tmp = null;

        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try {
            //tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            Log.e(LOG_TAG, "create() failed", e);
        }
        mmSocket = tmp;

        Main.myBluetoothSocket = mmSocket;
        Main.myBluetoothDevice = mmDevice;
    }

    @Override
    public void run() {
        Log.i(LOG_TAG, "BEGIN mConnectThread");
        setName("ConnectThread");

        // Always cancel discovery because it will slow down a connection
        mAdapter.cancelDiscovery();

        // Send a failure message back to the Activity
        Message msg = mHandler.obtainMessage(MESSAGE_TOAST);
        Log.e(LOG_TAG, "Attempting connection to " + mmSocket.getRemoteDevice().getName());
        String ss = "Attempting connection to " + mmSocket.getRemoteDevice().getName();
        Bundle bundle = new Bundle();
        bundle.putString(TOAST, ss);
        msg.setData(bundle);
        mHandler.sendMessage(msg);

        // Make a connection to the BluetoothSocket
        try {
            // This is a blocking call and will only return on a
            // successful connection or an exception
            mmSocket.connect();
        } catch (IOException e) {
            Log.e(LOG_TAG, "*+*+*+*   Connection Failed");
            connectionFailed();
            // Close the socket
            try {
                mmSocket.close();
            } catch (IOException e2) {
                Log.e(LOG_TAG, "unable to close() socket during connection failure", e2);
            }
            // Start the service over to restart listening mode
            BluetoothCommandService.this.start();
            return;
        }

        // Reset the ConnectThread because we're done
        synchronized (BluetoothCommandService.this) {
            mConnectThread = null;
        }

        // Start the connected thread
        connected(mmSocket, mmDevice);
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(LOG_TAG, "close() of connect socket failed", e);
        }
    }
}
于 2013-04-06T00:23:46.810 に答える
0

私も同じ問題に直面しています。Reflectionメソッドを使用すると機能します

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
BluetoothSocket socket = socket = (BluetoothSocket) m.invoke(device, 1);
于 2014-04-02T09:44:00.320 に答える