-
android蓝牙主动发起配对实例
- package cn.madfinger.core;
-
- import java.io.IOException;
- 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 BlueToothTestActivity extends Activity {
-
-
- static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
- Button btnSearch, btnDis, btnExit;
- ToggleButton tbtnSwitch;
- ListView lvBTDevices;
- ArrayAdapter<String> adtDevices;
- List<String> lstDevices = new ArrayList<String>();
- BluetoothAdapter btAdapt;
- public static BluetoothSocket btSocket;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- 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());
-
-
- tbtnSwitch = (ToggleButton) this.findViewById(R.id.tbtnSwitch);
- tbtnSwitch.setOnClickListener(new ClickEvent());
-
-
- lvBTDevices = (ListView) this.findViewById(R.id.lvDevices);
- 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);
- }
-
-
- IntentFilter intent = new IntentFilter();
- intent.addAction(BluetoothDevice.ACTION_FOUND);
- intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
- intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
- intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
- registerReceiver(searchDevices, 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)));
- }
- BluetoothDevice device = null;
-
- 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);
- adtDevices.notifyDataSetChanged();
- }
- }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;
- }
- }
-
- }
- };
-
- @Override
- protected void onDestroy() {
- this.unregisterReceiver(searchDevices);
- super.onDestroy();
- android.os.Process.killProcess(android.os.Process.myPid());
- }
-
- 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) {
-
- Method createBondMethod = BluetoothDevice.class
- .getMethod("createBond");
- Log.d("BlueToothTestActivity", "开始配对");
- returnValue = (Boolean) createBondMethod.invoke(btDev);
-
- }else if(btDev.getBondState() == BluetoothDevice.BOND_BONDED){
- connect(btDev);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
- }
-
- private void connect(BluetoothDevice btDev) {
- UUID uuid = UUID.fromString(SPP_UUID);
- try {
- btSocket = btDev.createRfcommSocketToServiceRecord(uuid);
- Log.d("BlueToothTestActivity", "开始连接...");
- btSocket.connect();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
-
- class ClickEvent implements View.OnClickListener {
- @Override
- public void onClick(View v) {
- if (v == btnSearch)
- {
- if (btAdapt.getState() == BluetoothAdapter.STATE_OFF) {
- Toast.makeText(BlueToothTestActivity.this, "请先打开蓝牙", 1000)
- .show();
- return;
- }
- if (btAdapt.isDiscovering())
- btAdapt.cancelDiscovery();
- lstDevices.clear();
- Object[] lstDevice = btAdapt.getBondedDevices().toArray();
- for (int i = 0; i < lstDevice.length; i++) {
- BluetoothDevice device = (BluetoothDevice) lstDevice[i];
- String str = "已配对|" + device.getName() + "|"
- + device.getAddress();
- lstDevices.add(str);
- adtDevices.notifyDataSetChanged();
- }
- setTitle("本机蓝牙地址:" + btAdapt.getAddress());
- btAdapt.startDiscovery();
- } else if (v == tbtnSwitch) {
- if (tbtnSwitch.isChecked() == false)
- btAdapt.enable();
-
- else if (tbtnSwitch.isChecked() == true)
- btAdapt.disable();
- } else if (v == btnDis)
- {
- Intent discoverableIntent = new Intent(
- BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
- discoverableIntent.putExtra(
- BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
- startActivity(discoverableIntent);
- } else if (v == btnExit) {
- try {
- if (btSocket != null)
- btSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- BlueToothTestActivity.this.finish();
- }
- }
-
- }
- }
-
相关阅读:
老罗android开发视频教程学习完了
微软云工具
Andriod视频http://pan.baidu.com/share/link?shareid=7300&uk=3339495714
微软图片编辑工具
Smartdraw世界上最流行的商业绘图软件
arcgis安装问题SDK开始安装不了
光学字符识别OCR
Android实现OCR扫描识别数字图片之图片扫描识别
android实现图片识别的几种方法
开源的asp.net工作流程引擎。 http://ccflow.org
-
原文地址:https://www.cnblogs.com/Free-Thinker/p/6514533.html
Copyright © 2020-2023
润新知