Android通過Bluetooth藍(lán)牙發(fā)送手機(jī)照片文件到Windows PC:Java實現(xiàn)
本文在《Android通過藍(lán)牙發(fā)送數(shù)據(jù)到Windows PC電腦:Java實現(xiàn)(鏈接地址:https://blog.csdn.net/zhangphil/article/details/83146705 )》基礎(chǔ)上改進(jìn)代碼,還是用Java實現(xiàn)把Android手機(jī)上的一張照片,通過Bluetooth藍(lán)牙連接,發(fā)送到Windows PC電腦上,實現(xiàn)上仍區(qū)分為Windows電腦端的藍(lán)牙服務(wù)器端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是藍(lán)牙的服務(wù)器端。部署在Windows操作系統(tǒng)(PC電腦上)。 等待手機(jī)客戶端或者其他藍(lán)牙設(shè)備的連接。* * @author fly**/
public class BluetoothJavaServer {// 藍(lán)牙服務(wù)器端的UUID必須和手機(jī)端的UUID一致。// 手機(jī)端的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("服務(wù)器端開始監(jiān)聽客戶端連接請求...");while (true) {StreamConnection connection = mStreamConnectionNotifier.acceptAndOpen();System.out.println("接受客戶端連接");new ClientThread(connection).start();}} catch (Exception e) {e.printStackTrace();}}/*** 開啟一個線程專門從與客戶端藍(lán)牙設(shè)備中讀取文件數(shù)據(jù),并把文件數(shù)據(jù)存儲到本地。* * @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());// 本地創(chuàng)建一個image.jpg文件接收來自于手機(jī)客戶端發(fā)來的圖片文件數(shù)據(jù)。FileOutputStream fos = new FileOutputStream("image.jpg");int c = 0;byte[] buffer = new byte[1024];System.out.println("開始讀數(shù)據(jù)...");while (true) {c = bis.read(buffer);if (c == -1) {System.out.println("讀取數(shù)據(jù)結(jié)束");break;} else {fos.write(buffer, 0, c);}}fos.flush();fos.close();bis.close();mStreamConnection.close();} catch (Exception e) {e.printStackTrace();}}}
}
Android手機(jī)端的藍(lán)牙客戶端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;//要連接的目標(biāo)藍(lán)牙設(shè)備(Windows PC電腦的名字)。private final String TARGET_DEVICE_NAME = "PHIL-PC";private final String TAG = "藍(lán)牙調(diào)試";//UUID必須是Android藍(lán)牙客戶端和Windows PC電腦端一致。private final String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB";// 通過廣播接收系統(tǒng)發(fā)送出來的藍(lán)牙設(shè)備發(fā)現(xiàn)通知。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, "發(fā)現(xiàn)藍(lán)牙設(shè)備:" + name);if (name != null && name.equals("PHIL-PC")) {Log.d(TAG, "發(fā)現(xiàn)目標(biāo)藍(lán)牙設(shè)備,開始線程連接");new Thread(new ClientThread(device)).start();// 藍(lán)牙搜索是非常消耗系統(tǒng)資源開銷的過程,一旦發(fā)現(xiàn)了目標(biāo)感興趣的設(shè)備,可以關(guān)閉掃描。mBluetoothAdapter.cancelDiscovery();}}}};/*** 該線程往藍(lán)牙服務(wù)器端發(fā)送文件數(shù)據(jù)。*/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, "連接藍(lán)牙服務(wù)端...");socket.connect();Log.d(TAG, "連接建立.");// 開始往服務(wù)器端發(fā)送數(shù)據(jù)。Log.d(TAG, "開始往藍(lán)牙服務(wù)器發(fā)送數(shù)據(jù)...");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, "讀取結(jié)束");break;} else {bos.write(buffer, 0, c);}}bos.flush();fis.close();bos.close();Log.d(TAG, "發(fā)送文件成功");} catch (Exception e) {e.printStackTrace();}}}/*** 發(fā)給給藍(lán)牙服務(wù)器的文件。* 本例發(fā)送一張位于存儲器根目錄下名為 image.jpg 的照片。** @return*/private File getFile() {File root = Environment.getExternalStorageDirectory();File file = new File(root, "image.jpg");return file;}/*** 獲得和當(dāng)前Android藍(lán)牙已經(jīng)配對的藍(lán)牙設(shè)備。** @return*/private BluetoothDevice getPairedDevices() {Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();if (pairedDevices != null && pairedDevices.size() > 0) {for (BluetoothDevice device : pairedDevices) {// 把已經(jīng)取得配對的藍(lán)牙設(shè)備名字和地址打印出來。Log.d(TAG, device.getName() + " : " + device.getAddress());//如果已經(jīng)發(fā)現(xiàn)目標(biāo)藍(lán)牙設(shè)備和Android藍(lán)牙已經(jīng)配對,則直接返回。if (TextUtils.equals(TARGET_DEVICE_NAME, device.getName())) {Log.d(TAG, "已配對目標(biāo)設(shè)備 -> " + TARGET_DEVICE_NAME);return device;}}}return null;}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();BluetoothDevice device = getPairedDevices();if (device == null) {// 注冊廣播接收器。// 接收系統(tǒng)發(fā)送的藍(lán)牙發(fā)現(xiàn)通知事件。IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);registerReceiver(mBroadcastReceiver, filter);if (mBluetoothAdapter.startDiscovery()) {Log.d(TAG, "搜索藍(lán)牙設(shè)備...");}} else {new ClientThread(device).start();}}@Overrideprotected void onDestroy() {super.onDestroy();unregisterReceiver(mBroadcastReceiver);}
}
本例出于簡單演示功能的目的,省去了Android客戶端選擇文件的代碼實現(xiàn),事先作為測試文件,先在Android手機(jī)的存儲器根目錄放置一張名為image.jpg的圖片。
務(wù)必保證Windows電腦和Android手機(jī)上的藍(lán)牙均處于打開狀態(tài)。然后先啟動服務(wù)器端程序,再啟動Android手機(jī)客戶端程序。
Windows電腦端輸出:
Android studio的logcat輸出:
總結(jié)
以上是生活随笔為你收集整理的Android通过Bluetooth蓝牙发送手机照片文件到Windows PC:Java实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。