package com.NewApp;
import android.app.Activity;
import android.os.Bundle;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;
import android.R.*;
import android.app.Activity;
import android.bluetooth.*;
import android.content.BroadcastReceiver;
import android.content.Context;
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.view.View.OnClickListener;
import android.widget.*;
import zephyr.android.BioHarnessBT.*;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
BluetoothAdapter adapter = null;
BTClient _bt;
ZephyrProtocol _protocol;
NewConnectedListener _NConnListener;
private final int HEART_RATE = 0x100;
private final int RESPIRATION_RATE = 0x101;
private final int SKIN_TEMPERATURE = 0x102;
private final int POSTURE = 0x103;
private final int PEAK_ACCLERATION = 0x104;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*Sending a message to android that we are going to initiate a pairing request*/
IntentFilter filter = new IntentFilter("android.bluetooth.device.action.PAIRING_REQUEST");
/*Registering a new BTBroadcast receiver from the Main Activity context with pairing request event*/
this.getApplicationContext().registerReceiver(new BTBroadcastReceiver(), filter);
// Registering the BTBondReceiver in the application that the status of the receiver has changed to Paired
IntentFilter filter2 = new IntentFilter("android.bluetooth.device.action.BOND_STATE_CHANGED");
this.getApplicationContext().registerReceiver(new BTBondReceiver(), filter2);
//Obtaining the handle to act on the CONNECT button
TextView tv = (TextView) findViewById(R.id.labelStatusMsg);
String ErrorText = "Not Connected to BioHarness !";
tv.setText(ErrorText);
Button btnConnect = (Button) findViewById(R.id.ButtonConnect);
if (btnConnect != null)
{
btnConnect.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String BhMacID = "00:07:80:9D:8A:E8";
//String BhMacID = "00:07:80:88:F6:BF";
adapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = adapter.getBondedDevices();
if (pairedDevices.size() > 0)
{
for (BluetoothDevice device : pairedDevices)
{
if (device.getName().startsWith("BH"))
{
BluetoothDevice btDevice = device;
BhMacID = btDevice.getAddress();
break;
}
}
}
//BhMacID = btDevice.getAddress();
BluetoothDevice Device = adapter.getRemoteDevice(BhMacID);
String DeviceName = Device.getName();
_bt = new BTClient(adapter, BhMacID);
_NConnListener = new NewConnectedListener(Newhandler,Newhandler);
_bt.addConnectedEventListener(_NConnListener);
TextView tv1 = (EditText)findViewById(R.id.labelHeartRate);
tv1.setText("000");
tv1 = (EditText)findViewById(R.id.labelRespRate);
tv1.setText("0.0");
tv1 = (EditText)findViewById(R.id.labelSkinTemp);
tv1.setText("0.0");
tv1 = (EditText)findViewById(R.id.labelPosture);
tv1.setText("000");
tv1 = (EditText)findViewById(R.id.labelPeakAcc);
tv1.setText("0.0");
if(_bt.IsConnected())
{
_bt.start();
TextView tv = (TextView) findViewById(R.id.labelStatusMsg);
String ErrorText = "Connected to BioHarness "+DeviceName;
tv.setText(ErrorText);
//Reset all the values to 0s
}
else
{
TextView tv = (TextView) findViewById(R.id.labelStatusMsg);
String ErrorText = "Unable to Connect !";
tv.setText(ErrorText);
}
}
});
}
/*Obtaining the handle to act on the DISCONNECT button*/
Button btnDisconnect = (Button) findViewById(R.id.ButtonDisconnect);
if (btnDisconnect != null)
{
btnDisconnect.setOnClickListener(new OnClickListener() {
@Override
/*Functionality to act if the button DISCONNECT is touched*/
public void onClick(View v) {
// TODO Auto-generated method stub
/*Reset the global variables*/
TextView tv = (TextView) findViewById(R.id.labelStatusMsg);
String ErrorText = "Disconnected from BioHarness!";
tv.setText(ErrorText);
/*This disconnects listener from acting on received messages*/
_bt.removeConnectedEventListener(_NConnListener);
/*Close the communication with the device & throw an exception if failure*/
_bt.Close();
}
});
}
}
private class BTBondReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
BluetoothDevice device = adapter.getRemoteDevice(b.get("android.bluetooth.device.extra.DEVICE").toString());
Log.d("Bond state", "BOND_STATED = " + device.getBondState());
}
}
private class BTBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("BTIntent", intent.getAction());
Bundle b = intent.getExtras();
Log.d("BTIntent", b.get("android.bluetooth.device.extra.DEVICE").toString());
Log.d("BTIntent", b.get("android.bluetooth.device.extra.PAIRING_VARIANT").toString());
try {
BluetoothDevice device = adapter.getRemoteDevice(b.get("android.bluetooth.device.extra.DEVICE").toString());
Method m = BluetoothDevice.class.getMethod("convertPinToBytes", new Class[] {String.class} );
byte[] pin = (byte[])m.invoke(device, "1234");
m = device.getClass().getMethod("setPin", new Class [] {pin.getClass()});
Object result = m.invoke(device, pin);
Log.d("BTTest", result.toString());
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NoSuchMethodException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
final Handler Newhandler = new Handler(){
public void handleMessage(Message msg)
{
TextView tv;
switch (msg.what)
{
case HEART_RATE:
String HeartRatetext = msg.getData().getString("HeartRate");
tv = (EditText)findViewById(R.id.labelHeartRate);
System.out.println("Heart Rate Info is "+ HeartRatetext);
if (tv != null)tv.setText(HeartRatetext);
break;
case RESPIRATION_RATE:
String RespirationRatetext = msg.getData().getString("RespirationRate");
tv = (EditText)findViewById(R.id.labelRespRate);
if (tv != null)tv.setText(RespirationRatetext);
break;
case SKIN_TEMPERATURE:
String SkinTemperaturetext = msg.getData().getString("SkinTemperature");
tv = (EditText)findViewById(R.id.labelSkinTemp);
if (tv != null)
没有合适的资源?快使用搜索试试~ 我知道了~
BioHarness BT Example Android Project
共92个文件
class:68个
png:6个
java:4个
需积分: 9 5 下载量 99 浏览量
2013-01-02
11:26:32
上传
评论
收藏 285KB ZIP 举报
温馨提示
这些代码是我们项目购买zephyr传感器做项目时带的源码包含蓝牙通信,需要的话可以看看
资源推荐
资源详情
资源评论
收起资源包目录
BioHarness BT Example Android Project.zip (92个子文件)
BioHarness BT Example Android Project
bin
classes.dex 70KB
res
drawable-ldpi
icon1.png 2KB
drawable-hdpi
icon1.png 4KB
drawable-mdpi
icon1.png 2KB
NewProjBioHarness.apk 39KB
BioHarness BT Example Android Project.apk 39KB
com
NewApp
MainActivity.class 3KB
MainActivity$3.class 1KB
MainActivity$2.class 3KB
MainActivity$1.class 2KB
R$layout.class 370B
MainActivity$BTBroadcastReceiver.class 3KB
R$id.class 874B
R$drawable.class 377B
MainActivity$BTBondReceiver.class 2KB
NewConnectedListener$1.class 5KB
R$attr.class 316B
R.class 475B
R$string.class 625B
NewConnectedListener.class 6KB
jarlist.cache 119B
classes
com
NewApp
MainActivity.class 3KB
MainActivity$3.class 1KB
MainActivity$2.class 3KB
MainActivity$1.class 2KB
R$layout.class 370B
MainActivity$BTBroadcastReceiver.class 3KB
R$id.class 874B
R$drawable.class 377B
MainActivity$BTBondReceiver.class 2KB
NewConnectedListener$1.class 5KB
R$attr.class 316B
R.class 475B
R$string.class 625B
NewConnectedListener.class 6KB
resources.ap_ 12KB
BioHarnessBT.jar 54KB
res
drawable-ldpi
icon1.png 2KB
drawable-hdpi
icon1.png 4KB
values
strings.xml 465B
drawable-mdpi
icon1.png 3KB
layout
main.xml 4KB
assets
gen
com
NewApp
BuildConfig.java 152B
R.java 2KB
proguard.cfg 1KB
src
com
NewApp
NewConnectedListener.java 6KB
MainActivity.java 8KB
.project 853B
BioHarnessBT
META-INF
MANIFEST.MF 39B
zephyr
android
BioHarnessBT
ConnectedEvent.class 727B
MainActivity.class 4KB
ConnectListenerImpl.class 8KB
MainActivity$3.class 1KB
MainActivity$2.class 2KB
MainActivity$1.class 2KB
MainActivity$BTBroadcastReceiver.class 3KB
ZephyrProtocol$1SetBreathingPacketListener.class 2KB
ZephyrPacketArgs.class 1KB
ConnectListenerImpl$AccelerometerPacketInfo.class 5KB
ConnectListenerImpl$RtoRPacketInfo.class 2KB
ConnectListenerImpl$GeneralPacketInfo.class 6KB
BTComms$1.class 872B
ZephyrProtocol.class 10KB
ZephyrPacketListener.class 227B
BTComms.class 4KB
MainActivity$BTBondReceiver.class 2KB
ConnectListenerImpl$AccelerometerPacketInfo$XYZ_AccelerationData.class 1022B
ZephyrPacket.class 5KB
ZephyrProtocol$1SetGeneralPacketListener.class 1KB
ReceivedEvent.class 552B
BTReceiver.class 723B
ZephyrProtocol$1SetSummaryDataPacketListener.class 2KB
ConnectListenerImpl$SummaryPacketInfo.class 8KB
ZephyrProtocol$1.class 2KB
BTClient.class 4KB
PacketTypeRequest.class 1KB
ConnectedListener.class 339B
ConnectListenerImpl$1.class 10KB
ZephyrPacketEvent.class 699B
ZephyrProtocol$1SetLoggingPacketListener.class 2KB
CRC8.class 671B
ZephyrProtocol$1SetAccelerometerListener.class 2KB
ZephyrProtocol$2.class 872B
ConnectListenerImpl$BreathingPacketInfo.class 3KB
ZephyrProtocol$1SetRtoRListener.class 2KB
ReceivedListener.class 209B
ZephyrProtocol$1SetECGListener.class 1KB
ConnectListenerImpl$EventPacketInfo.class 2KB
ConnectListenerImpl$ECGPacketInfo.class 2KB
.classpath 506B
project.properties 360B
AndroidManifest.xml 965B
共 92 条
- 1
资源评论
宋时强
- 粉丝: 2
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功