连接到其他蓝牙设备时,如何达到“连接状态”?

时间:2019-06-03 11:56:22

标签: android bluetooth connection uuid

我设法做到了,通过蓝牙绑定(和我认为可以连接)我的平板电脑和智能手机。 我用this寻求帮助。

我目前的状态是mSocket.connect();没有引发任何异常,我的代码移至了Connected线程中。

但是我现在不想传输数据,我只想要一个连接, 因此我将run ()中的ConnectedThread设置为while(true){;}

我的问题是BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED的Brodcastreceiver不会移入STATE_CONNECTED,而且我的应用程序也不会崩溃。

是因为我使用了错误的UUID吗? 我需要手机的UUID吗? 当我尝试连接到BluetoothDongle时,是否有必要从Dongle中获取特殊的UUID?

顺便说一句:我没有在手机上刷新任何软件,只是在平板电脑上刷新了APP。

ConnectedThread的代码,仅在device#connect();时可访问;没事

private class ConnectedThread extends Thread{
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            this.mmSocket= socket;
            InputStream tmpIn =null;
            OutputStream tmpOut =null;

            mProgressDialog.dismiss();

            try {
                tmpIn =this.mmSocket.getInputStream();
                tmpOut= this.mmSocket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }

            mmInStream =tmpIn;
            mmOutStream =tmpOut;
        }

        public void run() {
            byte[] buffer = new byte[1024];
            int bytes;
            while (true)
            {
                ;
            }
        }

        public void write(byte[] bytes) {
            String text = new String(bytes, Charset.defaultCharset());
            Log.d(TAG, "write: Writing to outputstream: " + text);
            try {
                mmOutStream.write(bytes);
            } catch (IOException e) {
                Log.e(TAG, "write: Error writing to output stream. " + e.getMessage() );
            }
        }

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

我检查了mSocket.isConnected();它返回false。

0 个答案:

没有答案
相关问题