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; ArrayListdatas = 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 { ArrayListdatas; 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布局