Android通过Bluetooth蓝牙发送手机照片文件到电脑Windows PC端

本文档介绍了如何使用C#编写PC端程序,通过Bluetooth蓝牙从Android设备发送照片文件到Windows PC。主要涉及蓝牙适配器的获取、文件选择、蓝牙地址的设置以及发送文件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 端代码

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Set;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {
    private BluetoothAdapter mBluetoothAdapter;

    //要连接的目标蓝牙设备(Windows PC电脑的名字)。自己更改
    private final String TARGET_DEVICE_NAME = "*********";

    private final String TAG = "蓝牙调试";
    //UUID必须是Android蓝牙客户端和Windows PC电脑端一致。
    private final String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB";
    // 通过广播接收系统发送出来的蓝牙设备发现通知。
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String name = device.getName();
                if (name != null)
                    Log.d(TAG, "发现蓝牙设备:" + name);
                if (name != null && name.equals("PHIL-PC")) {
                    Log.d(TAG, "发现目标蓝牙设备,开始线程连接");
                    new Thread(new ClientThread(device)).start();
                    // 蓝牙搜索是非常消耗系统资源开销的过程,一旦发现了目标感兴趣的设备,可以关闭扫描。
                    mBluetoothAdapter.cancelDiscovery();
                }
            }
        }
    };

    /**
     * 该线程往蓝牙服务器端发送文件数据。
     */
    private class ClientThread extends Thread {
        private BluetoothDevice device;

        public ClientThread(BluetoothDevice device) {
            this.device = device;
        }

        @Override
        public void run() {
            BluetoothSocket socket;

            try {
                socket = device.createRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));

                Log.d(TAG, "连接蓝牙服务端...");
                socket.connect();
                Log.d(TAG, "连接建立.");

                // 开始往服务器端发送数据。
                Log.d(TAG, "开始往蓝牙服务器发送数据...");
                sendDataToServer(socket);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        private void sendDataToServer(BluetoothSocket socket) {
            try {
                FileInputStream fis = new FileInputStream(getFile());
                BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());

                byte[] buffer = new byte[1024];
                int c;
                while (true) {
                    c = fis.read(buffer);
                    if (c == -1) {
                        Log.d(TAG, "读取结束");
                        break;
                    } else {
                        Log.d(TAG, "读取"+c);
                        bos.write(buffer, 0, c);
                    }
                }

                bos.flush();
                fis.close();
                bos.close();

                Log.d(TAG, "发送文件成功");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 发给给蓝牙服务器的文件。
     * 本例发送一张位于存储器根目录下名为 image.jpg 的照片。
     *
     * @return
     */
    private File getFile() {
//        File root = Environment.getExternalStorageDirectory();
//        File file = new File(root, "image.jpg");
        String folderName = Environment.getExternalStorageDirectory().getPath() + "/beidou".toString();
        File saveFile =new File(folderName,"0360405"+".txt".toString());

        return saveFile;
    }

    /**
     * 获得和当前Android蓝牙已经配对的蓝牙设备。
     *
     * @return
     */
    private BluetoothDevice getPairedDevices() {
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices != null && pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                // 把已经取得配对的蓝牙设备名字和地址打印出来。
                Log.d(TAG, device.getName() + " : " + device.getAddress());

                //如果已经发现目标蓝牙设备和Android蓝牙已经配对,则直接返回。
                if (TextUtils.equals(TARGET_DEVICE_NAME, device.getName())) {
                    Log.d(TAG, "已配对目标设备 -> " + TARGET_DEVICE_NAME);
                    return device;
                }
            }
        }

        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        BluetoothDevice device = getPairedDevices();
        if (device == null) {
            // 注册广播接收器。
            // 接收系统发送的蓝牙发现通知事件。
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(mBroadcastReceiver, filter);

            if (mBluetoothAdapter.startDiscovery()) {
                Lo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋风落叶黄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值