Android Bluetooth蓝牙开发(二)

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

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Bluetooth驱动开发是指针对Android操作系统的蓝牙功能进行驱动开发的过程。蓝牙驱动是操作系统与蓝牙硬件之间的桥梁,它负责实现蓝牙功能的核心代码。 在Android平台上进行Bluetooth驱动开发开发者需要了解Android的底层架构和蓝牙协议栈。首先,开发者需要熟悉Android系统的架构,包括应用层、系统服务层、HAL层和内核层等,以便更好地理解和操作Bluetooth驱动。其次,开发者还需要了解蓝牙协议栈的组成与工作原理,蓝牙协议栈包括HCI层、L2CAP层、RFCOMM层、SDP层等,每一层都需要进行相应的开发和调试。 在进行Android Bluetooth驱动开发时,开发者可以使用Android提供的Bluetooth API进行开发,通过这些API可以实现设备的蓝牙扫描、设备连接、数据传输等功能。此外,开发者还需要了解蓝牙硬件的特性和规范,以便在驱动开发中正确地操作蓝牙硬件。 在Android Bluetooth驱动开发过程中,开发者需要进行相应的测试和调试,以确保驱动程序的正确性和稳定性。开发者可以使用模拟器进行一些简单的测试,但最好还是借助真实的蓝牙设备进行全面的测试。同时,开发者还可以利用工具和调试器来定位和解决驱动开发中的问题,如使用Logcat来查看日志信息、使用adb命令进行调试等。 总之,Android Bluetooth驱动开发是一项复杂而精细的工作,需要开发者具备扎实的Android开发基础、蓝牙协议栈的专业知识和丰富的实践经验。通过合理规划和高效工作,开发者可以成功地开发出高质量的Android Bluetooth驱动程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值