Android开发中编写蓝牙相关功能的核心代码讲解

一. 什么是蓝牙(Bluetooth)?
1.1  BuleTooth是目前使用最广泛的无线通信协议
1.2  主要针对短距离设备通讯(10m)
1.3  常用于连接耳机,鼠标和移动通讯设备等.
二. 与蓝牙相关的API
2.1 BluetoothAdapter:
代表了本地的蓝牙适配器
2.2 BluetoothDevice
代表了一个远程的Bluetooth设备
三. 扫描已经配对的蓝牙设备(1)
注:必须部署在真实手机上,模拟器无法实现
首先需要在AndroidManifest.xml 声明蓝牙权限
<user-permission android:name="android.permission.BLUETOOTH" />
配对蓝牙需要手动操作:
1. 打开设置--> 无线网络 --> 蓝牙 勾选开启
2. 打开蓝牙设置  扫描周围已经开启的蓝牙设备(可以与自己的笔记本电脑进行配对),点击进行配对
 电脑上会弹出提示窗口: 添加设备
 显示计算与设备之间的配对码,要求确认是否配对
 手机上也会显示类似的提示.
四. 扫描已经配对的蓝牙设备(2)
4.1 获得BluetoothAdapter对象
4.2 判断当前移动设备中是否拥有蓝牙
4.3 判断当前移动设备中蓝牙是否已经打开
4.4 得到所有已经配对的蓝牙设备对象

蓝牙配对实现的核心代码如下:

MainActivity: import java.util.Iterator; import java.util.Set; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button button = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button)findViewById(R.id.buttonId); button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { //获得BluetoothAdapter对象,该API是android 2.0开始支持的 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); //adapter不等于null,说明本机有蓝牙设备 if(adapter != null){ System.out.println("本机有蓝牙设备!"); //如果蓝牙设备未开启 if(!adapter.isEnabled()){ Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); //请求开启蓝牙设备 startActivity(intent); } //获得已配对的远程蓝牙设备的集合 Set<BluetoothDevice> devices = adapter.getBondedDevices(); if(devices.size()>0){ for(Iterator<BluetoothDevice> it = devices.iterator();it.hasNext();){ BluetoothDevice device = (BluetoothDevice)it.next(); //打印出远程蓝牙设备的物理地址 System.out.println(device.getAddress()); } }else{ System.out.println("还没有已配对的远程蓝牙设备!"); } }else{ System.out.println("本机没有蓝牙设备!"); } } }); } }

修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备
1. 修改本机蓝牙设备的可见性
2. 扫描周围可用的蓝牙设备

Eg:
一.  清单文件AdroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.se7en" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.BLUETOOTH"/> <!-若需要管理蓝牙设备,如修改可见性,则需以下的权限-> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> </manifest>

二. 布局文件: main.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/discoverButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="设置可见性"/> <Button android:id="@+id/scanButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="开始扫描"/> </LinearLayout>

三. MainActivity:

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.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button discoverButton = null; private Button scanButton = null; private BluetoothAdapter adapter = null; private BluetoothReceiver bluetoothReceiver = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); adapter = BluetoothAdapter.getDefaultAdapter(); discoverButton = (Button)findViewById(R.id.discoverButton); scanButton = (Button)findViewById(R.id.scanButton); //修改蓝牙设备的可见性 discoverButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View view) { Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); //设置蓝牙可见性,500表示可见时间(单位:秒),当值大于300时默认为300 discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,500); startActivity(discoverIntent); } }); scanButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { //开始扫描周围蓝牙设备,该方法是异步调用并以广播的机制返回,所以需要创建一个BroadcastReceiver来获取信息 adapter.startDiscovery(); } }); //设定广播接收的filter IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); //创建蓝牙广播信息的receiver bluetoothReceiver = new BluetoothReceiver (); //注册广播接收器 registerReceiver(bluetoothReceiver,intentFilter); } private class BluetoothReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { //获得扫描到的远程蓝牙设备 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); System.out.println(device.getAddress()); } } }

时间: 2024-07-29 19:52:58

Android开发中编写蓝牙相关功能的核心代码讲解的相关文章

