簡體   English   中英

Android中的藍牙連接期間未創建連接(失敗或中止)的異常

[英]Connection is not created (failed or aborted) exception thrown during Bluetooth connection in Android

我正在開發一個項目,該項目使用了一個運行於4.1.2上的android平板電腦(iball 3G 7271),該平板電腦由MTK處理器和RN42藍牙(PCB上的芯片)制成。這兩個模塊相互通信以進行傳輸數據通過藍牙。

我遇到了問題。在某些情況下,我反復遇到異常,指出``未創建連接(失敗或中止)''。我發現了如下所述的實例:

我從PlayStation的BlueTerm應用程序中復制了相關代碼,並制作了一個示例應用程序來測試BT連接.Blueterm是一款用於測試設備與RN42之間的藍牙連接的應用程序。

1)我將應用程序連接到RN42或斷開了連接,發現它一直在工作,正在正確連接和斷開連接。 2)我模擬了電源故障(僅通過關閉RN42模塊),然后斷開並重新連接了app和RN42之間的BT連接,發現平板電腦已與RN42重新連接,沒有太大問題。

3) 重新安裝應用程序與BT連接到RN42之間的鏈接

測試案例1:在重新安裝之前,該應用已與RN42斷開連接; 結果-重新安裝后,在重新安裝的應用程序中BT重新連接到RN42的工作正常。

測試案例2:在重新安裝之前,該應用程序已與RN42處於連接狀態;結果-在重新安裝之后,未發生BT與RN42的重新連接。 我跟蹤到測試用例2的異常是:

W/System.err(4603): java.io.IOException: [JSR82] connect: Connection is not created (failed or aborted).
W/System.err(4603):     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:395)

這是幾天前我一次又一次收到的異常。所以現在我有點知道何時可以拋出該異常。

注意:對於“測試用例2”,此處甚至卸載並重新安裝應用程序然后嘗試將應用程序連接到RN42均不起作用。我們需要重新啟動平板電腦以使應用程序再次連接到BT。此外,我什至嘗試連接真正的BlueTerm應用程序(在測試用例2中),但也沒有連接。因此我嘗試關閉並打開平板電腦的BT。我觀察到先關閉然后再打開BT,然后嘗試建立BT平板電腦與RN42之間正在建立連接。但是現在,我沒有從RN42到平板電腦的任何輸入信號,而是能夠將數據從平板電腦發送到RN42。

在Samsung S2 / grand / nexus設備上測試對於上述測試用例2,在重新安裝后,即使重新安裝應用程序未通過BT將其連接到RN42,在重新安裝后,這些應用程序也確實已連接到BT。

以下是我的應用程序代碼和log cat異常:

