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
Android通过Bluetooth蓝牙发送手机照片文件到电脑Windows PC端
最新推荐文章于 2025-06-24 13:19:45 发布