首页文章Android蓝牙手机通信程序手机程序「Android蓝牙手机通信程序」

Android蓝牙手机通信程序手机程序「Android蓝牙手机通信程序」

时间2025-01-16 22:52:50发布yu分类文章浏览143
导读:package com.testBlueTooth; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java...

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 adtDevices; private List lstDevices = new ArrayList(); 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(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) {// 如果蓝牙还没开启

德清版权声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕,E-mail:xinmeigg88@163.com

展开全文READ MORE
程序手机通信蓝牙
真香永不迟到:四款颜值与性能兼备的手机推荐好看的手机「真香永不迟到:四款颜值与性能兼备的手机推荐」 堪舆中的罗盘有什么价值和作用?普通人能用上吗?手机罗盘「堪舆中的罗盘有什么价值和作用?普通人能用上吗?」