2015年12月20日 星期日

Android Bluetooth Transfer Data 藍芽 傳送 檔案

藍芽傳送檔案,須先準備兩台並配對好藍芽
1.AndroidManifest.xml 加上權限


    
    
    

    
        
            
                

                
            
        
    




2./res/layout/activity_main.xml 放個Button


    



3.MainActivity.java
package com.terryyamg.bluetoothdatatransfertest;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final int DISCOVER_DURATION = 300;
    private static final int REQUEST_BLU = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btSend = (Button) findViewById(R.id.btSend);
        btSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendViaBluetooth(v);
            }
        });
    }

    public void sendViaBluetooth(View v) {

        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

        if (btAdapter == null) {
            Toast.makeText(this, "裝置沒有藍芽", Toast.LENGTH_LONG).show();
        } else {
            enableBluetooth();
        }
    }

    //啟動藍芽
    public void enableBluetooth() {

        Intent discoveryIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        //開啟藍芽時間
        discoveryIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISCOVER_DURATION);

        startActivityForResult(discoveryIntent, REQUEST_BLU);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == DISCOVER_DURATION && requestCode == REQUEST_BLU) {

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.setType("text/plain");//檔案類型
            File file = new File(Environment.getExternalStorageDirectory(), "BluetoothTest.txt"); //建立傳送檔案名稱

            String content = "Hello"; //文件內容
            try {
                FileOutputStream fop = new FileOutputStream(file);

                if (!file.exists()) { // 如果檔案不存在,建立檔案
                    file.createNewFile();
                }
                byte[] contentInBytes = content.getBytes();// 取的字串內容bytes
                fop.write(contentInBytes); // 輸出
                fop.flush();
                fop.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));

            PackageManager pm = getPackageManager();
            List<ResolveInfo> appsList = pm.queryIntentActivities(intent, 0);
            if (appsList.size() > 0) {
                String packageName = null;
                String className = null;
                boolean found = false;

                for (ResolveInfo info : appsList) {
                    packageName = info.activityInfo.packageName;
                    if (packageName.equals("com.android.bluetooth")) {
                        className = info.activityInfo.name;
                        found = true;
                        break;
                    }
                }

                if (!found) {
                    Toast.makeText(this, "沒有找到藍芽", Toast.LENGTH_LONG).show();
                } else {
                    intent.setClassName(packageName, className);
                    startActivity(intent);
                }
            }
        } else {
            Toast.makeText(this, "取消", Toast.LENGTH_LONG).show();
        }
    }
}




檔案下載:
 https://github.com/terryyamg/BluetoothDataTransferTest
參考連結:
https://www.youtube.com/watch?v=6hQ87u9v7SY

沒有留言 :

張貼留言