蓝牙匹配
来源:互联网 发布:js 全角 半角 编辑:程序博客网 时间:2024/04/29 10:48
package com.example.bluedemo;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;import java.util.Set;import android.app.Activity;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.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.AdapterView;import android.widget.Button;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;public class MainActivity extends Activity { private BluetoothAdapter mBluetoothAdapter; private List<String> liststr; private ListView listview; private MyViewAdapter myadapter; private Button server,client; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.mainlist); liststr = new ArrayList<String>(); listview = (ListView) findViewById(R.id.listview); server = (Button) findViewById(R.id.server); client = (Button) findViewById(R.id.client); server.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent in = new Intent(getApplication(), Server.class); startActivity(in); } }); client.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent in = new Intent(getApplication(), ClientRoom.class); startActivity(in); } }); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> pairedDevices = mBluetoothAdapter .getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { liststr.add(device.getName() + "|" + device.getAddress()); } } // myadapter = new MyViewAdapter(this, liststr); listview.setAdapter(myadapter); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { String address = liststr.get(position); address = address.substring(address.indexOf("|") + 1, address.length()); // 获取设备 BluetoothDevice btDev = mBluetoothAdapter .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", "开始配对"); returnValue = (Boolean) createBondMethod.invoke(btDev); } } catch (Exception e) { e.printStackTrace(); } } }); IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); this.registerReceiver(receiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); this.registerReceiver(receiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); this.registerReceiver(receiver, filter); } public void onClick_Search(View view) { setProgressBarIndeterminateVisibility(true); setTitle("正在扫描..."); if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); } mBluetoothAdapter.startDiscovery(); }/** * */ private final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { liststr.add(device.getName() + "|" + device.getAddress()); myadapter.notifyDataSetChanged(); } } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED .equals(action)) { setProgressBarIndeterminateVisibility(false); setTitle("搜索完成"); } } };}
0 0
- 蓝牙匹配
- android 蓝牙 搜索匹配
- MacBook 蓝牙键盘的匹配
- Android 蓝牙4.0实现,自动匹配蓝牙设备
- 扫描已匹配的蓝牙设备
- 关于android手机的蓝牙自动匹配实现
- Android 蓝牙自动匹配PIN码跳过用户交互
- 关于蓝牙BLE开发中要做掉的东西——除了设备和客户端外的蓝牙匹配对话框,notification 蓝牙匹配的通知栏
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- 蓝牙
- Android Studio如何发布APK
- 信号同步机制
- 2416开发记录八: platform驱动之LED(字符设备)
- Objective-C多线程详解(NSThread、NSOperation、GCD)
- 性能测试新手误区(一):找不到测试点,不知为何而测
- 蓝牙匹配
- iOS 【通知】
- Caffe训练过程:test_iter test_interval等概念
- Hbase存储架构(整理)
- PCL点云库配置整理
- C++ IO基础
- 初识JavaScript Promises
- Java基础_10_IO流总结
- map.js的编写(js编写一个对象的方式)