查找藍(lán)牙設(shè)備
查找分為兩部分:第一是已經(jīng)綁定的設(shè)備,第二個(gè)搜索新的設(shè)備;于已綁定的設(shè)備,我們可以直接通過(guò)BluetoothAdapter的getBondedDevices()這個(gè)方法來(lái)獲得。該方法返回的是一個(gè)Set<BluetoothDevice>我們可以直接從人獲得我們需要的信息:
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> devices = adapter.getBondedDevices();
for (BluetoothDevice device : devices) {
System.out.println(device.getName() + " : " + device.getAddress());
}
查找新的設(shè)備,我們要調(diào)用startDiscovery()方法,這個(gè)方法并不會(huì)在我們的程序中執(zhí)行,它是系統(tǒng)進(jìn)程來(lái)的,然后通過(guò)異步發(fā)送廣播的方式來(lái)通知。所以我們還要注冊(cè)一個(gè)廣播接收器來(lái)接收系統(tǒng)的廣播,這個(gè)廣播的action為BluetoothDevice.ACTION_FOUND。它攜帶兩個(gè)值,一個(gè)是BluetoothDevice.EXTRA_DEVICE另一個(gè)是BluetoothDevice.EXTRA_CLASS分別表示哪一個(gè)具體的設(shè)備和這個(gè)設(shè)備的類型。
private class BluetoothDeviceScanListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// 這里可以把我們的將我們的設(shè)備添加到一個(gè)列表中
System.out.println(device.getName() + " : " + device.getAddress());
}
}
設(shè)置藍(lán)牙可搜索
/**
* 打開(kāi)藍(lán)牙可見(jiàn)性
* @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);
}
在官方的文檔中一直強(qiáng)調(diào),查找設(shè)備這個(gè)動(dòng)作是會(huì)消耗大量的系統(tǒng)資源的,而且它會(huì)大大減少藍(lán)牙的可用帶寬,所以當(dāng)我們找到了目標(biāo)設(shè)備后,應(yīng)該由我們主動(dòng)調(diào)用cancelDiscovery()這個(gè)方法來(lái)取消搜索,而不應(yīng)該期待默認(rèn)的搜索時(shí)間到了,由系統(tǒng)來(lái)取消搜索。
小Demo(可以不看)
這里我們用一個(gè)listview來(lái)展示數(shù)據(jù);
list的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
數(shù)據(jù)的設(shè)配器用到了ViewHolder來(lái)優(yōu)化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<BluetoothDevice> mDate;
public DeviceAdapter(List<BluetoothDevice> 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();
}
//獲取藍(lán)牙設(shè)備
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<BluetoothDevice> data){
mDate = data;
notifyDataSetChanged();
}
}
頁(yè)面包含一個(gè)listview,菜單包含
代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.phonejason.MainActivity">
<ListView
android:id="@+id/device_list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
菜單
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/enable_visiblity" android:title="可見(jiàn)"
android:orderInCategory="100" app:showAsAction="always|withText" />
<item android:id="@+id/find_device" android:title="查找設(shè)備"
android:orderInCategory="100" app:showAsAction="always|withText" />
<item android:id="@+id/bonded_device" android:title="綁定設(shè)備"
android:orderInCategory="100" app:showAsAction="always|withText" />
</menu>
在MainActivityfest中實(shí)現(xiàn)的邏輯
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<BluetoothDevice> mDeviceList = new ArrayList<>();
private List<BluetoothDevice> mBondedDeviceList = new ArrayList<>();
private BlueToothController mController = new BlueToothController();
private ListView mListView;
//設(shè)配器
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();
//開(kāi)始查找
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
//結(jié)束查找
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
//查找設(shè)備
filter.addAction(BluetoothDevice.ACTION_FOUND);
//設(shè)備掃描模式改變
filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
//綁定狀態(tài)
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();
//開(kāi)始搜索藍(lán)牙(開(kāi)始查找)
if( BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action) ) {
setProgressBarIndeterminateVisibility(true);
//初始化數(shù)據(jù)列表
mDeviceList.clear();
mAdapter.notifyDataSetChanged();
}//搜索藍(lán)牙結(jié)束(結(jié)束查找)
else if( BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
}//找到藍(lán)牙(查找設(shè)備)
else if( BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//找到一個(gè),添加一個(gè)
mDeviceList.add(device);
mAdapter.notifyDataSetChanged();
}//設(shè)備掃描模式改變
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);
}
}//綁定狀態(tài)
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設(shè)置點(diǎn)擊事件
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) {
//查找設(shè)備
mAdapter.refresh(mDeviceList);
mController.findDevice();
mListView.setOnItemClickListener(bindDeviceClick);
}
else if (id == R.id.bonded_device) {
//查看已綁定設(shè)備
mBondedDeviceList = mController.getBondedDeviceList();
mAdapter.refresh(mBondedDeviceList);
mListView.setOnItemClickListener(null);
}
return super.onOptionsItemSelected(item);
}
//設(shè)配器控件的點(diǎn)擊事件
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();
}
};
}