Android Bluetooth蓝牙开发(二)

编程入门 行业动态 更新时间:2024-10-11 15:25:06

Android Bluetooth<a href=https://www.elefans.com/category/jswz/34/1768306.html style=蓝牙开发(二)"/>

Android Bluetooth蓝牙开发(二)

OK,上一节,我们打开蓝牙并打印出了设备列表,但是打印出来是无卵用的。我们要把它们显示在列表里,并进行选择,才能在实际项目中运用。
那么这一节,我们将以对话框的形式显示搜索结果。如果你对android了解足够,可以自己定义对话框显示搜索列表,那么你完全可以跳过本章节。

1、新建一个xml文件(activity_blue_tooth_device_list.xml)

里面没有写什么,只是把上一节在xml写的代码,挪到了另一个xml中而已

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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;@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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

 <activityandroid: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;@Overridepublic 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() {@Overridepublic void onClick(View v) {Intent serverIntent = new Intent(BlueToothActivity.this,DeviceListActivity.class);startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);}});}@Overridepublic void initViews(){m_btnConnDevice = (Button) findViewById(R.id.btn_conn_device);}}

6、附上运行效果图

转载请注明: SangBigYe:

更多推荐

Android Bluetooth蓝牙开发(二)

本文发布于:2024-02-06 11:47:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1748945.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:蓝牙   Android   Bluetooth

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!