博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
蓝牙扫描功能
阅读量:5822 次
发布时间:2019-06-18

本文共 6649 字,大约阅读时间需要 22 分钟。

1.加入需要的权限

2.扫描界面实现

4.功能实现

package myapplication.com.myblue;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.content.BroadcastReceiver;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.IntentFilter;import android.os.Handler;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;import java.util.ArrayList;import java.util.List;import myapplication.com.myblue.activity.Zhuye_Activity;import myapplication.com.myblue.adapter.MyAdapter;public class Search_Activity extends AppCompatActivity {    BluetoothAdapter mBluetoothAdapter;    ArrayList
datas = new ArrayList
(); ListView listview; // ArrayAdapter
ad; MyAdapter ad; ImageView imageView1; ImageView textView_shoushuo; TextView textView1; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); Intent intent=new Intent(this,Zhuye_Activity.class); startActivity(intent); initView(); mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter(); IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver,filter); IntentFilter filter2=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(mReceiver,filter2); textView_shoushuo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(!mBluetoothAdapter.isEnabled()) { mBluetoothAdapter.enable(); } mBluetoothAdapter.startDiscovery(); textView1.setText("搜索中..."); } }); // ad = new ArrayAdapter
(this, android.R.layout.simple_list_item_1, datas); ad=new MyAdapter(datas,getApplicationContext()); listview.setAdapter(ad); } public void initView(){ imageView1= (ImageView) findViewById(R.id.imageView1); imageView1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); listview= (ListView) findViewById(R.id.listView); textView_shoushuo= (ImageView) findViewById(R.id.textView_shoushuo); textView1= (TextView) findViewById(R.id.textView1); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView
parent, View view, int position, long id) { String s= (String) parent.getItemAtPosition(position); Intent intent=new Intent(Search_Activity.this,Zhuye_Activity.class); intent.putExtra("mac",s); /** * 离开本页面时广播就解除注册,并关闭蓝牙 * **/ mBluetoothAdapter.disable(); unregisterReceiver(mReceiver); startActivity(intent); } }); } /** * * 解除注册 * **/ public void onDestroy() { super.onDestroy(); //解除注册 unregisterReceiver(mReceiver); Log.e("destory","解除注册"); } /** * 打开蓝牙设备 * **/ public void enable() { //蓝牙设备 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); //启用 mBluetoothAdapter.enable(); } /*** * 定义广播接收 * **/ private BroadcastReceiver mReceiver=new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { String action=intent.getAction(); Log.e("ywq", action); if(action.equals(BluetoothDevice.ACTION_FOUND)) { BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if(device.getBondState()==BluetoothDevice.BOND_BONDED) { //显示已配对设备 // 这里的datas.indexof()用来判断是否重复添加
            String s=device.getName()+"*"+device.getAddress();           if(datas.indexOf(s)==-1){
               datas.add(device.getName()+"*"+device.getAddress());             }
}else if(device.getBondState()!=BluetoothDevice.BOND_BONDED) {
            String s=device.getName()+"*"+device.getAddress();                if(datas.indexOf(s)==-1){
                 datas.add(device.getName()+"*"+device.getAddress());                   }
} }else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){ // text2.setText("搜索完成..."); textView1.setText("搜索完成..."); } } }; }

4.ListView适配器

package myapplication.com.myblue.adapter;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;import java.util.ArrayList;import myapplication.com.myblue.R;/** * Created by Administrator on 2016/11/24. */public class MyAdapter extends BaseAdapter {    ArrayList
datas; Context context; public MyAdapter(ArrayList
datas, Context context) { this.datas = datas; this.context = context; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return datas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView==null){ convertView= LayoutInflater.from(context).inflate(R.layout.searchitem_layout, null); holder=new ViewHolder(); holder.textViewl=(TextView) convertView.findViewById(R.id.textView_item); convertView.setTag(holder); }else{ holder=(ViewHolder) convertView.getTag(); } String s=datas.get(position); holder.textViewl.setText("MAC:"+s); return convertView; } class ViewHolder{ TextView textViewl; }}

ListView—item布局

 

转载于:https://www.cnblogs.com/galibujianbusana/p/6119458.html

你可能感兴趣的文章
linux命令:ls
查看>>
Using RequireJS in AngularJS Applications
查看>>
hdu 2444(二分图最大匹配)
查看>>
【SAP HANA】关于SAP HANA中带层次结构的计算视图Cacultation View创建、激活状况下在系统中生成对象的研究...
查看>>
DevOps 前世今生 | mPaaS 线上直播 CodeHub #1 回顾
查看>>
iOS 解决UITabelView刷新闪动
查看>>
CentOS 7 装vim遇到的问题和解决方法
查看>>
JavaScript基础教程1-20160612
查看>>
FCN图像分割
查看>>
ios xmpp demo
查看>>
python matplotlib 中文显示参数设置
查看>>
【ros】Create a ROS package:package dependencies报错
查看>>
通过容器编排和服务网格来改进Java微服务的可测性
查看>>
re:Invent解读:没想到你是这样的AWS
查看>>
PyTips 0x02 - Python 中的函数式编程
查看>>
阿里云安全肖力:安全基础建设是企业数字化转型的基石 ...
查看>>
使用《Deep Image Prior》来做图像复原
查看>>
Linux基础命令---rmdir
查看>>
编玩边学获数千万元A轮融资,投资方为君联资本
查看>>
蓝图(Blueprint)详解
查看>>