Android通过Bluetooth蓝牙发送手机照片文件到Windows PC:Java实现

本文在《Android通过蓝牙发送数据到Windows PC电脑:Java实现(链接地址:https://blog.csdn.net/zhangphil/article/details/83146705 )》基础上改进代码,还是用Java实现把Android手机上的一张照片,通过Bluetooth蓝牙连接,发送到Windows PC电脑上,实现上仍区分为Windows电脑端的蓝牙服务器端BluetoothJavaServer:

import java.io.BufferedInputStream;
import java.io.FileOutputStream;import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;/*** BluetoothJavaServer是蓝牙的服务器端。部署在Windows操作系统(PC电脑上)。 等待手机客户端或者其他蓝牙设备的连接。* * @author fly**/
public class BluetoothJavaServer {// 蓝牙服务器端的UUID必须和手机端的UUID一致。// 手机端的UUID需要去掉中间的-分割符。private String MY_UUID = "0000110100001000800000805F9B34FB";public static void main(String[] args) {new BluetoothJavaServer();}public BluetoothJavaServer() {StreamConnectionNotifier mStreamConnectionNotifier = null;try {mStreamConnectionNotifier = (StreamConnectionNotifier) Connector.open("btspp://localhost:" + MY_UUID);} catch (Exception e) {e.printStackTrace();}try {System.out.println("服务器端开始监听客户端连接请求...");while (true) {StreamConnection connection = mStreamConnectionNotifier.acceptAndOpen();System.out.println("接受客户端连接");new ClientThread(connection).start();}} catch (Exception e) {e.printStackTrace();}}/*** 开启一个线程专门从与客户端蓝牙设备中读取文件数据,并把文件数据存储到本地。* * @author fly**/private class ClientThread extends Thread {private StreamConnection mStreamConnection = null;public ClientThread(StreamConnection sc) {mStreamConnection = sc;}@Overridepublic void run() {try {BufferedInputStream bis = new BufferedInputStream(mStreamConnection.openInputStream());// 本地创建一个image.jpg文件接收来自于手机客户端发来的图片文件数据。FileOutputStream fos = new FileOutputStream("image.jpg");int c = 0;byte[] buffer = new byte[1024];System.out.println("开始读数据...");while (true) {c = bis.read(buffer);if (c == -1) {System.out.println("读取数据结束");break;} else {fos.write(buffer, 0, c);}}fos.flush();fos.close();bis.close();mStreamConnection.close();} catch (Exception e) {e.printStackTrace();}}}
}

Android手机端的蓝牙客户端BluetoothClientActivity:

package zhangphil.test;import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Set;
import java.util.UUID;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;public class BluetoothClientActivity extends AppCompatActivity {private BluetoothAdapter mBluetoothAdapter;//要连接的目标蓝牙设备(Windows PC电脑的名字)。private final String TARGET_DEVICE_NAME = "PHIL-PC";private final String TAG = "蓝牙调试";//UUID必须是Android蓝牙客户端和Windows PC电脑端一致。private final String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB";// 通过广播接收系统发送出来的蓝牙设备发现通知。private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {@Overridepublic 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;}@Overridepublic 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 {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");return file;}/*** 获得和当前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;}@Overrideprotected 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()) {Log.d(TAG, "搜索蓝牙设备...");}} else {new ClientThread(device).start();}}@Overrideprotected void onDestroy() {super.onDestroy();unregisterReceiver(mBroadcastReceiver);}
}

本例出于简单演示功能的目的,省去了Android客户端选择文件的代码实现,事先作为测试文件,先在Android手机的存储器根目录放置一张名为image.jpg的图片。
务必保证Windows电脑和Android手机上的蓝牙均处于打开状态。然后先启动服务器端程序,再启动Android手机客户端程序。
Windows电脑端输出:

Android studio的logcat输出:

Android通过Bluetooth蓝牙发送手机照片文件到Windows PC:Java实现相关推荐

  1. airdrop 是 蓝牙吗_您可以在Windows PC或Android手机上使用AirDrop吗?

    airdrop 是 蓝牙吗 Aleksey Khilko/Shutterstock.comAleksey Khilko / Shutterstock.com Apple's AirDrop is a ...

  2. app读写照片和文件_App 偷看手机照片文件 25000 次,你要干什么?

    现在的手机 App,真是越来越过分! 在昨天,央视新闻给我们揭露了手机 App"偷窥"乱象,不仅仅多款 App 遭受点名,并且 App 运营商们都在甩锅! 新闻中报道,大学生小刘把 ...

  3. android私密照片恢复,安卓手机照片被误删?这样设置一下,立马就能恢复

    原标题:安卓手机照片被误删?这样设置一下,立马就能恢复 今日分享:手机照片恢复 适用系统:安卓 手机作为我们日常生活中经常使用工具,多多少少保存一些特别重要.有意义的照片,藏着很多"美好&q ...

  4. 手机照片文件删除怎么恢复?三种恢复的方法

    不知道大家在使用手机的过程中是否遇到过这种情况,就是手机照片被误删了.如果我们想要恢复误删的手机照片该怎么办呢?不用着急,接下来就给大家分享三种恢复手机照片的方法. 最近删除里面找回 大部分手机都有最 ...

  5. Android 调用系统蓝牙发送文件

    调用原生系统的文件分享中的蓝牙分享功能 //调用android分享窗口Intent intent2 = new Intent(Intent.ACTION_SEND);intent2.setType(& ...

  6. android+蓝牙传输文件,在Android中使用蓝牙的消息和文件传输

    我正在开发一个应用程序,首先我们必须搜索和连接可用的配对蓝牙设备.我做到了连接.但之后我放了一个屏幕要求在文本和文件传输之间进行选择.当我选择文本时,将打开另一个屏幕,其中有edittext和按钮.无 ...

  7. Android BLE低功耗蓝牙重启手机后自动连接失败问题

    最近在做安卓开发,用到蓝牙模块相关功能.主要功能是使用手机连上低功耗蓝牙设备,比如蓝牙手环.关于如何蓝牙连接在这里就不讲述了,网上搜索一大堆相关教程.想要来这里看蓝牙连接方式的朋友可能要大失所望了. ...

  8. Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用...

    近来由于项目需要,需要用到手机短信验证码的功能,其中最主要的是用到了第三方提供的短信平台接口WebService客户端接口,下面我把我在项目中用到的记录一下,以便给大家提供个思路,由于本人的文采有限, ...

  9. 一次性从linux发送多个文件到windows

    最近经常从linux服务器迁移数据,迁移方式是在mysql执行脚本,生成多个数据文本到某个文件夹下面,由于改文件夹包含了其他项目的数据文本,而且量比较大,如果整个文件打包发送,一来耗时间,而来也没必要 ...

最新文章

  1. Zookeeper 安装和配置
  2. [转]xml解析工具的效率比较QDomDocument、TinyXml-2、RapidXml、PugiXml
  3. 在Android开发中怎样调用系统Email发送邮件
  4. 如何检查文件是否是python中的目录或常规文件? [重复]
  5. 把光盘转化成镜像文件
  6. matlab文件序号超出511,求教一段matlab的代码 - 数学 - 小木虫 - 学术 科研 互动社区...
  7. 开博第一篇,附上我开通博客的理由
  8. PHP动态属性和stdclass
  9. Atitit.java线程池使用总结attilax 1.1. 动态更改线程数量 1 1.2. code 1 三、线程池的原理 其实线程池的原理很简单,类似于操作系统中的缓冲区的概念,它的流程如下
  10. 揭秘井井有条的流水线(ZooKeeper 原理篇)
  11. 淘晶驰串口屏下载工程慢怎么办
  12. smarty下载及入门教程
  13. php毕业设计商城模板,基于Thinkphp的毕业设计网上购物商城
  14. ssm共享充电宝管理系统计算机毕业设计
  15. C编译报错: implicit declaration of function xxx is invalid in C99 [-Wimplicit-function-declaration]
  16. 云服务器无法远程连接常见原因如下:
  17. IDEA中新建Module
  18. Dubbox 基本特性之结果缓存
  19. 解决内联汇编64位Linux系统调用提示Bad Address
  20. ubuntu12.04安装极点五笔

热门文章

  1. 阿里开发手册规范(JAVA)
  2. ES Curator的使用及其配置
  3. 海迅咨询推荐的读书单
  4. DO-178B三种文本对照学习注记
  5. execute immediate用法
  6. 期权一张是多少?一张有多少份?
  7. [转] ubuntu 一些常用软件的安装
  8. 驾驶员行为检测+疲劳驾驶
  9. APP动效设计需要遵守哪些原则?
  10. 云计算导论(第2版)课后题答案