Android蓝牙发送文件(createChooser)

时间:2016-01-17 08:48:21

标签: java android android-studio bluetooth sendfile

我正在尝试通过蓝牙发送文件,但是当我选择文件时,没有任何反应。我试图通过点击设备(ListView)>来做到这一点。然后选择一个文件(在选择活动之后:文件管理器)>将所选文件发送到选定的(从ListView)设备,但它不起作用。 请考虑上面的顺序告诉我如何正确发送文件。

BluetoothActivity.java:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth);

    toggleButton = (ToggleButton) findViewById(R.id.toggleButton);

    listview = (ListView) findViewById(R.id.listView);
    // ListView Item Click Listener
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // ListView Clicked item value
            String itemValue = (String) listview.getItemAtPosition(position);

            String MAC = itemValue.substring(itemValue.length() - 17);

            BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice(MAC);

            // Initiate a connection request in a separate thread
            pickFile();

            ConnectingThread t = new ConnectingThread(bluetoothDevice);
            t.start();
        }
    });
    adapter = new ArrayAdapter
            (this,android.R.layout.simple_list_item_1);
    listview.setAdapter(adapter);

    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
onActivityResult:

    super.onActivityResult(requestCode, resultCode, data);
    try {
        Uri selectedImage = data.getData();
        Intent sendIntent = new Intent();
        sendIntent.setType("*/*");
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_STREAM, selectedImage);
        //startActivity(Intent.createChooser(sendIntent, ""));
        startActivity(Intent.createChooser(sendIntent, "Send file via:"));
    }
    catch(Exception e)
    {
    }
BluetoothActivity.java中的public void pickFile():

    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.setType("file/*");
    startActivityForResult(i, FILE_SELECT_CODE);

activity_bluetooth.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.abuk.kuba.androidconnection.BluetoothActivity">

<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toggleButton"
android:textOn="Bluetooth On"
android:textOff="Bluetooth Off"
android:onClick="onToggleClicked" />
<ListView
android:layout_width="wrap_content"
android:layout_height="340dp"
android:id="@+id/listView" />
</LinearLayout>

0 个答案:

没有答案