BlueTerm.java

        @SuppressLint("HandlerLeak")
        public class BlueTerm extends Activity {

            BluetoothSocket Socket;
            OutputStream DataOut;
            InputStream DataIn;


            // Intent request codes
            private static final int REQUEST_CONNECT_DEVICE = 1;
            private static final int REQUEST_ENABLE_BT = 2;

            private static TextView mTitle;

            // Name of the connected device
            private String mConnectedDeviceName = null;

            /**
             * Set to true to add debugging code and logging.
             */
            public static final boolean DEBUG = true;

            /**
             * Set to true to log each character received from the remote process to the
             * android log, which makes it easier to debug some kinds of problems with
             * emulating escape sequences and control codes.
             */
            public static final boolean LOG_CHARACTERS_FLAG = DEBUG && false;

            /**
             * Set to true to log unknown escape sequences.
             */
            public static final boolean LOG_UNKNOWN_ESCAPE_SEQUENCES = DEBUG && false;

            /**
             * The tag we use when logging, so that our messages can be distinguished
             * from other messages in the log. Public because it's used by several
             * classes.
             */
            public static final String LOG_TAG = "BlueTerm";

            // Message types sent from the BluetoothReadService Handler
            public static final int MESSAGE_STATE_CHANGE = 1;
            public static final int MESSAGE_READ = 2;
            public static final int MESSAGE_WRITE = 3;
            public static final int MESSAGE_DEVICE_NAME = 4;
            public static final int MESSAGE_TOAST = 5;  

            // Key names received from the BluetoothChatService Handler
            public static final String DEVICE_NAME = "device_name";
            public static final String TOAST = "toast";

            private BluetoothAdapter mBluetoothAdapter = null;

            private static BluetoothSerialService mSerialService = null;

            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                if (DEBUG)
                    Log.e(LOG_TAG, "+++ ON CREATE +++");
                setContentView(R.layout.main);


                mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

                mSerialService = new BluetoothSerialService(this, mHandlerBT);

                Button buzzerOn = (Button) findViewById(R.id.button1);
                buzzerOn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Log.v("BlueTerm","Buzzer button clicked");

                        //send("37".getBytes());
                        send(bigIntToByteArray(37));
                    }
                });

                Button buzzerOff = (Button) findViewById(R.id.button2);
                buzzerOff.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Log.v("BlueTerm","Buzzer button clicked");

                        //send("37".getBytes());
                        send(bigIntToByteArray(30));
                    }
                });

                Button recon = (Button) findViewById(R.id.button3);
                recon.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Log.v("BlueTerm","recon button clicked");

                        BluetoothAdapter iballAdapter = BluetoothAdapter.getDefaultAdapter();
                        BluetoothDevice RN42_Device = iballAdapter.getRemoteDevice("00:06:66:49:57:5F");

                        try {
                            Socket = RN42_Device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
                            DataOut = Socket.getOutputStream();
                            DataIn = Socket.getInputStream();

                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                        if (DataIn != null) {
                            Log.d("AppFunctions","DataIn is not null,so making it NULL");
                            try {DataIn.close();} catch (Exception e) {}
                            DataIn = null;
                        }

                        Log.i("AppFunctions", "DataOut -" + DataOut);
                        if (DataOut != null) {
                            Log.d("AppFunctions","DataOut is not null,so making it NULL");
                            try {DataOut.close();} catch (Exception e) {}
                            DataOut = null;
                        }

                        Log.i("AppFunctions", "Socket -" + Socket);
                        if (Socket != null) {
                            Log.d("AppFunctions","Socket is not null,so making it NULL");
                            try {Socket.close();} catch (Exception e) {}
                            Socket = null;
                        }

                        try {
                            Socket = RN42_Device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
                            Socket.connect();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (mSerialService != null)
                            mSerialService.stop();
                            mSerialService.start();

                    }
                });

                if (DEBUG)
                    Log.e(LOG_TAG, "+++ DONE IN ON CREATE +++");
            }

            @Override
            public void onStart() {
                super.onStart();
                if (DEBUG)
                    Log.e(LOG_TAG, "++ ON START ++");

                mEnablingBT = false;
            }

            @Override
            public synchronized void onResume() {
                super.onResume();

                if (DEBUG) {
                    Log.e(LOG_TAG, "+ ON RESUME +");
                }       
                    if (mSerialService != null) {
                        Log.v("BlueTerm","mSerialService is NOT null");
                        // Only if the state is STATE_NONE, do we know that we haven't started already
                        if (mSerialService.getState() == BluetoothSerialService.STATE_NONE) {
                            // Start the Bluetooth chat services
                            Log.v("BlueTerm","starting BT chat service");
                            mSerialService.start();
                        }
                    }
                }
            }

            @Override
            public synchronized void onPause() {
                super.onPause();
                if (DEBUG)
                    Log.e(LOG_TAG, "- ON PAUSE -");
            }

            @Override
            public void onStop() {
                super.onStop();
                if(DEBUG)
                    Log.e(LOG_TAG, "-- ON STOP --");
            }


            @Override
            public void onDestroy() {
                super.onDestroy();
                if (DEBUG)
                    Log.e(LOG_TAG, "--- ON DESTROY ---");

                if (mSerialService != null)
                    mSerialService.stop();

            }

            public int getConnectionState() {
                return mSerialService.getState();
            }


            public void send(byte[] out) {
                mSerialService.write( out );
            }

            // The Handler that gets information back from the BluetoothService
            private final Handler mHandlerBT = new Handler() {

                @Override
                public void handleMessage(Message msg) {            
                    switch (msg.what) {
                    case MESSAGE_STATE_CHANGE:
                        if(DEBUG) Log.i(LOG_TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
                        switch (msg.arg1) {
                        case BluetoothSerialService.STATE_CONNECTED:
                            break;

                        case BluetoothSerialService.STATE_CONNECTING:
                            break;

                        case BluetoothSerialService.STATE_LISTEN:
                        case BluetoothSerialService.STATE_NONE:
                            Log.d("BlueTerm","inside STATE_NONE in handler");
                            break;
                        }
                        break;
                    case MESSAGE_WRITE:
                        if (mLocalEcho) {
                            byte[] writeBuf = (byte[]) msg.obj;
                        }

                        break;

                    case MESSAGE_READ:
                        byte[] readBuf = (byte[]) msg.obj;              
                        Log.d("incoming writebytes",""+readBuf.toString());

                        break;

                    case MESSAGE_DEVICE_NAME:
                        // save the connected device's name
                        mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
                        Toast.makeText(getApplicationContext(), "Connected to "
                                       + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
                        break;
                    case MESSAGE_TOAST:
                        Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
                                       Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            };    

            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                if(DEBUG) Log.d(LOG_TAG, "onActivityResult " + resultCode);
                switch (requestCode) {

                case REQUEST_CONNECT_DEVICE:

                    // When DeviceListActivity returns with a device to connect
                    if (resultCode == Activity.RESULT_OK) {
                        // Get the device MAC address
                        String address = "00:06:66:49:57:5F";
                        // Get the BLuetoothDevice object
                        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
                        // Attempt to connect to the device
                        mSerialService.connect(device);                
                    }
                    break;

                case REQUEST_ENABLE_BT:
                    // When the request to enable Bluetooth returns
                    if (resultCode == Activity.RESULT_OK) {
                        Log.d(LOG_TAG, "BT not enabled");

                    }
                }
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                MenuInflater inflater = getMenuInflater();
                inflater.inflate(R.menu.option_menu, menu);
                mMenuItemConnect = menu.getItem(0);
                return true;
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                case R.id.connect:

                    if (getConnectionState() == BluetoothSerialService.STATE_NONE) {                
                        String address = "00:06:66:49:57:5F";
                        // Get the BLuetoothDevice object
                        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
                        Log.d("BlueTerm","device: " + device);
                        // Attempt to connect to the device
                        mSerialService.connect(device);
                    }
                    else
                        if (getConnectionState() == BluetoothSerialService.STATE_CONNECTED) {
                            mSerialService.stop();
                            mSerialService.start();
                        }
                    return true;
                case R.id.preferences:
                    //doPreferences();
                    return true;
                case R.id.menu_special_keys:
                    //doDocumentKeys();
                    return true;
                }
                return false;
            }

            private byte[] bigIntToByteArray( final int i ) {
                BigInteger bigInt = BigInteger.valueOf(i);      
                return bigInt.toByteArray();
            }
        }

藍牙服務:

        /**
         * This class does all the work for setting up and managing Bluetooth
         * connections with other devices. It has a thread that listens for
         * incoming connections, a thread for connecting with a device, and a
         * thread for performing data transmissions when connected.
         */
        public class BluetoothSerialService {
            // Debugging
            private static final String TAG = "BluetoothReadService";
            private static final boolean D = true;


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

            // Member fields
            private final BluetoothAdapter mAdapter;
            private final Handler mHandler;
            private ConnectThread mConnectThread;
            private ConnectedThread mConnectedThread;
            private int mState;


            // Constants that indicate the current connection state
            public static final int STATE_NONE = 0;       // we're doing nothing
            public static final int STATE_LISTEN = 1;     // now listening for incoming connections
            public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection
            public static final int STATE_CONNECTED = 3;  // now connected to a remote device

            /**
             * Constructor. Prepares a new BluetoothChat session.
             * @param context  The UI Activity Context
             * @param handler  A Handler to send messages back to the UI Activity
             */
            public BluetoothSerialService(Context context, Handler handler) {
                mAdapter = BluetoothAdapter.getDefaultAdapter();
                mState = STATE_NONE;
                mHandler = handler;
               // mEmulatorView = emulatorView;
            }

            /**
             * Set the current state of the chat connection
             * @param state  An integer defining the current connection state
             */
            private synchronized void setState(int state) {
                if (D) Log.d(TAG, "setState() " + mState + " -> " + state);
                mState = state;
                Log.d("BluetoothSerialService","state : " + state);
                // Give the new state to the Handler so the UI Activity can update
                mHandler.obtainMessage(BlueTerm.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();
            }

            /**
             * Return the current connection state. */
            public synchronized int getState() {
                return mState;
            }

            /**
             * Start the chat service. Specifically start AcceptThread to begin a
             * session in listening (server) mode. Called by the Activity onResume() */
            public synchronized void start() {
                if (D) Log.d(TAG, "start");

                // Cancel any thread attempting to make a connection
                if (mConnectThread != null) {
                    mConnectThread.cancel(); 
                    mConnectThread = null;
                }

                // Cancel any thread currently running a connection
                if (mConnectedThread != null) {
                    mConnectedThread.cancel(); 
                    mConnectedThread = null;
                }

                setState(STATE_NONE);
            }

            /**
             * Start the ConnectThread to initiate a connection to a remote device.
             * @param device  The BluetoothDevice to connect
             */
            public synchronized void connect(BluetoothDevice device) {
                if (D) Log.d(TAG, "connect to: " + device);

                // Cancel any thread attempting to make a connection
                if (mState == STATE_CONNECTING) {
                    if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
                }

                // Cancel any thread currently running a connection
                if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}

                // Start the thread to connect with the given device
                mConnectThread = new ConnectThread(device);
                mConnectThread.start();
                setState(STATE_CONNECTING);
            }

            /**
             * Start the ConnectedThread to begin managing a Bluetooth connection
             * @param socket  The BluetoothSocket on which the connection was made
             * @param device  The BluetoothDevice that has been connected
             */
            public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
                if (D) Log.d(TAG, "connected");

                // Cancel the thread that completed the connection
                if (mConnectThread != null) {
                    mConnectThread.cancel(); 
                    mConnectThread = null;
                }

                // Cancel any thread currently running a connection
                if (mConnectedThread != null) {
                    mConnectedThread.cancel(); 
                    mConnectedThread = null;
                }

                // Start the thread to manage the connection and perform transmissions
                mConnectedThread = new ConnectedThread(socket);
                mConnectedThread.start();

                // Send the name of the connected device back to the UI Activity
                Message msg = mHandler.obtainMessage(BlueTerm.MESSAGE_DEVICE_NAME);
                Bundle bundle = new Bundle();
                bundle.putString(BlueTerm.DEVICE_NAME, device.getName());
                msg.setData(bundle);
                mHandler.sendMessage(msg);

                setState(STATE_CONNECTED);
            }

            /**
             * Stop all threads
             */
            public synchronized void stop() {
                if (D) Log.d(TAG, "stop");


                if (mConnectThread != null) {
                    mConnectThread.cancel(); 
                    mConnectThread = null;
                }

                if (mConnectedThread != null) {
                    mConnectedThread.cancel(); 
                    mConnectedThread = null;
                }

                setState(STATE_NONE);
            }

            /**
             * Write to the ConnectedThread in an unsynchronized manner
             * @param out The bytes to write
             * @see ConnectedThread#write(byte[])
             */
            public void write(byte[] out) {
                Log.e("BluetoothSerialService","Inside write fn" + "    :" + out.toString());
                // Create temporary object
                ConnectedThread r;
                // Synchronize a copy of the ConnectedThread
                synchronized (this) {
                    if (mState != STATE_CONNECTED) return;
                    r = mConnectedThread;
                }
                // Perform the write unsynchronized
                r.write(out);
            }

            /**
             * Indicate that the connection attempt failed and notify the UI Activity.
             */
            private void connectionFailed() {
                setState(STATE_NONE);

                // Send a failure message back to the Activity
                Message msg = mHandler.obtainMessage(BlueTerm.MESSAGE_TOAST);
                Bundle bundle = new Bundle();
                bundle.putString(BlueTerm.TOAST, "Unable to connect device");
                msg.setData(bundle);
                mHandler.sendMessage(msg);
            }

            /**
             * Indicate that the connection was lost and notify the UI Activity.
             */
            private void connectionLost() {
                setState(STATE_NONE);

                // Send a failure message back to the Activity
                Message msg = mHandler.obtainMessage(BlueTerm.MESSAGE_TOAST);
                Bundle bundle = new Bundle();
                bundle.putString(BlueTerm.TOAST, "Device connection was lost");
                msg.setData(bundle);
                mHandler.sendMessage(msg);
            }

            /**
             * This thread runs while attempting to make an outgoing connection
             * with a device. It runs straight through; the connection either
             * succeeds or fails.
             */
            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(SerialPortServiceClass_UUID);
                    } catch (IOException e) {
                        Log.e(TAG, "create() failed", e);
                    }
                    mmSocket = tmp;
                }

                public void run() {
                    Log.e(TAG, "BEGIN mConnectThread");
                    setName("ConnectThread");

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

                    // 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) {
                        connectionFailed();
                        // Close the socket
                        try {
                            mmSocket.close();
                        } catch (IOException e2) {
                            Log.e(TAG, "unable to close() socket during connection failure", e2);
                        }
                        // Start the service over to restart listening mode
                        //BluetoothSerialService.this.start();
                        return;
                    }

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

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

                public void cancel() {
                    try {
                        mmSocket.close();
                    } catch (IOException e) {
                        Log.e(TAG, "close() of connect socket failed", e);
                    }
                }
            }

            /**
             * This thread runs during a connection with a remote device.
             * It handles all incoming and outgoing transmissions.
             */
            private class ConnectedThread extends Thread {
                private final BluetoothSocket mmSocket;
                private final InputStream mmInStream;
                private final OutputStream mmOutStream;


                public ConnectedThread(BluetoothSocket socket) {
                    Log.e(TAG, "create ConnectedThread");
                    mmSocket = socket;
                    InputStream tmpIn = null;
                    OutputStream tmpOut = null;

                    // Get the BluetoothSocket input and output streams
                    try {
                        tmpIn = socket.getInputStream();
                        tmpOut = socket.getOutputStream();
                    } catch (IOException e) {
                        Log.e(TAG, "temp sockets not created", e); 
                    } 

                    mmInStream = tmpIn;
                    mmOutStream = tmpOut;
                }

                public void run() {
                    Log.e(TAG, "BEGIN mConnectedThread");
                    byte[] buffer = new byte[1024];
                    int bytes;

                    // Keep listening to the InputStream while connected
                    Log.e(TAG, "Entering while");
                    while (true) {
                        Log.e(TAG, "Inside while");
                        try {
                            // Read from the InputStream
                            bytes = 0;
                            Log.d("incoming bytes",""+bytes);
                            Log.e("BT","Inputstream :" + mmInStream);
                            //bytes = mmInStream.read(buffer);
                            bytes = mmInStream.read();
                            Log.d("incoming bytes",""+bytes);
                            //mEmulatorView.write(buffer, bytes);
                            // Send the obtained bytes to the UI Activity
                            mHandler.obtainMessage(BlueTerm.MESSAGE_READ, bytes, -1, buffer).sendToTarget();

                            String a = buffer.toString();  
                            a = ""; 
                        } catch (Exception e) {
                            Log.e(TAG, "disconnected", e); 
                            connectionLost();  
                            break;
                        }
                        bytes = 0;
                    }
                    //Log.e(TAG, "Outside while");
                }

                /**
                 * Write to the connected OutStream.
                 * @param buffer  The bytes to write
                 */
                public void write(byte[] buffer) {
                    try {
                        Log.d("writing to outStream byte :", buffer.toString());
                        mmOutStream.write(buffer);

                        // Share the sent message back to the UI Activity
                        //mHandler.obtainMessage(BlueTerm.MESSAGE_WRITE, buffer.length, -1, buffer).sendToTarget();
                    } catch (IOException e) {
                        Log.e(TAG, "Exception during write", e);
                    }
                }

                public void cancel() {
                    try {
                        mmSocket.close();
                    } catch (Exception e) {
                        Log.e(TAG, "close() of connect socket failed", e);
                    }
                }
            }
        }

