package com.ds.bluetooth;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import android.app.Activity;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import com.ds.bluetoothUtil.BluetoothCommunSocket;
import com.ds.bluetoothUtil.BluetoothServerConnThread;
import com.ds.bluetoothUtil.BluetoothServerService;
import com.ds.bluetoothUtil.BluetoothTools;
import com.ds.bluetoothUtil.TransmitBean;
public class ServerActivity extends Activity {
private TextView serverStateTextView;
private EditText msgEditText;
private EditText sendMsgEditText;
private Button sendBtn;
private ProgressDialog mpDialog;
private ListView bondDevicesListView;
private ArrayList<BluetoothDevice> bondDevices=new ArrayList<BluetoothDevice>(); // 用于存放已配对蓝牙设备
private BluetoothAdapter bluetooth=BluetoothAdapter.getDefaultAdapter();
//服务端连接线程
private BluetoothServerConnThread connThread;
//蓝牙通讯
private BluetoothCommunSocket communSocket;
public BluetoothSocket socket; //通信Socket
@Override
protected void onStart() {
//开启后台service
// Intent startService = new Intent(ServerActivity.this, BluetoothServerService.class);
// startService(startService);
//注册BoradcasrReceiver
// IntentFilter intentFilter = new IntentFilter();
// intentFilter.addAction(BluetoothTools.ACTION_DATA_TO_GAME);
// intentFilter.addAction(BluetoothTools.ACTION_CONNECT_SUCCESS);
// intentFilter.addAction(BluetoothTools.ACTION_CONNECT_ERROR);
// intentFilter.addAction(BluetoothTools.ACTION_FILE_SEND_PERCENT);
// intentFilter.addAction(BluetoothTools.ACTION_FILE_RECIVE_PERCENT);
// registerReceiver(broadcastReceiver, intentFilter);
super.onStart();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server);
bondDevicesListView = (ListView) this.findViewById(R.id.bondDevices);
serverStateTextView=(TextView) this.findViewById(R.id.serverStateText);
msgEditText = (EditText)findViewById(R.id.serverEditText);
addBondDevicesToListView();
mpDialog=new ProgressDialog(ServerActivity.this);
mpDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mpDialog.setTitle("提示");
mpDialog.setIcon(R.drawable.icon);
mpDialog.setIndeterminate(false);
mpDialog.setCancelable(true); // 设置是否可以通过点击Back键取消
mpDialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
mpDialog.setMax(100);
// mpDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
// new DialogInterface.OnClickListener() {
//
// @Override
// public void onClick(DialogInterface dialog, int which) {
// // TODO Auto-generated method stub
//
// }
// });
// mpDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
// new DialogInterface.OnClickListener() {
//
// @Override
// public void onClick(DialogInterface dialog, int which) {
// // TODO Auto-generated method stub
//
// }
// });
// IntentFilter controlFilter = new IntentFilter();
// controlFilter.addAction(BluetoothTools.ACTION_STOP_SERVICE);
// controlFilter.addAction(BluetoothTools.ACTION_DATA_TO_SERVICE);
// controlFilter.addAction(BluetoothTools.ACTION_SELECTED_DEVICE);
// registerReceiver(controlReceiver, controlFilter);
bluetooth.enable(); //打开蓝牙
//开启蓝牙发现功能(300秒)
Intent discoveryIntent = new Intent(bluetooth.ACTION_REQUEST_DISCOVERABLE);
discoveryIntent.putExtra(bluetooth.EXTRA_DISCOVERABLE_DURATION, 300);
discoveryIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//提示信息
startActivity(discoveryIntent);
//开启连接线程
connThread=new BluetoothServerConnThread(serviceHandler);
connThread.start();
}
/**
* 添加已绑定蓝牙设备到ListView
*/
public void addBondDevicesToListView() {
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
int count = bondDevices.size();
if(count==0){//刚打开ACTIVITY 尚未开始搜索
//获得已配对的远程蓝牙设备的集合
Set<BluetoothDevice> devices = bluetooth.getBondedDevices();
if(devices.size()>0){
for(Iterator<BluetoothDevice> it = devices.iterator();it.hasNext();){
BluetoothDevice device = (BluetoothDevice)it.next();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("deviceName", device.getName()+ "|" +device.getAddress());
bondDevices.add(device);
data.add(map);// 把item项的数据加到data中
}
}
}
String[] from = { "deviceName" };
int[] to = { R.id.device_name };
SimpleAdapter simpleAdapter = new SimpleAdapter(ServerActivity.this, data,R.layout.bonddevice_item, from, to);
// 把适配器装载到listView中
this.bondDevicesListView.setAdapter(simpleAdapter);
this.bondDevicesListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
BluetoothDevice device = bondDevices.get(arg2);
Intent intent = new Intent();
Intent selectDeviceIntent = new Intent(BluetoothTools.ACTION_SELECTED_DEVICE);
selectDeviceIntent.putExtra(BluetoothTools.DEVICE, device);
ServerActivity.this.sendBroadcast(selectDeviceIntent);
intent.setClassName(ServerActivity.this,"com.ds.bluetooth.ServerActivity1");
intent.putExtra("deviceAddress", device.getAddress());
// unregisterReceiver(broadcastReceiver);
ServerActivity.this.startActivity(intent);
}
});
}
//接收其他线程消息的Handler
private Handler serviceHandler = new Handler() {
@Override
public void handleMessage( Message msg) {
switch (msg.what) {
case BluetoothTools.MESSAGE_CONNECT_SUCCESS:
// //发送连接成功消息
// Intent connSuccIntent = new Intent(BluetoothTools.ACTION_CONNECT_SUCCESS);
// sendBroadcast(connSuccIntent);
break;
case BluetoothTools.MESSAGE_CONNECT_ERROR:
//发送连接错误广播
mpDialog.dismiss();
Toast.makeText(ServerActivity.this, "通讯失败", 2000).show();
break;
case BluetoothTools.MESSAGE_READ_OBJECT:
//读取到数据
TransmitBean transmit = (TransmitBean)msg.obj;
String text="";
if(transmit.getFilename()!=null&&!"".equals(transmit.getFilename())){
text = "receive file from remote " + new Date().toLocaleString() + " :\r\n" + transmit.getFilename() + "\r\n";
}else{
没有合适的资源?快使用搜索试试~ 我知道了~
Android Bluetooth文件传输
共108个文件
class:60个
java:23个
xml:14个
5星 · 超过95%的资源 需积分: 47 383 下载量 32 浏览量
2015-07-16
22:30:09
上传
评论 10
收藏 150KB ZIP 举报
温馨提示
Android Bluetooth文件的引入和传输,可使用两台设备,一个做客户端一个做服务端,传输文件,显示传送进度。
资源推荐
资源详情
资源评论
收起资源包目录
Android Bluetooth文件传输 (108个子文件)
proguard.cfg 1KB
BluetoothCommunThreads.class 7KB
BluetoothCommunSocket.class 7KB
ServerActivity.class 6KB
ClientActivity1.class 6KB
ClientActivity.class 5KB
ClientListListener.class 5KB
SelectFileActivity.class 4KB
ServerActivity1.class 4KB
ServerActivity1$1.class 4KB
BluetoothClientService$1.class 4KB
ClientActivity2.class 4KB
BluetoothServerService$1.class 4KB
BluetoothClientService.class 3KB
ClientActivity2$1.class 3KB
BluetoothServerService.class 3KB
AdapterManager.class 3KB
ClientActivity1$1.class 3KB
ClientActivity$1.class 3KB
BluetoothServerConnThread.class 3KB
ServerActivity$1.class 3KB
BluetoothTools.class 3KB
wanquantestBluetoothServerConnThread.class 2KB
FileListAdapter.class 2KB
BluetoothClientConnThread.class 2KB
ClientListListener$1.class 2KB
ClientListListener$3.class 2KB
ClientListListener$2.class 2KB
SelectFileActivity$1.class 2KB
ServerActivity$2.class 2KB
ServerActivity1$4.class 2KB
ClientActivity2$4.class 2KB
wanquantestBluetoothServerConnThread$AcceptThread.class 2KB
ClientActivity2$3.class 2KB
ServerActivity1$3.class 2KB
TransmitBean.class 2KB
wanquantestBluetoothServerConnThread$ConnectedThread.class 2KB
BluetoothServerService$2.class 2KB
FileUtil.class 2KB
BluetoothClientService$2.class 2KB
ClientActivity$2.class 1KB
SelectFileActivity$3.class 1KB
ClientActivity1$2.class 1KB
R$id.class 1KB
BluetoothServerService$1$1MyRunnable.class 1KB
BluetoothClientService$1$1MyRunnable.class 1KB
SelectFileActivity$2.class 1KB
SelectFileActivity$5.class 1KB
MainActivity.class 1KB
MainActivity$ButtonClickListener.class 1KB
ServerActivity1$2.class 1KB
ClientActivity2$2.class 1KB
SelectFileActivity$4.class 800B
AdapterManager$1.class 792B
R$layout.class 700B
R$string.class 611B
R$drawable.class 527B
R.class 517B
MyFile.class 371B
BuildConfig.class 341B
R$attr.class 334B
.classpath 475B
ServerActivity.java 9KB
ClientActivity2.java 8KB
BluetoothCommunThreads.java 8KB
ServerActivity1.java 8KB
BluetoothCommunSocket.java 7KB
ClientActivity1.java 7KB
BluetoothClientService.java 7KB
ClientListListener.java 7KB
ClientActivity.java 7KB
SelectFileActivity.java 5KB
BluetoothServerService.java 5KB
wanquantestBluetoothServerConnThread.java 5KB
BluetoothTools.java 4KB
R.java 3KB
BluetoothServerConnThread.java 3KB
AdapterManager.java 2KB
MainActivity.java 2KB
FileListAdapter.java 2KB
BluetoothClientConnThread.java 2KB
TransmitBean.java 2KB
FileUtil.java 753B
MyFile.java 208B
BuildConfig.java 158B
icon.png 4KB
icon.png 3KB
icon.png 2KB
ic_launcher.png 2KB
icon_file.png 2KB
icon_folder.png 939B
org.eclipse.jdt.core.prefs 177B
.project 845B
project.properties 563B
bluetooth.xml 3KB
client2.xml 3KB
client.xml 2KB
select_file.xml 2KB
AndroidManifest.xml 2KB
AndroidManifest.xml 2KB
共 108 条
- 1
- 2
谢谢2013
- 粉丝: 88
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页