Android开发中编写蓝牙相关功能的核心代码讲解_Android

一. 什么是蓝牙(Bluetooth)?1.1  BuleTooth是目前使用最广泛的无线通信协议 1.2  主要针对短距离设备通讯(10m) 1.3  常用于连接耳机,鼠标和移动通讯设备等.二. 与蓝牙相关的API2.1 BluetoothAdapter: 代表了本地的蓝牙适配器 2.2 BluetoothDevice 代表了一个远程的Bluetooth设备三. 扫描已经配对的蓝牙设备(1)注:必须部署在真实手机上,模拟器无法实现 首先需要在AndroidManifest.xml 声明蓝牙权限

Android开发中应用程序分享功能实例_Android

本文实例讲述了Android开发中应用程序分享功能.分享给大家供大家参考,具体如下: Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); //设置类型 shareIntent.setType("text/plain"); //设置分享的主题 shareIntent.putExtra("android.intent.extra.SUBJECT", "分享&

Android开发中应用程序分享功能实例

本文实例讲述了Android开发中应用程序分享功能.分享给大家供大家参考,具体如下: Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); //设置类型 shareIntent.setType("text/plain"); //设置分享的主题 shareIntent.putExtra("android.intent.extra.SUBJECT", "分享&

rfid-请问Android开发中一般带有NFC功能的手机能否实现NFID识别入场证id信息

问题描述 请问Android开发中一般带有NFC功能的手机能否实现NFID识别入场证id信息 请问Android开发中一般带有NFC功能的手机能否实现NFID识别入场证id信息 是否需要相应的物理模块? 比如在应用中实现读取员工卡的id来实现某个app的登陆功能. 解决方案 已解决. 从硬件厂家要了驱动接口

Android开发中RecyclerView组件使用的一些进阶技讲解_Android

RecyclerView的优势: 它自带ViewHolder来实现View的复用机制,再也不用ListView那样在getView()里自己写了 使用LayoutManager可以实现ListView,GridView以及流式布局的列表效果 通过setItemAnimator(ItemAnimator animator)可以实现增删动画(懒的话,可以使用默认的ItemAnimator对象,效果也不错) 控制item的间隔,可以使用addItemDecoration(ItemDecoration

Android开发中RecyclerView组件使用的一些进阶技讲解

RecyclerView的优势: 它自带ViewHolder来实现View的复用机制,再也不用ListView那样在getView()里自己写了 使用LayoutManager可以实现ListView,GridView以及流式布局的列表效果 通过setItemAnimator(ItemAnimator animator)可以实现增删动画(懒的话,可以使用默认的ItemAnimator对象,效果也不错) 控制item的间隔,可以使用addItemDecoration(ItemDecoration

分享Android开发中最有效率最快的循环代码

/* 1 ( 最快 ) */ for (int i = initializer; i >= 0; i--) { ... } /* 2 第二 */ int limit = calculateLoopLimit(); for (int i = 0; i < limit; i++) { ... } /* 3 */ Type[] array = getMyArray(); for (Type obj : array) { ... } /* 4 */ for (int i = 0; i < arr

代码-在Android开发中,怎样查找资源id?

问题描述 在Android开发中,怎样查找资源id? 例如以下代码: String[] data={"test1","test2","test3"}; ArrayAdapter adapter=new ArrayAdapter (MainActivity.this,an droid.R.layout.simple_dropdown_item_1line,data); 其中,android.R.layout.simple_dropdown_item

Android开发中使用sqlite实现新闻收藏和取消收藏的功能_Android

 之前学习oracle,简单的认为数据库只存在服务器端,学习安卓之后才发现原来android和Ios本身是"携带"数据库的--SQLite,是轻量级的.嵌入式的.关系型数据库,是Android.IOS等广泛使用的的数据库系统.用于存储本地的一直状态.刚写出来一个实现新闻收藏的功能,写出来供大家参考. 在Android中我们通过SQLiteDatabase这个类的对象操作SQLite数据库.由于SQLite数据库并不需要像C/S数据库那样建立连接以及身份验证的特性,以及SQLite数据库