package zicox.esc;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class Demo_ad_escActivity extends Activity
{
//---------------------------------------------------
public static String ErrorMessage;
Button btnSearch, btnDis, btnExit;
ToggleButton tbtnSwitch;
ListView lvBTDevices;
ArrayAdapter<String> adtDevices;
List<String> lstDevices = new ArrayList<String>();
BluetoothAdapter btAdapt;
public static BluetoothSocket btSocket;
//---------------------------------------------------
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// if(!ListBluetoothDevice())finish();
Button Button1 = (Button) findViewById(R.id.button1);
ErrorMessage = "";
//---------------------------------------------------
btnSearch = (Button) this.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new ClickEvent());
btnExit = (Button) this.findViewById(R.id.btnExit);
btnExit.setOnClickListener(new ClickEvent());
btnDis = (Button) this.findViewById(R.id.btnDis);
btnDis.setOnClickListener(new ClickEvent());
// ToogleButton设置
tbtnSwitch = (ToggleButton) this.findViewById(R.id.toggleButton1);
tbtnSwitch.setOnClickListener(new ClickEvent());
// ListView及其数据源 适配器
lvBTDevices = (ListView) this.findViewById(R.id.listView1);
adtDevices = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, lstDevices);
lvBTDevices.setAdapter(adtDevices);
lvBTDevices.setOnItemClickListener(new ItemClickEvent());
btAdapt = BluetoothAdapter.getDefaultAdapter();// 初始化本机蓝牙功能
if (btAdapt.isEnabled())
tbtnSwitch.setChecked(false);
else
tbtnSwitch.setChecked(true);
// 注册Receiver来获取蓝牙设备相关的结果
String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";
IntentFilter intent = new IntentFilter();
intent.addAction(BluetoothDevice.ACTION_FOUND);// 用BroadcastReceiver来取得搜索结果
intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
intent.addAction(ACTION_PAIRING_REQUEST);
intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(searchDevices, intent);
Button1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
// Print1(SelectedBDAddress);
}
});
}
//---------------------------------------------------
private BroadcastReceiver searchDevices = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Bundle b = intent.getExtras();
Object[] lstName = b.keySet().toArray();
// 显示所有收到的消息及其细节
for (int i = 0; i < lstName.length; i++) {
String keyName = lstName[i].toString();
Log.e(keyName, String.valueOf(b.get(keyName)));
}
BluetoothDevice device = null;
// 搜索设备时,取得设备的MAC地址
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() == BluetoothDevice.BOND_NONE) {
String str = "未配对|" + device.getName() + "|"
+ device.getAddress();
if (lstDevices.indexOf(str) == -1)// 防止重复添加
lstDevices.add(str); // 获取设备名称和mac地址
adtDevices.notifyDataSetChanged();
try {
ClsUtils.setPin(device.getClass(),device,"0000");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ClsUtils.cancelPairingUserInput(device.getClass(), device);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
switch (device.getBondState()) {
case BluetoothDevice.BOND_BONDING:
Log.d("BlueToothTestActivity", "正在配对......");
break;
case BluetoothDevice.BOND_BONDED:
Log.d("BlueToothTestActivity", "完成配对");
connect(device);//连接设备
break;
case BluetoothDevice.BOND_NONE:
Log.d("BlueToothTestActivity", "取消配对");
default:
break;
}
}
if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST"))
{
Log.e("tag11111111111111111111111", "ddd");
BluetoothDevice btDevice = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");
// device.setPin(pinBytes);
try
{
ClsUtils.setPin(btDevice.getClass(), btDevice, "0000"); // 手机和蓝牙采集器配对
ClsUtils.createBond(btDevice.getClass(), btDevice);
ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
class ItemClickEvent implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if(btAdapt.isDiscovering())btAdapt.cancelDiscovery();
String str = lstDevices.get(arg2);
String[] values = str.split("\\|");
String address = values[2];
Log.e("address", values[2]);
BluetoothDevice btDev = btAdapt.getRemoteDevice(address);
try {
Boolean returnValue = false;
if (btDev.getBondState() == BluetoothDevice.BOND_NONE) {
//利用反射方法调用BluetoothDevice.createBond(BluetoothDevice remoteDevice);
// Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
// Log.d("BlueToothTestActivity", "开始配对");
//
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
demo_ad_esc(自动配对,无弹出框).rar (43个子文件)
demo_ad_esc(自动配对,无弹出框)
.project 847B
proguard.cfg 1KB
src
zicox
esc
Demo_ad_escActivity.java 11KB
ClsUtils.java 6KB
StatusBox.java 2KB
MessageBox.java 2KB
BtSPP.java 5KB
AndroidManifest.xml 849B
res
drawable-ldpi
icon.png 5KB
drawable-hdpi
icon.png 7KB
drawable-mdpi
icon.png 6KB
layout
messagebox.xml 2KB
statusbox_shape.xml 360B
statusbox.xml 694B
main.xml 3KB
values
strings.xml 225B
assets
gen
zicox
esc
R.java 2KB
default.properties 362B
.classpath 280B
bin
zicox
esc
R$layout.class 473B
R$drawable.class 373B
R$attr.class 313B
StatusBox$2.class 687B
MessageBox$3.class 698B
Demo_ad_escActivity.class 8KB
Demo_ad_escActivity$2.class 769B
Demo_ad_escActivity$ItemClickEvent.class 3KB
Demo_ad_escActivity$ClickEvent.class 3KB
Demo_ad_escActivity$1.class 4KB
StatusBox.class 2KB
ClsUtils.class 4KB
BtSPP.class 5KB
StatusBox$3.class 801B
R$id.class 989B
StatusBox$1.class 1KB
R.class 468B
R$string.class 433B
MessageBox$2.class 736B
MessageBox$1.class 736B
MessageBox.class 2KB
resources.ap_ 17KB
Demo_ad_escActivity.apk 30KB
classes.dex 24KB
共 43 条
- 1
jilong17
- 粉丝: 59
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页