蓝牙不会将图像发送到其他设备

时间:2013-07-19 12:29:52

标签: android bluetooth

  

我是Android开发的新手。我创建了一个蓝牙应用程序,用于从我的应用程序向另一台设备发送图像。 首先我从图库加载图像并使用此uri将该图像发送到远程设备。并使用搜索按钮搜索可见设备,这是可见的列表视图。 我的问题是,当我点击列表视图(远程设备地址)时,远程设备无法配对,也没有发送图像。请帮助我谢谢...`

  

我的xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
    android:id="@+id/button_search"
    android:text="Search for listener"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/text_message"
    />
     <Button
            android:id="@+id/btnidOpenGallery"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/button_listen"
            android:text="OpenGallery" />


    <ListView
    android:id="@+id/list_discovered"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/btnidOpenGallery"
    android:layout_alignParentTop="true"
    />

    </RelativeLayout>
  

我的主要活动:

@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            btnOpenimage=(Button)findViewById(R.id.btnidOpenGallery);
            OpenImage();
            foundDevices = new ArrayList<BluetoothDevice>();
            configureBluetooth();
            setupListView();
            }



    private void configureBluetooth() {
            bluetooth = BluetoothAdapter.getDefaultAdapter();
        }

    private void OpenImage() {
            btnOpenimage.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_PICK);
                        startActivityForResult(Intent.createChooser(intent,
                                "Select Picture"), SELECT_PICTURE);
                    }
                });
        }

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==2) {
                    selectedImageUri = data.getData();

                }
             }


    private void setupSearchButton() {
            Button searchButton = (Button) findViewById(R.id.button_search);

            searchButton.setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    registerReceiver(discoveryResult, new IntentFilter(
                            BluetoothDevice.ACTION_FOUND));

                    if (!bluetooth.isDiscovering()) {
                        foundDevices.clear();
                        bluetooth.startDiscovery();
                    }
                }
            });
        }

        private void setupListView() {
            list = (ListView) findViewById(R.id.list_discovered);
            aa = new ArrayAdapter<BluetoothDevice>(this,
                    android.R.layout.simple_list_item_1, foundDevices);
            list.setAdapter(aa);

            list.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> arg0, View view, int index,
                        long arg3) {
                    AsyncTask<Integer, Void, Void> connectTask = new AsyncTask<Integer, Void, Void>() {
                        @Override
                        protected Void doInBackground(Integer... params) {
                            try {
                                BluetoothDevice device = foundDevices
                                        .get(params[0]);
                                socket = device
                                        .createRfcommSocketToServiceRecord(uuid);
                                socket.connect();
                            } catch (IOException e) {
                                Log.d("BLUETOOTH_CLIENT", e.getMessage());
                            }
                            return null;
                        }

                        @Override
                        protected void onPostExecute(Void result) {
                            //switchUI();
                            try {
                                sendFile(selectedImageUri, socket);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    };
                    connectTask.execute(index);
                }
            });
        }



        public void sendFile(Uri uri, BluetoothSocket bs) throws IOException {
            BufferedInputStream bis = new BufferedInputStream(getContentResolver().openInputStream(uri));
            OutputStream os = bs.getOutputStream();
        try {
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];

            // we need to know how may bytes were read to write them to the byteBuffer
            int len = 0;
            while ((len = bis.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
        } finally {
            bis.close();
            os.flush();
            os.close();
            bs.close();
        }
    } 

0 个答案:

没有答案