查找蓝牙设备
查找分为两部分:第一是已经绑定的设备,第二个搜索新的设备;于已绑定的设备,我们可以直接通过BluetoothAdapter的getBondedDevices()这个方法来获得。该方法返回的是一个Set
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Set devices = adapter.getBondedDevices();
for (BluetoothDevice device : devices) {
System.out.println(device.getName() + " : " + device.getAddress());
}
查找新的设备,我们要调用startDiscovery()方法,这个方法并不会在我们的程序中执行,它是系统进程来的,然后通过异步发送广播的方式来通知。所以我们还要注册一个广播接收器来接收系统的广播,这个广播的action为BluetoothDevice.ACTION_FOUND。它携带两个值,一个是BluetoothDevice.EXTRA_DEVICE另一个是BluetoothDevice.EXTRA_CLASS分别表示哪一个具体的设备和这个设备的类型。
private class BluetoothDeviceScanListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// 这里可以把我们的将我们的设备添加到一个列表中
System.out.println(device.getName() + " : " + device.getAddress());
}
}
设置蓝牙可搜索
/**
* 打开蓝牙可见性
* @param context
*/
public void enableVisibly(Context context) {
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
context.startActivity(discoverableIntent);
}
在官方的文档中一直强调,查找设备这个动作是会消耗大量的系统资源的,而且它会大大减少蓝牙的可用带宽,所以当我们找到了目标设备后,应该由我们主动调用cancelDiscovery()这个方法来取消搜索,而不应该期待默认的搜索时间到了,由系统来取消搜索。
小Demo(可以不看)
这里我们用一个listview来展示数据;
list的布局
数据的设配器用到了ViewHolder来优化listview
package com.example.phonejason;
import android.bluetooth.BluetoothDevice;
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.List;
/**
* Created by 小新 on 2016/6/11.
*/
public class DeviceAdapter extends BaseAdapter {
private Context mContext;
private List mDate;
public DeviceAdapter(List Date, Context context){
mDate = Date;
mContext = context;
}
@Override
public int getCount() {
return mDate.size();
}
@Override
public Object getItem(int position) {
return mDate.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView==null){
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.deviceadapter_layout,null);
viewHolder.textView1 = (TextView)convertView.findViewById(R.id.textview1);
viewHolder.textView2 = (TextView)convertView.findViewById(R.id.textview2);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
//获取蓝牙设备
BluetoothDevice bluetoothDevice = (BluetoothDevice) getItem(position);
viewHolder.textView1.setText("Name="+bluetoothDevice.getName());
viewHolder.textView2.setText("Address"+bluetoothDevice.getAddress());
return convertView;
}
public class ViewHolder{
public TextView textView1;
public TextView textView2;
}
public void refresh(List data){
mDate = data;
notifyDataSetChanged();
}
}
页面包含一个listview,菜单包含
代码:
菜单
在MainActivityfest中实现的逻辑
package com.example.phonejason;
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.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public static final int REQUEST_CODE = 0;
private List mDeviceList = new ArrayList<>();
private List mBondedDeviceList = new ArrayList<>();
private BlueToothController mController = new BlueToothController();
private ListView mListView;
//设配器
private DeviceAdapter mAdapter;
private Toast mToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
IntentFilter filter = new IntentFilter();
//开始查找
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
//结束查找
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
//查找设备
filter.addAction(BluetoothDevice.ACTION_FOUND);
//设备扫描模式改变
filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
//绑定状态
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mReceiver, filter);
mController.turnOnBlueTooth(this, REQUEST_CODE);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//开始搜索蓝牙(开始查找)
if( BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action) ) {
setProgressBarIndeterminateVisibility(true);
//初始化数据列表
mDeviceList.clear();
mAdapter.notifyDataSetChanged();
}//搜索蓝牙结束(结束查找)
else if( BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
}//找到蓝牙(查找设备)
else if( BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//找到一个,添加一个
mDeviceList.add(device);
mAdapter.notifyDataSetChanged();
}//设备扫描模式改变
else if( BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {
int scanMode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE,0);
if( scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
setProgressBarIndeterminateVisibility(true);
}
else {
setProgressBarIndeterminateVisibility(false);
}
}//绑定状态
else if( BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action) ) {
BluetoothDevice remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if( remoteDevice == null ) {
showToast("no device");
return;
}
int status = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,0);
if( status == BluetoothDevice.BOND_BONDED) {
showToast("Bonded " + remoteDevice.getName());
}
else if( status == BluetoothDevice.BOND_BONDING){
showToast("Bonding " + remoteDevice.getName());
}
else if(status == BluetoothDevice.BOND_NONE){
showToast("Not bond " + remoteDevice.getName());
}
}
}
};
//初始化UI控件
private void initUI() {
mListView = (ListView) findViewById(R.id.device_list);
mAdapter = new DeviceAdapter(mDeviceList, this);
mListView.setAdapter(mAdapter);
//给listview设置点击事件
mListView.setOnItemClickListener(bindDeviceClick);
}
@Override
protected void onDestroy() {
super.onDestroy();
//注销广播
unregisterReceiver(mReceiver);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//加载菜单
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
private void showToast(String text) {
if( mToast == null) {
mToast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
}
else {
mToast.setText(text);
}
mToast.show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.enable_visiblity) {
mController.enableVisibly(this);
}
else if( id == R.id.find_device) {
//查找设备
mAdapter.refresh(mDeviceList);
mController.findDevice();
mListView.setOnItemClickListener(bindDeviceClick);
}
else if (id == R.id.bonded_device) {
//查看已绑定设备
mBondedDeviceList = mController.getBondedDeviceList();
mAdapter.refresh(mBondedDeviceList);
mListView.setOnItemClickListener(null);
}
return super.onOptionsItemSelected(item);
}
//设配器控件的点击事件
private AdapterView.OnItemClickListener bindDeviceClick = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
BluetoothDevice device = mDeviceList.get(i);
device.createBond();
}
};
}