OK,上一节,我们打开蓝牙并打印出了设备列表,但是打印出来是无卵用的。我们要把它们显示在列表里,并进行选择,才能在实际项目中运用。
那么这一节,我们将以对话框的形式显示搜索结果。如果你对android了解足够,可以自己定义对话框显示搜索列表,那么你完全可以跳过本章节。
1、新建一个xml文件(activity_blue_tooth_device_list.xml)
里面没有写什么,只是把上一节在xml写的代码,挪到了另一个xml中而已
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:text="@string/title_paired_devices"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/paired_devices"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"></ListView>
<TextView
android:layout_width="wrap_content"
android:text="@string/title_other_devices"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/new_devices"
android:layout_width="fill_parent"
android:layout_weight="2"
android:layout_height="wrap_content"></ListView>
<Button
android:id="@+id/btn_find_device"
android:layout_width="wrap_content"
android:text="@string/find_blue_toolth_device"
android:layout_height="wrap_content" />
</LinearLayout>
2、新建Activity(DeviceListActivity.java)
package com.sangbo.bluetooth;
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.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.sangb.projecttest.BaseActivity;
import com.sangb.projecttest.R;
import java.util.Set;
/**
* Created by 桑博 on 2015/10/29.
*/
public class DeviceListActivity extends Activity {
private BluetoothAdapter m_BtAdapter;
private Button m_btnFindDevice;
private ListView m_pairedListView;
private ListView m_newListView;
private ArrayAdapter<String> m_pairedAdapter;
private ArrayAdapter<String> m_newAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置窗口
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_blue_tooth_device_list);
//设置取消后,返回
setResult(Activity.RESULT_CANCELED);
//初始化一些控件
initViews();
//获取默认的蓝牙适配器
m_BtAdapter = BluetoothAdapter.getDefaultAdapter();
//注册相应的监听
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
//寻找设备按钮事件
m_btnFindDevice = (Button) findViewById(R.id.btn_find_device);
m_btnFindDevice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//隐藏按钮
v.setVisibility(View.GONE);
//显示进度条
setProgressBarIndeterminateVisibility(true);
//如果当前本地蓝牙适配器处于搜索设备中
if(m_BtAdapter.isDiscovering()){
//那么取消搜索
m_BtAdapter.cancelDiscovery();
}
//先把数据清除一下,以免重复
m_newAdapter.clear();
//开始搜索蓝牙设备
m_BtAdapter.startDiscovery();
}
});
Set<BluetoothDevice> pairedDevices = m_BtAdapter.getBondedDevices();
if(pairedDevices.size() > 0){
for (BluetoothDevice device: pairedDevices) {
m_pairedAdapter.add(device.getName()+"\n" +device.getAddress());
}
}else{
m_pairedAdapter.add("没有配对的设备");
}
}
public void initViews() {
m_pairedListView = (ListView) findViewById(R.id.paired_devices);
m_newListView = (ListView) findViewById(R.id.new_devices);
m_pairedAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1);
m_newAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1);
m_pairedListView.setAdapter(m_pairedAdapter);
m_newListView.setAdapter(m_newAdapter);
}
//接收消息的一个监听
private final BroadcastReceiver mReceiver = 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);
//打印出蓝牙的名称和蓝牙地址
Log.i("info", "devie 蓝牙名称:" + device.getName() + ",蓝牙地址:" + device.getAddress());
m_newAdapter.add(device.getName()+"\n" +device.getAddress());
}
//搜索完成后
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//打印搜索完成
Log.i("info", "devie : 搜索结束");
//显示按钮
m_btnFindDevice.setVisibility(View.VISIBLE);
//取消进度条
setProgressBarIndeterminateVisibility(false);
}
}
};
}
3、在AndroidManifest.xml注册新添加的Activity
<activity
android:name="com.sangbo.bluetooth.DeviceListActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/conn_blue_toolth_device"
android:theme="@android:style/Theme.Holo.Dialog"/>
4、最后在BlueToothActivity.java中,添加一个跳转就可以了
Intent serverIntent = new Intent(BlueToothActivity.this,DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
5、BlueToothActivity.java的源代码
package com.sangbo.bluetooth;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.sangb.projecttest.BaseActivity;
import com.sangb.projecttest.R;
/**
* Created by 桑博 on 2015/10/28.
*/
public class BlueToothActivity extends BaseActivity{
private Button m_btnConnDevice;
private BluetoothAdapter m_BtAdapter;
private final int REQUEST_ENABLE = 0;
private final int REQUEST_CONNECT_DEVICE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blue_tooth);
initViews();
//获取默认的本地蓝牙适配器,参考①
m_BtAdapter = BluetoothAdapter.getDefaultAdapter();
//获取本地蓝牙适配器的状态,是否启用
if(!m_BtAdapter.isEnabled()){
//如果没有启用,发出提示进行启用。
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent,REQUEST_ENABLE);
//当然你也可以不提示,强行打开(如果没有root权限,系统会提示获取蓝牙的root权限)
// m_BtAdapter.enable();
}
m_btnConnDevice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent serverIntent = new Intent(BlueToothActivity.this,DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
}
});
}
@Override
public void initViews(){
m_btnConnDevice = (Button) findViewById(R.id.btn_conn_device);
}
}
6、附上运行效果图
转载请注明: SangBigYe:http://blog.csdn.net/cutelittlebo/article/details/49489225