package com.qin.bt;
import android.app.Activity;
import android.os.Bundle;
import java.util.Iterator;
import java.util.Set;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener
{
private static String TAG = "Bluetooth_State";
private BluetoothAdapter mBluetoothAdapter; // 本机蓝牙适配器对象
private TextView btDesc;
private Button btOpen;
private Button btClose;
private Button btOpenBySystem; // 调用系统API去打开蓝牙
private Button btDiscoveryDevice;
private Button btCancelDiscovery;
private Button btDiscoveryBySystem; //调用系统Api去扫描蓝牙设备
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // 获得本机蓝牙适配器对象引用
if (mBluetoothAdapter == null)
{
toast("对不起 ,您的机器不具备蓝牙功能");
return;
}
IntentFilter bluetoothFilter = new IntentFilter();
bluetoothFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
bluetoothFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
this.registerReceiver(BluetoothReciever, bluetoothFilter);
//蓝牙扫描相关设备
IntentFilter btDiscoveryFilter = new IntentFilter();
btDiscoveryFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
btDiscoveryFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
btDiscoveryFilter.addAction(BluetoothDevice.ACTION_FOUND);
btDiscoveryFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
this.registerReceiver(BTDiscoveryReceiver, btDiscoveryFilter);
int initialBTState = mBluetoothAdapter.getState();
printBTState(initialBTState); // 初始时蓝牙状态
initialViews();
btDesc.setText(" Name : " + mBluetoothAdapter.getName() + " Address : "
+ mBluetoothAdapter.getAddress() + " Scan Mode --" + mBluetoothAdapter.getScanMode());
//打印处当前已经绑定成功的蓝牙设备
Set<BluetoothDevice> bts = mBluetoothAdapter.getBondedDevices();
Iterator<BluetoothDevice> iterator = bts.iterator();
while(iterator.hasNext())
{
BluetoothDevice bd = iterator.next() ;
Log.i(TAG , " Name : " + bd.getName() + " Address : "+ bd.getAddress() ); ;
Log.i(TAG, "Device class" + bd.getBluetoothClass());
}
BluetoothDevice findDevice = mBluetoothAdapter.getRemoteDevice("00:11:22:33:AA:BB");
Log.i(TAG , "findDevice Name : " + findDevice.getName() + " findDevice Address : "+ findDevice.getAddress() ); ;
Log.i(TAG , "findDevice class" + findDevice.getBluetoothClass());
}
private void initialViews()
{
btDesc = (TextView) findViewById(R.id.btDesc);
btOpen = (Button) findViewById(R.id.btOpen);
btClose = (Button) findViewById(R.id.btClose);
btOpenBySystem = (Button) findViewById(R.id.btOpenBySystem);
btDiscoveryDevice = (Button) findViewById(R.id.btDiscoveryDevice);
btCancelDiscovery = (Button) findViewById(R.id.btCancelDiscovery);
btDiscoveryBySystem = (Button) findViewById(R.id.btDiscoveryBySystem);
btOpen.setOnClickListener(this);
btClose.setOnClickListener(this);
btOpenBySystem.setOnClickListener(this);
btDiscoveryDevice.setOnClickListener(this);
btCancelDiscovery.setOnClickListener(this);
btDiscoveryBySystem.setOnClickListener(this);
}
//蓝牙开个状态以及扫描状态的广播接收器
private BroadcastReceiver BluetoothReciever = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction()))
{
Log.v(TAG, "### Bluetooth State has changed ##");
int btState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.STATE_OFF);
printBTState(btState);
}
else if(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(intent.getAction()))
{
Log.v(TAG, "### ACTION_SCAN_MODE_CHANGED##");
int cur_mode_state = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.SCAN_MODE_NONE);
int previous_mode_state = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_SCAN_MODE, BluetoothAdapter.SCAN_MODE_NONE);
Log.v(TAG, "### cur_mode_state ##" + cur_mode_state + " ~~ previous_mode_state" + previous_mode_state);
}
}
};
//蓝牙扫描时的广播接收器
private BroadcastReceiver BTDiscoveryReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(intent.getAction()))
{
Log.v(TAG, "### BT ACTION_DISCOVERY_STARTED ##");
}
else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent.getAction()))
{
Log.v(TAG, "### BT ACTION_DISCOVERY_FINISHED ##");
}
else if(BluetoothDevice.ACTION_FOUND.equals(intent.getAction()))
{
Log.v(TAG, "### BT BluetoothDevice.ACTION_FOUND ##");
BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(btDevice != null)
Log.v(TAG , "Name : " + btDevice.getName() + " Address: " + btDevice.getAddress());
}
else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction()))
{
Log.v(TAG, "### BT ACTION_BOND_STATE_CHANGED ##");
int cur_bond_state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
int previous_bond_state = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.BOND_NONE);
Log.v(TAG, "### cur_bond_state ##" + cur_bond_state + " ~~ previous_bond_state" + previous_bond_state);
}
}
};
private void printBTState(int btState)
{
switch (btState)
{
case BluetoothAdapter.STATE_OFF:
toast("蓝牙状态:已关闭");
Log.v(TAG, "BT State : BluetoothAdapter.STATE_OFF ###");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
toast("蓝牙状态:正在关闭");
Log.v(TAG, "BT State : BluetoothAdapter.STATE_TURNING_OFF ###");
break;
case BluetoothAdapter.STATE_TURNING_ON:
toast("蓝牙状态:正在打开");
Log.v(TAG, "BT State :BluetoothAdapter.STATE_TURNING_ON ###");
break;
case BluetoothAdapter.STATE_ON:
toast("蓝牙状态:已打开");
Log.v(TAG, "BT State :BluetoothAdapter.STATE_ON ###");
break;
default:
break;
}
}
private final int R
没有合适的资源?快使用搜索试试~ 我知道了~
Android中蓝牙的基本使用----BluetoothAdapter类简介
共23个文件
class:9个
xml:3个
png:3个
5星 · 超过95%的资源 需积分: 49 2.8k 下载量 172 浏览量
2012-07-08
13:11:15
上传
评论 21
收藏 53KB RAR 举报
温馨提示
Android中蓝牙的基本使用----BluetoothAdapter类简介
资源推荐
资源详情
资源评论
收起资源包目录
BluetoothMTest.rar (23个子文件)
BluetoothMTest
.project 850B
src
com
qin
bt
MainActivity.java 11KB
AndroidManifest.xml 675B
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
layout
main.xml 1KB
values
strings.xml 176B
assets
gen
com
qin
bt
R.java 1KB
default.properties 364B
.classpath 280B
bin
resources.ap_ 11KB
com
qin
bt
R$layout.class 370B
R$drawable.class 376B
R$attr.class 316B
R$id.class 584B
MainActivity$2.class 2KB
R.class 475B
R$string.class 403B
MainActivity$1.class 2KB
MainActivity.class 8KB
classes.dex 11KB
BluetoothMTest.apk 18KB
共 23 条
- 1
qinjuning
- 粉丝: 5296
- 资源: 30
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页