蓝牙自动收发文件
自动接收:
package com.xuben.testBlueTooth;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List; //import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;
public class AutoreceiveBTActivity extends Activity {
static final String targetAddr = "FE:12:78:07:DA:00";
/**
* This transfer is confirmed by user.
*/
public static final int USER_CONFIRMATION_CONFIRMED = 1;
public static final String USER_CONFIRMATION = "confirm";
ArrayAdapter<String> adtDevices;
List<String> lstDevices = new ArrayList<String>();
BluetoothAdapter btAdapt;
public static BluetoothSocket btSocket;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 初始化本机蓝牙功能
btAdapt = BluetoothAdapter.getDefaultAdapter();
/*
* 自动接收文件
*/
BluetoothDevice btDev = btAdapt.getRemoteDevice(targetAddr);
try {
Method m = null;
try {
m = btDev.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
btSocket = (BluetoothSocket) m.invoke(btDev, 1);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//发送蓝牙配对
btSocket.connect();
/*
* 发送一指定的文件到其它蓝牙设备
*/
ContentValues cv = new ContentValues();
cv.put(USER_CONFIRMATION,
USER_CONFIRMATION_CONFIRMED);
getContentResolver().update(
Uri.parse("content://com.android.bluetooth.opp/btopp"),
cv, null, null);
btSocket.close();
Toast.makeText(this, getString(R.string.bt_toast), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
自动发送:
BluetoothDevice btDev = btAdapt.getRemoteDevice(targetAddr);
try {
Method m = null;
try {
m = btDev.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
btSocket = (BluetoothSocket) m.invoke(btDev, 1);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//发送蓝牙配对
btSocket.connect();
/*
* 发送一指定的文件到其它蓝牙设备
*/
ContentValues cv = new ContentValues();
cv.put("uri", FilePath);
cv.put("destination", targetAddr);
cv.put("direction", 0);
Long ts = System.currentTimeMillis();
cv.put("timestamp", ts);
getContentResolver().insert(
Uri.parse("content://com.android.bluetooth.opp/btopp"),
cv);
btSocket.close();
} catch (IOException e) {
e.printStackTrace();
}