package jdl.android.test;
import java.io.InputStream;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
// Message types sent from the BluetoothChatService Handler
public static final int MESSAGE_STATE_CHANGE = 1;
public static final int MESSAGE_READ = 2;
public static final int MESSAGE_WRITE = 3;
public static final int MESSAGE_DEVICE_NAME = 4;
public static final int MESSAGE_TOAST = 5;
// Key names received from the BluetoothChatService Handler
public static final String DEVICE_NAME = "device_name";
public static final String TOAST = "toast";
// Intent request codes
private static final int REQUEST_CONNECT_DEVICE = 1;
private static final int REQUEST_ENABLE_BT = 2;
private static final int REQUEST_NEW_CARD = 3;
String data = "0000";
String Button_Buffer="00000000";
String seekBar_BufferString="82";
String messege_String="";
// Layout Views
private TextView mTitle; //标题
// Name of the connected device
private String mConnectedDeviceName = null;
// String buffer for outgoing messages
private StringBuffer mOutStringBuffer;
// Local Bluetooth adapter
private BluetoothAdapter mBluetoothAdapter = null;
// Member object for the chat services
private BluetoothService mCardService = null;
Context mContext = null;
//G_
private int count = 0;
private String str = "";//存储发过来的数据(温度值)
private String ss = "";//暂存当前发过来的字节
//_G
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set up the window layout
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
//setContentView(R.layout.main);
//mAnimView = new AnimView(this);
//setContentView(mAnimView);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
// Set up the custom title
Log.e("MainActivity", "5");
Log.e("MainActivity", "7");
mTitle = (TextView) findViewById(R.id.title_left_text); //左边应用标题
Log.e("MainActivity", "622");
mTitle.setText(R.string.app_name);
Log.e("MainActivity", "6");
mTitle = (TextView) findViewById(R.id.title_right_text); //右边连接状态
// Get local Bluetooth adapter
Log.e("MainActivity", "8");
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// If the adapter is null, then Bluetooth is not supported
Log.e("MainActivity", "2");
if (mBluetoothAdapter == null) {
Toast.makeText(this, "蓝牙不可用!", Toast.LENGTH_LONG).show();
Log.e("MainActivity", "3");
finish();
return;
}
Button bt1= (Button)findViewById(R.id.bt1);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
EditText ed1 = (EditText) findViewById(R.id.ed1);
String s = ed1.getText().toString();
sendByte(s.getBytes());
//判断是否已经连接
if (mCardService.getState() == BluetoothService.STATE_CONNECTED){
Toast.makeText(getApplicationContext(),s,
Toast.LENGTH_SHORT).show();
}
else//若没有连接
{
Toast.makeText(getApplicationContext(),R.string.not_connected,
Toast.LENGTH_SHORT).show();
}
}
});
/*
* 按钮2 ----请求重发
*/
Button bt2= (Button)findViewById(R.id.bt2);
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View paramView) {
//判断是否已经连接
if (mCardService.getState() == BluetoothService.STATE_CONNECTED){
requestSend();
}
else//若没有连接
{
Toast.makeText(getApplicationContext(),R.string.not_connected,
Toast.LENGTH_SHORT).show();
}
}
});
/*
* 清空温度显示值
*/
Button bt3= (Button)findViewById(R.id.bt3);
bt3.setOnClickListener(new OnClickListener() {
TextView et1 = (TextView)findViewById(R.id.et1);//显示温度值的TextView
TextView et2 = (TextView)findViewById(R.id.et2);//显示摄氏度符号的TextView
@Override
public void onClick(View paramView) {
et1.setText("");
et2.setText("");
}
});
}
@Override
public void onStart()
{
//G
Toast.makeText(getApplicationContext(),"程序开始执行",
Toast.LENGTH_SHORT).show();
super.onStart();
//确保蓝牙打开,然后读取名片信息显示在节目上(未打开则在返回信息处理中setupCards())
if (!mBluetoothAdapter.isEnabled())
{
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
// Otherwise, setup the chat session
}
else
{
if (mCardService == null) setupCards(); //初始化ListView对象
}
}
@Override
public synchronized void onResume()
{
super.onResume();
// Performing this check in onResume() covers the case in which BT was
// not enabled during onStart(), so we were paused to enable it...
// onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
if (mCardService != null)
{
// Only if the state is STATE_NONE, do we know that we haven't started already
if (mCardService.getState() == BluetoothService.STATE_NONE)
{
// Start the Bluetooth chat services
mCardService.start();
}
}
}
private void setupCards()
{
//完成显示内容后,初始化蓝牙服务对象
mCardService = new BluetoothService(mHandler);
// Initialize the buffer for outgoing messages
setmOutStringBuffer(new StringBuffer(""));
}
public void onDestroy()
{
super.onDestroy();
// Stop the Bluetooth chat services
if (mCardService != null) mCardService.stop();
}
//开启蓝牙可见
private void ensureDiscoverable()
{
if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)
{
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
}
//处理线程返回信息
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
Toast.makeText(MainActivity.this, "change", Toast.LENGTH_LONG
没有合适的资源?快使用搜索试试~ 我知道了~
安卓蓝牙温度采集(配合单片机)
共129个文件
class:42个
png:35个
xml:18个
需积分: 9 38 下载量 192 浏览量
2016-07-24
14:00:10
上传
评论 4
收藏 3.41MB ZIP 举报
温馨提示
很简单实用的源码,没有错,可以使用。要注意安卓6.0的sdk需要运行时权限Manifest.permission.ACCESS_FINE_LOCATION,不然不能搜索到新设备。
资源推荐
资源详情
资源评论
收起资源包目录
安卓蓝牙温度采集(配合单片机) (129个子文件)
resources.ap_ 411KB
resources.ap_ 276KB
lanya_project.apk 426KB
lanya_project.apk 290KB
jarlist.cache 119B
jarlist.cache 119B
proguard.cfg 1KB
proguard.cfg 1KB
MainActivity.class 8KB
MainActivity.class 8KB
BluetoothService.class 5KB
BluetoothService.class 5KB
DeviceListActivity.class 5KB
DeviceListActivity.class 5KB
MainActivity$1.class 3KB
MainActivity$1.class 3KB
BluetoothService$ConnectThread.class 2KB
BluetoothService$ConnectThread.class 2KB
BluetoothService$ConnectedThread.class 2KB
BluetoothService$ConnectedThread.class 2KB
BluetoothService$AcceptThread.class 2KB
BluetoothService$AcceptThread.class 2KB
DeviceListActivity$2.class 2KB
DeviceListActivity$2.class 2KB
DeviceListActivity$1.class 2KB
DeviceListActivity$1.class 2KB
MainActivity$2.class 2KB
MainActivity$2.class 2KB
MainActivity$3.class 1KB
MainActivity$3.class 1KB
MainActivity$4.class 1KB
MainActivity$4.class 1KB
R$string.class 1016B
R$string.class 1016B
DeviceListActivity$3.class 870B
DeviceListActivity$3.class 870B
R$id.class 846B
R$id.class 846B
R.class 561B
R.class 561B
R$drawable.class 526B
R$drawable.class 526B
R$layout.class 521B
R$layout.class 521B
R$menu.class 389B
R$menu.class 389B
BuildConfig.class 341B
BuildConfig.class 341B
R$attr.class 334B
R$attr.class 334B
.classpath 491B
.classpath 364B
Thumbs.db 19KB
Thumbs.db 19KB
Thumbs.db 16KB
Thumbs.db 16KB
classes.dex 28KB
classes.dex 23KB
annotations-f4f7b9103eece400389f36cf69fc1cbf.jar 943B
annotations-f4f7b9103eece400389f36cf69fc1cbf.jar 943B
MainActivity.java 15KB
MainActivity.java 15KB
BluetoothService.java 15KB
BluetoothService.java 15KB
DeviceListActivity.java 8KB
DeviceListActivity.java 8KB
R.java 3KB
R.java 3KB
BuildConfig.java 158B
BuildConfig.java 158B
background2.PNG 270KB
background2.PNG 270KB
background2.PNG 169KB
background2.PNG 169KB
background2.PNG 169KB
wdj1.png 149KB
wdj1.png 149KB
wdj1.png 85KB
background.png 85KB
wdj1.png 85KB
wdj1.png 85KB
background.png 85KB
ic_launcher.png 55KB
ic_launcher.png 55KB
ic_launcher.png 6KB
ic_launcher.png 6KB
background.png 5KB
background.png 5KB
ic_launcher.png 4KB
icon.png 4KB
icon.png 4KB
icon.png 4KB
icon.png 4KB
icon.png 4KB
icon.png 3KB
icon.png 3KB
icon.png 2KB
icon.png 2KB
icon.png 2KB
icon.png 2KB
共 129 条
- 1
- 2
资源评论
时代逆子
- 粉丝: 7
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功