package com.gch.BTSpectrumForm;
/*************************************************************/
/* Project Shmimn
* Mobile Health-care Device
* Gong Chunhui
* 2011-4-24 16:35:21
*/
/*************************************************************/
import java.io.IOException;
import java.util.UUID;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.ToggleButton;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
public class BTConnectActivity extends Activity
{
/** Called when the activity is first created. */
public static final int REQUEST_ENABLE_BT = 8807;
public BroadcastReceiver mBTReceiver;
public static BluetoothSocket mBTSocket;
public BluetoothAdapter mBTAdapter;
private Button btnSearchDevices;
private ToggleButton tBtnBTSwitch;
private BluetoothDevice mBTDevice;
private ArrayAdapter<String> adtDvcs;
private List<String> lstDvcsStr = new ArrayList<String>();
private ListView lvDevicesList;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.btconnect);
// Init Bluetooth Adapter
mBTAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBTAdapter == null)
{
Toast.makeText(BTConnectActivity.this, " No devices supporting Bluetooth! ", Toast.LENGTH_SHORT).show();
this.finish();
}
// Set up BroadCast Receiver
mBTReceiver = new BroadcastReceiver()
{
public void onReceive(Context context,Intent intent)
{
String act = intent.getAction();
// To see whether the action is that already found devices
if(act.equals(BluetoothDevice.ACTION_FOUND))
{
// If found one device, get the device object
BluetoothDevice tmpDvc = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Put the name & address into a string
String tmpDvcStr = tmpDvc.getName()+"|"+tmpDvc.getAddress();
if (lstDvcsStr.indexOf(tmpDvcStr)==-1)
{
// Avoid duplicate add devices
lstDvcsStr.add(tmpDvcStr);
adtDvcs.notifyDataSetChanged();
Toast.makeText(BTConnectActivity.this, "Find a new device!", Toast.LENGTH_SHORT).show();
}
}
if(act.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED))
{
Toast.makeText(BTConnectActivity.this, " Searching Complete!", Toast.LENGTH_SHORT).show();
}
if (act.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED))
{
Toast.makeText(BTConnectActivity.this, "Begin Searching Devices", Toast.LENGTH_SHORT).show();
}
}
};
//Register the broadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBTReceiver,filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
registerReceiver(mBTReceiver,filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mBTReceiver,filter);
// Init Buttons
btnSearchDevices = (Button)findViewById(R.id.btnSearchDevice);
tBtnBTSwitch = (ToggleButton)findViewById(R.id.tBtnBTSwitch);
lvDevicesList = (ListView)findViewById(R.id.lvDevicesList);
if(mBTAdapter.getState()==BluetoothAdapter.STATE_OFF)
tBtnBTSwitch.setChecked(false);
if(mBTAdapter.getState()==BluetoothAdapter.STATE_ON)
tBtnBTSwitch.setChecked(true);
// ListView And Data Adapter
adtDvcs = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,lstDvcsStr);
lvDevicesList.setAdapter(adtDvcs);
// Add Click Listeners to Buttons
tBtnBTSwitch.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
// Open BT
if(tBtnBTSwitch.isChecked())
{
if(!mBTAdapter.isEnabled())
{
//Open a new dialog to ask user whether wanna open BT
Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler,REQUEST_ENABLE_BT);
}
tBtnBTSwitch.setChecked(true);
}
// Close BT
if(!tBtnBTSwitch.isChecked())
{
if(mBTAdapter.isEnabled())
{
mBTAdapter.disable();
Toast.makeText(BTConnectActivity.this, "BT has been closed!", Toast.LENGTH_SHORT).show();
}
tBtnBTSwitch.setChecked(false);
}
}
});
// Search Devices
btnSearchDevices.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
if (mBTAdapter.isDiscovering())
{
Toast.makeText(BTConnectActivity.this, "Already Searching", Toast.LENGTH_SHORT).show();
}
else{
lstDvcsStr.clear();
adtDvcs.notifyDataSetChanged();
mBTDevice = null;
mBTAdapter.startDiscovery();
}
}
});
// Create Click listener for the items on devices list
lvDevicesList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
if (mBTAdapter == null)
Toast.makeText(BTConnectActivity.this, "No devices support BlueTooth", Toast.LENGTH_SHORT).show();
else
{
// stop searching
mBTAdapter.cancelDiscovery();
// Get address of remote device
String str = lstDvcsStr.get(arg2);
String[] dvcValues = str.split("\\|");
String dvcAddr = dvcValues[1];
//UUID dvcUUID = UUID.randomUUID();
//00001101-0000-1000-8000-00805F9B34FB SPP protocal
UUID dvcUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
// Set BT device
mBTDevice = mBTAdapter.getRemoteDevice(dvcAddr);
// Connect Device
try
{
mBTSocket = mBTDevice.createRfcommSocketToServiceRecord(dvcUUID);
mBTSocket.connect();
Intent mInt = new Intent(BTConnectActivity.this,RTMonitorActivity.class);
startActivity(mInt);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
});
}
public void onActivityResult(int RequestCode, int ResultCode,Intent data){
switch(RequestCode)
{
case REQUEST_ENABLE_BT:
if(ResultCode == RESULT_OK)
{
Toast.makeText(this.getApplicationContext(), "BT Launched!", Toast.LENGTH_SHORT).show();
}
else
if(ResultCode == RESULT_CANCELED)
{
Toast.makeText(this.getApplicationContext(), "Launched BT cancled!", Toast.LENGTH_SHORT).show();
}
break;
}
}
@Override
protected void onDestroy()
{
this.unregisterReceiver(mBTReceiver);
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android 蓝牙发现设备,建立连接,显示谱线
共32个文件
class:11个
png:6个
java:4个
4星 · 超过85%的资源 需积分: 10 198 下载量 132 浏览量
2012-12-11
09:36:14
上传
评论 2
收藏 77KB RAR 举报
温馨提示
Android 蓝牙发现设备,建立连接,显示谱线 Android 蓝牙发现设备,建立连接,
资源推荐
资源详情
资源评论
收起资源包目录
Android 蓝牙发现设备,建立连接,显示谱线.rar (32个子文件)
BTSpectrumForm(3.29...)
bin
classes.dex 16KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
classes
com
gch
BTSpectrumForm
R$layout.class 1KB
R$id.class 1KB
R$drawable.class 1KB
RTMonitorActivity$BTReadThread.class 6KB
BTConnectActivity.class 8KB
R$attr.class 1KB
R.class 1KB
RTMonitorActivity.class 7KB
DrawSpectrumForm.class 4KB
RTMonitorActivity$MsgHandler.class 6KB
R$string.class 1KB
resources.ap_ 12KB
BTSpectrumForm.apk 22KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
values
strings.xml 405B
drawable-mdpi
ic_launcher.png 3KB
layout
rtmonitor.xml 2KB
btconnect.xml 1KB
assets
gen
com
gch
BTSpectrumForm
R.java 2KB
proguard.cfg 1KB
src
com
gch
BTSpectrumForm
DrawSpectrumForm.java 3KB
BTConnectActivity.java 8KB
RTMonitorActivity.java 6KB
.project 850B
.classpath 364B
project.properties 360B
AndroidManifest.xml 986B
共 32 条
- 1
lvp_mobile
- 粉丝: 5
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页