package com.testBlueTooth;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
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.BluetoothServerSocket;
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 testBlueTooth extends Activity {
static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
private UUID uuid ;
private static final String TAG = "BluetoothTest";
private static final boolean STATE_CONNECTED = true;
private Button btnSearch, btnDis, btnExit;
private ToggleButton tbtnSwitch;
private ListView lvBTDevices;
private ArrayAdapter<String> adtDevices;
private List<String> lstDevices = new ArrayList<String>();
private BluetoothAdapter btAdapt;
public static BluetoothSocket socket = null;
public static BluetoothSocket btSocket;
public static AcceptThread serverThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Button 设置
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.tbtnSwitch);
tbtnSwitch.setOnClickListener(new ClickEvent());
// ListView及其数据源 适配器
lvBTDevices = (ListView) this.findViewById(R.id.lvDevices);
adtDevices = new ArrayAdapter<String>(testBlueTooth.this,
android.R.layout.simple_list_item_1, lstDevices);
lvBTDevices.setAdapter(adtDevices);
lvBTDevices.setOnItemClickListener(new ItemClickEvent());
btAdapt = BluetoothAdapter.getDefaultAdapter();// 初始化本机蓝牙功能
uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
if(btAdapt == null){
Log.e(TAG, "No BlueToothDevice!");
finish();
return;
}
else{
if (btAdapt.getState() == BluetoothAdapter.STATE_OFF)// 读取蓝牙状态并显示
{
tbtnSwitch.setChecked(true);
Toast.makeText(testBlueTooth.this, "蓝牙尚未打开,服务端需先打开蓝牙", Toast.LENGTH_LONG).show();
}
else if (btAdapt.getState() == BluetoothAdapter.STATE_ON){
tbtnSwitch.setChecked(false);
//服务端监听
serverThread=new AcceptThread();
serverThread.start();
}
// 注册Receiver来获取蓝牙设备相关的结果
IntentFilter intent = new IntentFilter();
intent.addAction(BluetoothDevice.ACTION_FOUND);// 用BroadcastReceiver来取得搜索结果
intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(searchDevices, intent);
}
}
private void manageConnectedSocket() {
//setTitle("检测到蓝牙接入!");
btSocket=socket;
//打开控制继电器实例
Intent intent = new Intent();
intent.setClass(testBlueTooth.this, RelayControl.class);
startActivity(intent);
}
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)));
}
//搜索设备时,取得设备的MAC地址
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String str= device.getName() + "|" + device.getAddress();
if (lstDevices.indexOf(str) == -1)// 防止重复添加
lstDevices.add(str); // 获取设备名称和mac地址
adtDevices.notifyDataSetChanged();
}
}
};
@Override
protected void onDestroy() {
this.unregisterReceiver(searchDevices);
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
serverThread.cancel();
serverThread.destroy();
}
class ItemClickEvent implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String str = lstDevices.get(arg2);
String[] values = str.split("\\|");
String address=values[1];
Log.e("address",values[1]);
uuid = UUID.fromString(SPP_UUID);
Log.e("uuid",uuid.toString());
BluetoothDevice btDev = btAdapt.getRemoteDevice(address);//"00:11:00:18:05:45"
//int sdk = Integer.parseInt(Build.VERSION.SDK);
/*
int sdk = Integer.parseInt(Build.VERSION.SDK);
if (sdk >= 10) {
try {
btSocket = btDev.createInsecureRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, " High: Connection failed.", e);
}
} else {
try {
btSocket = btDev.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Low: Connection failed.", e);
}
}*/
Method m;
try {
m = btDev.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
btSocket = (BluetoothSocket) m.invoke(btDev, Integer.valueOf(1));
} 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();
}
// try {
// btSocket = btDev.createRfcommSocketToServiceRecord(uuid);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// Log.e(TAG, "Low: Connection failed.", e);
// }
// btSocket = InsecureBluetooth.listenUsingRfcommWithServiceRecord(btAdapt, "", uuid, true);
// btSocket = InsecureBluetooth.createRfcommSocketToServiceRecord(btDev, uuid, true);
btAdapt.cancelDiscovery();
try {
//btSocket = btDev.createRfcommSocketToServiceRecord(uuid);
btSocket.connect();
Log.e(TAG, " BT connection established, data transfer link open.");
Toast.makeText(testBlueTooth.this, "连接成功,进入控制界面", Toast.LENGTH_SHORT).show();
//setTitle("连接成功");
//打开控制继电器实例
Intent intent = new Intent();
intent.setClass(testBlueTooth.this, RelayControl.class);
startActivity(intent);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, " Connection failed.", e);
//Toast.makeText(getApplicationContext(), "连接失败", Toast.LENGTH_SHORT);
setTitle("连接失败..");
}
}
}
private class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
if (v == btnSearch)// 搜索蓝牙设备,在BroadcastReceiver显示结果
{
if (btAdapt.getState() == BluetoothAdapter.STATE_OFF) {// 如果蓝牙还没开启
Toast.makeText(testBlueTooth.this, "请先打开蓝牙",
Android蓝牙手机通信程序
4星 · 超过85%的资源 需积分: 31 2 浏览量
2012-10-17
15:50:30
上传
评论 63
收藏 40KB ZIP 举报
草绳哥哥
- 粉丝: 7
- 资源: 3
最新资源
- 批量将py编译为pyd文件.atbx
- Python项目-学生管理系统
- verilog HDL硬件语法设计包括算术运算三人表决器Verilog的阻塞和非阻塞赋值源码例程quartus13.1工程合集
- 【文章话题分类论文】OpenAlex Topic Classification Whitepaper
- linux学习常用命令
- 功率拓扑快速参考指南-ti,TI官方出品
- 开关电源拓朴图表,各种电路拓扑表格
- 登录和注册 前端:vue3+iview plus +axios 后台:spring boot +mybatis
- 软件测试入门简介:从基础到实践的全面介绍
- 2024CDA Level Ⅰ 认证考试大纲
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页