Logcat錯誤:

W / System.err(4603):java.io.IOException:[JSR82] connect:未創建連接(失敗或中止)。 W / System.err(4603):位於android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:395)


我懷疑可能會引發此錯誤,因為在上述測試案例中,該應用無法訪問活動的BT端口。原因是因為我閱讀了以下站點: https : //code.google.com/p/ android / issues / detail?id = 5427 (請參閱dknop的答案#19)我觀察到在我測試過的三星s2 / grand / nexus設備上均未發生此問題。我的錯誤也可能是由於MTK處理器/它的BT固件/ iball自定義android問題。( http://redacacia.me/2012/07/17/overcoming-android-bluetooth-blues-with-reflection-method/

有沒有人遇到過類似的問題? 任何幫助將不勝感激。非常感謝!

我已經在數十種不同的平板電腦上測試了藍牙連接性,並且看到了一些非常不穩定的行為。 我的應用程序總是在循環中調用createRfcommSocketToServiceRecord(...) ,因為有時它會由於沒有明顯的原因而失敗,僅在以后的嘗試中成功。 而且,API不允許您區分暫時性故障和永久性故障。 有些平板電腦會在錯誤消息的文本中為您提供線索,而其他平板電腦則不會。

我通常嘗試以100ms的間隔連接100次,然后停止並詢問用戶他們嘗試連接的設備是否已打開並且在范圍內。 在我測試過的一種特定的平板電腦型號上,連接失敗超過300次並不罕見……然后它會突然工作,好像沒有任何錯誤。

