• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
笨笨笨笨
博客园    首页    新随笔    联系   管理    订阅  订阅

蓝牙自动收发文件

自动接收:

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();
}


 

posted @ 2011-12-14 18:32  笨笨笨笨  阅读(1744)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3