蓝牙自动收发文件

自动接收:

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


 

转载于:https://www.cnblogs.com/xubensave/archive/2011/12/14/2430309.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值