我已經使用這段代碼來使我與藍牙設備(即藍牙打印機)的連接穩定。 現在,它連接10次有9.9次連接。如果仍然出現錯誤,我將以編程方式再次重置我的藍牙,再次調用此代碼,然后連接10次有10次連接。

public boolean connectToPrinter(String printerName) throws IOException 
    {
        BluetoothAdapter.getDefaultAdapter().cancelDiscovery();

        BluetoothDevice device = getPrinterByName(printerName);

        if (bluetoothSocket != null) 
        {
            bluetoothSocket.close();
        }


        try {

            Method m=device.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
            bluetoothSocket=    (BluetoothSocket) m.invoke(device, 1);          

        } catch (Exception e) {
            e.printStackTrace();
        }


        if (bluetoothSocket == null)
            return false;

        bluetoothSocket.connect();

        return true;
    }

這是getPrinterByName()的代碼:

private BluetoothDevice getPrinterByName(String printerName) 
    {
        Set<BluetoothDevice> pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();

        for (BluetoothDevice device : pairedDevices) 
        {

            Log.e("","device name: "+device.getName());

            if (device.getName() == null)
                continue;
            if (device.getName().contains(printerName)) 
            {
                remoteDevice = device;
                //              pairPrinter(printerName);
                return remoteDevice;
            }
        }
        return null;
    }

bluetoothSocket是BluetoothSocket類的Object。 並且不要忘記在線程中運行此代碼,否則將阻塞您的主線程。 :-)

嗨,朋友,只需使用反射將代碼替換為連接到藍牙套接字的代碼即可建立連接.....

private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
      if(Build.VERSION.SDK_INT >= 10){
          try {
              final Method  m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
              return (BluetoothSocket) m.invoke(device, MY_UUID);
          } catch (Exception e) {
              Log.e(TAG, "Could not create Insecure RFComm Connection",e);
          }
      }
      return  device.createRfcommSocketToServiceRecord(MY_UUID);
  }

Quote:我觀察到通過關閉BT然后再打開BT,然后試圖在平板電腦和RN42之間建立BT連接的方式發生了。但是現在,我沒有從RN42到平板電腦的任何輸入信號,但是能夠發送數據從平板電腦到RN42。

因此,在重新安裝應用程序之前,是否有可能從rn42接收輸入信號?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM