Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 连接线程建立失败_Java_Android_Android Bluetooth - Fatal编程技术网

Java 连接线程建立失败

Java 连接线程建立失败,java,android,android-bluetooth,Java,Android,Android Bluetooth,当我试图通过从配对设备列表中选择蓝牙设备建立连接时,应用程序崩溃并返回到主活动 PairingList.java public class PairingList extends Activity implements AdapterView.OnItemClickListener { private static final int SUCCESS_CONNECT = 0; private static final int MESSAGE_READ = 1;

当我试图通过从配对设备列表中选择蓝牙设备建立连接时,应用程序崩溃并返回到主活动

PairingList.java

 public class PairingList extends Activity implements AdapterView.OnItemClickListener {
        private static final int SUCCESS_CONNECT = 0;
        private static final int MESSAGE_READ = 1;
        ListView lview;
        String[] paires;
        ArrayAdapter<String> adapter;
        Set<BluetoothDevice> devicesarray;
        BluetoothAdapter mBluetoothAdapter;
        ArrayList<BluetoothDevice> devices;
        public BluetoothSocket socket;
        public final UUID MY_UUID = UUID.fromString("00001802-0000-1000-8000-00805f9b34fb");
        Handler mHandler = new Handler(){
            @Override
             public void handleMessage(Message msg)
                {
                    super.handleMessage(msg);
                    switch (msg.what){
                        case SUCCESS_CONNECT:
                            ConnectedThread connectedThread=new ConnectedThread((BluetoothSocket) msg.obj);
                            String s="Successfully Connected";
                            connectedThread.write(s.getBytes());
                            break;
                        case MESSAGE_READ:
                            byte[] readBuf=(byte[])msg.obj;
                            String s1=new String(readBuf);
                            Toast.makeText(getBaseContext(),"Connect",Toast.LENGTH_SHORT).show();
                        }
                }
        };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.pairinglist_layout);
            lview= (ListView) findViewById(R.id.listviewid);
            mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
            Bundle bn1=getIntent().getExtras();
            paires=bn1.getStringArray("paires");
            adapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,paires);
            lview.setAdapter(adapter);
            lview.setOnItemClickListener(this);
            devices=new ArrayList<BluetoothDevice>();

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_pairinglist_layout, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            BluetoothDevice selectedDevice=devices.get(position);
            ConnectThread connect=new ConnectThread(selectedDevice);
            connect.start();
        }
        private class ConnectThread extends Thread {
            private final BluetoothSocket mmSocket;
            private final BluetoothDevice mmDevice;

            public ConnectThread(BluetoothDevice device) {
                // Use a temporary object that is later assigned to mmSocket,because mmSocket is final
                BluetoothSocket tmp = null;
                mmDevice = device;

                // Get a BluetoothSocket to connect with the given BluetoothDevice
                try {
                    // MY_UUID is the app's UUID string, also used by the server code
                    tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
                } catch (IOException e) { }
                mmSocket = tmp;
            }

            public void run() {
                mBluetoothAdapter.cancelDiscovery();   // Cancel discovery because it will slow down the connection

                try {
                    // Connect the device through the socket. This will block until it succeeds or throws an exception
                    mmSocket.connect();
                } catch (IOException connectException) {
                    // Unable to connect; close the socket and get out
                    try {
                        mmSocket.close();
                    } catch (IOException closeException) { }
                    return;
                }

                // Do work to manage the connection (in a separate thread)
                mHandler.obtainMessage(SUCCESS_CONNECT,mmSocket).sendToTarget();
            }



            /** Will cancel an in-progress connection, and close the socket */
            public void cancel() {
                try {
                    mmSocket.close();
                } catch (IOException e) { }
            }
        }
        private class ConnectedThread extends Thread {
            private final BluetoothSocket mmSocket;
            private final InputStream mmInStream;
            private final OutputStream mmOutStream;

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

                // Get the input and output streams, using temp objects because
                // member streams are final
                try {
                    tmpIn = socket.getInputStream();
                    tmpOut = socket.getOutputStream();
                } catch (IOException e) { }

                mmInStream = tmpIn;
                mmOutStream = tmpOut;
            }

            public void run() {
                byte[] buffer ;  // buffer store for the stream
                int bytes; // bytes returned from read()

                // Keep listening to the InputStream until an exception occurs
                while (true) {
                    try {
                        // Read from the InputStream
                        buffer= new byte[1024];
                        bytes = mmInStream.read(buffer);
                        // Send the obtained bytes to the UI activity
                        mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                                .sendToTarget();
                    } catch (IOException e) {
                        break;
                    }
                }
            }

            /* Call this from the main activity to send data to the remote device */
            public void write(byte[] bytes) {
                try {
                    mmOutStream.write(bytes);
                } catch (IOException e) { }
            }

            /* Call this from the main activity to shutdown the connection */
            public void cancel() {
                try {
                    mmSocket.close();
                } catch (IOException e) { }
            }
        }
    }
公共类配对列表扩展活动实现AdapterView.OnItemClickListener{
私有静态最终int成功_CONNECT=0;
私有静态最终int消息_READ=1;
列表视图lview;
字符串[]对;
阵列适配器;
设置设备阵列;
蓝牙适配器mBluetoothAdapter;
阵列列表设备;
公共蓝牙插座;
公共最终UUID MY_UUID=UUID.fromString(“00001802-0000-1000-8000-00805f9b34fb”);
Handler mHandler=新处理程序(){
@凌驾
公共无效handleMessage(消息消息消息)
{
超级handleMessage(msg);
开关(msg.what){
成功案例(u CONNECT):
ConnectedThread ConnectedThread=新的ConnectedThread((BluetoothSocket)msg.obj);
字符串s=“已成功连接”;
connectedThread.write(s.getBytes());
打破
案例信息如下:
字节[]readBuf=(字节[])msg.obj;
字符串s1=新字符串(readBuf);
Toast.makeText(getBaseContext(),“Connect”,Toast.LENGTH_SHORT.show();
}
}
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.pairinglist_布局);
lview=(ListView)findViewById(R.id.listviewid);
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
Bundle bn1=getIntent().getExtras();
paires=bn1.getStringArray(“paires”);
adapter=newarrayadapter(这是android.R.layout.simple\u list\u item\u 1,paires);
lview.setAdapter(适配器);
lview.setonicmclicklistener(这个);
设备=新的ArrayList();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu\u pairinglist\u布局,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Bluetooth设备selectedDevice=devices.get(位置);
ConnectThread connect=新的ConnectThread(已选择的设备);
connect.start();
}
私有类ConnectThread扩展线程{
私人最终蓝牙插座mmSocket;
专用最终蓝牙设备;
公共连接线程(蓝牙设备){
//使用稍后指定给mmSocket的临时对象,因为mmSocket是最终对象
BluetoothSocket tmp=null;
mmDevice=设备;
//获取BluetoothSocket以连接给定的BluetoothDevice
试一试{
//MY_UUID是应用程序的UUID字符串,也由服务器代码使用
tmp=device.createrFComSocketToServiceRecord(我的UUID);
}捕获(IOE){}
mmSocket=tmp;
}
公开募捐{
mBluetoothAdapter.cancelDiscovery();//取消查找,因为它会减慢连接速度
试一试{
//通过套接字连接设备。这将阻止,直到它成功或引发异常
mmSocket.connect();
}捕获(IOException-connectException){
//无法连接;请关闭插座并退出
试一试{
mmSocket.close();
}捕获(IOException closeException){}
返回;
}
//执行管理连接的工作(在单独的线程中)
mHandler.ActainMessage(成功连接,彩信储存)。发送到目标();
}
/**将取消正在进行的连接,并关闭套接字*/
公开作废取消(){
试一试{
mmSocket.close();
}捕获(IOE){}
}
}
私有类ConnectedThread扩展线程{
私人最终蓝牙插座mmSocket;
私有最终输入流mmInStream;
私有最终输出流mmOutStream;
公共连接线程(BluetoothSocket){
mmSocket=插座;
InputStream tmpIn=null;
OutputStream tmpOut=null;
//使用临时对象获取输入和输出流,因为
//成员流是最终的
试一试{
tmpIn=socket.getInputStream();
tmpOut=socket.getOutputStream();
}捕获(IOE){}
mmInStream=tmpIn;
mmOutStream=tmpOut;
}
公开募捐{
byte[]buffer;//流的缓冲区存储
@Override

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    BluetoothDevice selectedDevice=devices.get(position);
    ConnectThread connect=new ConnectThread(selectedDevice);
    connect.start();
}