当前位置:首页 » 安卓系统 » android43蓝牙40

android43蓝牙40

发布时间: 2022-12-07 10:26:50

Ⅰ android中蓝牙2.0和4.0的区别是什么

最主要的区别就是蓝牙2.0的传输速度没有蓝牙4.0快。以下为蓝牙各版本的说明。x0dx0ax0dx0a1.1 为最早期版本,传输率约在748~810kb/s,因是早期设计,容易受到同频率之产品所干扰下影响通讯质量。x0dx0a蓝牙1.2标准x0dx0a1.2 同样是只有 748~810kb/s 的传输率,但在加上了(改善 Software)抗干扰跳频功能。x0dx0a蓝牙2.0标准x0dx0a2.0 是 1.2 的改良提升版,传输率约在 1.8M/s~2.1M/s,开始支持双工模式——即一面作语音通讯,同时亦可以传输档案/高质素图片,2.0 版本当然也支持 Stereo 运作。x0dx0a应用最为广泛的是Bluetooth 2.0+EDR标准,该标准在2004年已经推出,支持Bluetooth 2.0+EDR标准的产品也于2006年大量出现。x0dx0a虽然Bluetooth 2.0+EDR标准在技术上作了大量的改进,但从1.X标准延续下来的配置流程复杂和设备功耗较大的问题依然存在。x0dx0a蓝牙2.1标准x0dx0a2007年8月2日,蓝牙技术联盟今天正式批准了蓝牙2.1版规范,即“蓝牙2.1+EDR”,可供未来的设备自由使用。和2.0版本同时代产品,目前仍然占据蓝牙市场较大份额,相对2.0版本主要是提高了待机时间2倍以上,技术标准没有根本性变化。x0dx0a蓝牙3.0标准x0dx0a2009年4月21日,蓝牙技术联盟(Bluetooth SIG)正式颁布了新一代标准规范"Bluetooth Core Specification Version 3.0 High Speed"(蓝牙核心规范3.0版 ),蓝牙3.0的核心是"Generic Alternate MAC/PHY"(AMP),这是一种全新的交替射频技术,允许蓝牙协议栈针对任一任务动态地选择正确射频。x0dx0a蓝牙3.0的数据传输率提高到了大约24Mbps(即可在需要的时候调用802.11 WI-FI用于实现高速数据传输)。在传输速度上,蓝牙3.0是蓝牙2.0的八倍,可以轻松用于录像机至高清电视、PC至PMP、UMPC至打印机之间的资料传输,但是需要双方都达到此标准才能实现功能。x0dx0a蓝牙4.0标准x0dx0a蓝牙4.0规范于2010年7月7日正式发布,新版本的最大意义在于低功耗,同时加强不同OEM厂商之间的设备兼容性,并且降低延迟,理论最高传输速度依然为24Mbps(即3MB/s),有效覆盖范围扩大到100米(之前的版本为10米)。该标准芯片被大量的手机、平板所采用,如苹果The New iPad平板电脑,以及苹果iPhone 5、魅族MX4、HTC One X等手机上带有蓝牙4.0功能。x0dx0a蓝牙4.1标准x0dx0a蓝牙4.1于2013年12月6日发布,与LTE无线电信号之间如果同时传输数据,那么蓝牙4.1可以自动协调两者的传输信息,理论上可以减少 其它信号对蓝牙4.1的干扰。改进是提升了连接速度并且更加智能化,比如减少了设备之间重新连接的时间,意味着用户如果走出了蓝牙4.1的信号范围并且断开连接的时间不算很长,当用户再次回到信号范围中之后设备将自动连接,反应时间要比蓝牙4.0更短。最后一个改进之处是提高传输效率,如果用户连接的设备 非常多,比如连接了多部可穿戴设备,彼此之间的信息都能即时发送到接接收设备上。x0dx0a除此之外,蓝牙4.1也为开发人员增加了更多的灵活性,这个改变对普通用户没有很大影响,但是对于软件开发者来说是很重要的,因为为了应对逐渐兴起的可穿戴设备,那么蓝牙必须能够支持同时连接多部设备。x0dx0a目前支持该标准的手机还比较少,三星GALAXY Note4则是其中具有代表性的一款。x0dx0a蓝牙4.2标准x0dx0a2014年12月4日,最新的蓝牙4.2标准颁布,改善了数据传输速度和隐私保护程度,并接入了该设备将可直接通过IPv6和6LoWPAN接入互联网。在新的标准下蓝牙信号想要连接或者追踪用户设备必须经过用户许可,否则蓝牙信号将无法连接和追踪用户设备。x0dx0a速度方面变得更加快速,两部蓝牙设备之间的数据传输速度提高了2.5倍,因为蓝牙智能(Bluetooth Smart)数据包的容量提高,其可容纳的数据量相当于此前的10倍左右。蓝牙的版本自然是越高级越好,考虑到传输距离和功耗的问题,最新的蓝牙4.1是优选,但是目前市场上蓝牙4.1的产品并不多,而主流的蓝牙4.0产品性价比更高,至于蓝牙3.0、2.1及以下的版本已经失去选购的价值。

Ⅱ Android手机蓝牙不能用怎么办

Android手机蓝牙不能用,可重启一次手机,蓝牙硬件会在重启过程中会自动被重置。重启后蓝牙功能就可以正常启动了。

Ⅲ 蓝牙错误代码43,怎么解决

原因:

1、驱动没有安装正确,(安装了不匹配的驱动文件,没有按照程序要求完成驱动安装过程,意外重启);

2、内存冲突(这是由于安装的驱动的兼容性导致的);

3、操作系统文件损坏;

4、硬件故障。

解决方法:

1、如果是驱动故障,需要卸载驱动程序,删除驱动文件,否则重复安装会无效。

2、重新安装操作系统,使用计算机自带的驱动光盘(这个首先要看是否支持WIN7),没有光盘,到对应的官网下载即可。重新安装驱动程序和驱动文件。按照安装要求,重启计算机。

(3)android43蓝牙40扩展阅读

蓝牙各种版本的区别

蓝牙1.0 :传输速率约1M/s。

蓝牙2.0+EDR:传输速率约2-3M/s,其中2.1+EDR是最经典的蓝牙,最大的特点是安全简易配对。

蓝牙3.0+HS(高传输蓝牙):高传输24M/s,只有标注了"+HS"商标的设备才是真正支持802.11高速数据传输。

蓝牙4.0(低功耗蓝牙):它包括经典蓝牙、高速蓝牙和蓝牙低功耗协议,在3.0基础上功耗更低,主要面向对功耗需求极低、用纽扣电池供电的应用。其中4.1增加了物联网特性,支持批量数据交换率共存,4.2的最大特性是可以让多个蓝牙智能设备通过一个终端接入局域网或互联网。

Ⅳ Android开发之蓝牙(Bluetooth)

在上一篇中有介绍了Wifi与网络连接处理
Android开发之WiFi与网络连接处理
下面,来继续说说Android中蓝牙的基本使用。

Bluetooth是目前使用的最广泛的无线通讯协议之一,主要针对短距离设备通讯(10米),常用于连接耳机、鼠标和移动通讯设备等。

值得一提的是:
android4.2新增了部分新功能,但是对于Bluetooth熟悉的人或许开始头疼了,那就是Android4.2引入了一个新的蓝牙协议栈针BLE。谷歌和Broadcom之间的合作,开发新的蓝牙协议栈,取代了基于堆栈的Bluez。因此市场上出现了老设备的兼容问题,很多蓝牙设备在android4.2手机上不能正常使用。

BluetoothAdapter简单点来说就是代表了本设备(手机、电脑等)的蓝牙适配器对象。

first:we need permission
要操作蓝牙,先要在AndroidManifest.xml里加入权限

**下面来看看如何使用蓝牙。 **↓↓↓****
Demo已就绪:

返回值:如果设备具备蓝牙功能,返回BluetoothAdapter 实例;否则,返回null对象。

打开蓝牙设备的方式:
1.直接调用函数enable()去打开蓝牙设备 ;
2.系统API去打开蓝牙设备,该方式会弹出一个对话框样式的Activity供用户选择是否打开蓝牙设备。

注意: 1.如果蓝牙已经开启,不会弹出该Activity界面。2.在目前大多数Android手机中,是不支持在飞行模式下开启蓝牙的。如果蓝牙已经开启,那么蓝牙的开关 ,状态会随着飞行模式的状态而发生改变。

1. 搜索蓝牙设备
使用BluetoothAdapter的startDiscovery()方法来搜索蓝牙设备
startDiscovery()方法是一个异步方法,调用后会立即返回。该方法会进行对其他蓝牙设备的搜索,该过程会持续12秒。该方法调用后,搜索过程实际上是在一个System Service中进行的,所以可以调用cancelDiscovery()方法来停止搜索(该方法可以在未执行discovery请求时调用)。

系统开始搜索蓝牙设备
^( *  ̄(oo) ̄ ) ^ 系统会发送以下三个广播:

2.扫描设备

3.定义广播接收器接收搜索结果

4.注册广播

获取附近的蓝牙设备

第一步建立连接:首先Android sdk(2.0以上版本)支持的蓝牙连接是通过BluetoothSocket建立连接,服务端BluetoothServerSocket和客户端(BluetoothSocket)需指定同样的UUID,才能建立连接,因为建立连接的方法会阻塞线程,所以服务器端和客户端都应启动新线程连接。

(这里的服务端和客户端是相对来说的)
两个蓝牙设备之间的连接,则必须实现服务端与客户端的机制。
当两个设备在同一个RFCOMM channel下分别拥有一个连接的BluetoothSocket,这两个设备才可以说是建立了连接。

服务端设备与客户端设备获取BluetoothSocket的途径是不同的。
1,服务端设备是通过accepted一个incoming connection来获取的,
2,客户端设备则是通过打开一个到服务端的RFCOMM channel来获取的。

服务端
通过调用BluetoothAdapter的(String, UUID)方法来获取BluetoothServerSocket(UUID用于客户端与服务端之间的配对)

客户端
调用BluetoothService的(UUID)方法获取BluetoothSocket(该UUID应该同于服务端的UUID)。
调用BluetoothSocket的connect()方法(该方法为block方法),如果UUID同服务端的UUID匹配,并且连接被服务端accept,则connect()方法返回。

数据传递,通过以上操作,就已经建立的BluetoothSocket连接了,数据传递无非是通过流的形式
获取流

该类就是关于远程蓝牙设备的一个描述。通过它可以和本地蓝牙设备---BluetoothAdapter连接通信。

好多东西我也不知道怎么描述,下面给出Demo:
刚好有刚学习的小伙伴问我ListView怎么用,那我就用ListView。

源码
RairDemo
GitHub: https://github.com/Rairmmd/android-demo
Coding: https://coding.net/u/Rair/p/RairDemo/git

Ⅳ Android中如何实现蓝牙的配对与连接

蓝牙功能可以参考下面的操作打开使用:
1.打开其他设备的蓝牙,并使其对其他设备可见。
2.打开下拉顶帘,点击蓝牙图标使其变为绿色,跳出提示框,勾选对其他设备可见。
3.点击扫描,搜索到其他设备后,点击该设备名称,双方点确定后配对成功。
4.选择要传输的文件,共享通过蓝牙即可传输文件。

Ⅵ 如何使用android原生BLE蓝牙进行操作

之前的涉及的物联网项目中使用的: BLE 低功耗蓝牙(蓝牙4.0), 支持android 4.3以上的手机
主从关系: BLE低功耗蓝牙只能做从端设备 ,一个蓝牙主端设备,可同时与7个蓝牙从端设备进行通讯

1)低功耗
低功耗的原理:
1低功耗蓝牙仅使用了3个广播通道,传统蓝牙技术采用 16~32 个频道
2每次广播开启时间也由传统的 22.5ms 减少到 0.6~1.2ms(毫秒)

2)传输距离极大提高
传统蓝牙传输距离为 2~10m,而蓝牙4.0的有效传输距离可达到 60~100m

3)安全性
使用AES-128 CCM加密算法进行数据包加密和认证。
更多BLE蓝牙的解析参考博客 : BLE4.0教程一 蓝牙协议连接过程与广播分析

添加权限
打开蓝牙
1.先拿到BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
2.再拿到BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
判断是否打开蓝牙
未打开弹出 系统弹框 ,除了 魅族手机 是打开系统设置

设备/手机都是蓝牙信号

在回调方法中:

一般在扫描的过程中,我们还会设置 设备过滤原则 (因为我只想要搜索到我们想要的设备,忽略无关设备)
如:从 scanRecord -- beacon -- beacon.type == 0xFF代表Manufacture,通过与嵌入式软件定义 自己的 Manufacture值即可

用BluetoothDevice得到BluetoothGatt:

断连:

关键问题:连接后一般要做什么事?

( 必须在刚连接成功后2秒内app写一个值给设备,否则会被设备断开连接)

主要是读写 characteristic
gatt.wirteCharacteristic(mCurrentcharacteristic);

gatt.readCharacteristic(characteristic);

bluetoothGatt.setCharacteristicNotification(data, true);

真实工作中使用的蓝牙库BlueToothKit请参考我的另一篇博客:
android蓝牙入门知识和优秀蓝牙第三方库BluetoothKit的使用

Ⅶ 如何让android 蓝牙连接之后断开,再自动尝试连接

在源码 BluetoothDevice 类中还有两个隐藏方法 cancelBondProcess()和cancelPairingUserInput() 这两个方法一个是取消配对进程一个是取消用户输入 下面是自动配对的代码 Mainfest,xml注册 1 <</code>receiver android:name="." > 2 <</code>intent-filter> 3 <</code>action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /> 4 </</code>intent-filter> 5 </</code>receiver> 自己在收到广播时处理并将预先输入的密码设置进去 01 public class extends BroadcastReceiver 02 { 03 04 String strPsw = "0"; 05 06 @Override 07 public void onReceive(Context context, Intent intent) 08 { 09 // TODO Auto-generated method stub 10 if (intent.getAction().equals( 11 "android.bluetooth.device.action.PAIRING_REQUEST")) 12 { 13 BluetoothDevice btDevice = intent 14 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 15 16 // byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234"); 17 // device.setPin(pinBytes); 18 Log.i("tag11111", "ddd"); 19 try 20 { 21 ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手机和蓝牙采集器配对 22 ClsUtils.createBond(btDevice.getClass(), btDevice); 23 ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice); 24 } 25 catch (Exception e) 26 { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30 } 31 32 33 } 34 } 001 002 import java.lang.reflect.Field; 003 import java.lang.reflect.Method; 004 005 import android.bluetooth.BluetoothDevice; 006 import android.util.Log; 007 public class ClsUtils 008 { 009 010 014 static public boolean createBond(Class btClass, BluetoothDevice btDevice) 015 throws Exception 016 { 017 Method createBondMethod = btClass.getMethod("createBond"); 018 Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice); 019 return returnValue.booleanValue(); 020 } 021 022 026 static public boolean removeBond(Class btClass, BluetoothDevice btDevice) 027 throws Exception 028 { 029 Method removeBondMethod = btClass.getMethod("removeBond"); 030 Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice); 031 return returnValue.booleanValue(); 032 } 033 034 static public boolean setPin(Class btClass, BluetoothDevice btDevice, 035 String str) throws Exception 036 { 037 try 038 { 039 Method removeBondMethod = btClass.getDeclaredMethod("setPin", 040 new Class[] 041 {byte[].class}); 042 Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, 043 new Object[] 044 {str.getBytes()}); 045 Log.e("returnValue", "" + returnValue); 046 } 047 catch (SecurityException e) 048 { 049 // throw new RuntimeException(e.getMessage()); 050 e.printStackTrace(); 051 } 052 catch (IllegalArgumentException e) 053 { 054 // throw new RuntimeException(e.getMessage()); 055 e.printStackTrace(); 056 } 057 catch (Exception e) 058 { 059 // TODO Auto-generated catch block 060 e.printStackTrace(); 061 } 062 return true; 063 064 } 065 066 // 取消用户输入 067 static public boolean cancelPairingUserInput(Class btClass, 068 BluetoothDevice device) 069 070 throws Exception 071 { 072 Method createBondMethod = btClass.getMethod("cancelPairingUserInput"); 073 // cancelBondProcess() 074 Boolean returnValue = (Boolean) createBondMethod.invoke(device); 075 return returnValue.booleanValue(); 076 } 077 078 // 取消配对 079 static public boolean cancelBondProcess(Class btClass, 080 BluetoothDevice device) 081 082 throws Exception 083 { 084 Method createBondMethod = btClass.getMethod("cancelBondProcess"); 085 Boolean returnValue = (Boolean) createBondMethod.invoke(device); 086 return returnValue.booleanValue(); 087 } 088 089 093 static public void printAllInform(Class clsShow) 094 { 095 try 096 { 097 // 取得所有方法 098 Method[] hideMethod = clsShow.getMethods(); 099 int i = 0; 100 for (; i < hideMethod.length; i++) 101 { 102 Log.e("method name", hideMethod[i].getName() + ";and the i is:" 103 + i); 104 } 105 // 取得所有常量 106 Field[] allFields = clsShow.getFields(); 107 for (i = 0; i < allFields.length; i++) 108 { 109 Log.e("Field name", allFields[i].getName()); 110 } 111 } 112 catch (SecurityException e) 113 { 114 // throw new RuntimeException(e.getMessage()); 115 e.printStackTrace(); 116 } 117 catch (IllegalArgumentException e) 118 { 119 // throw new RuntimeException(e.getMessage()); 120 e.printStackTrace(); 121 } 122 catch (Exception e) 123 { 124 // TODO Auto-generated catch block 125 e.printStackTrace(); 126 } 127 } 128 } 执行时直接使用: 01 public static boolean pair(String strAddr, String strPsw) 02 { 03 boolean result = false; 04 BluetoothAdapter bluetoothAdapter = BluetoothAdapter 05 .getDefaultAdapter(); 06 07 bluetoothAdapter.cancelDiscovery(); 08 09 if (!bluetoothAdapter.isEnabled()) 10 { 11 bluetoothAdapter.enable(); 12 } 13 14 if (!BluetoothAdapter.checkBluetoothAddress(strAddr)) 15 { // 检查蓝牙地址是否有效 16 17 Log.d("mylog", "devAdd un effient!"); 18 } 19 20 BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr); 21 22 if (device.getBondState() != BluetoothDevice.BOND_BONDED) 23 { 24 try 25 { 26 Log.d("mylog", "NOT BOND_BONDED"); 27 ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对 28 ClsUtils.createBond(device.getClass(), device); 29 remoteDevice = device; // 配对完毕就把这个设备对象传给全局的remoteDevice 30 result = true; 31 } 32 catch (Exception e) 33 { 34 // TODO Auto-generated catch block 35 36 Log.d("mylog", "setPiN failed!"); 37 e.printStackTrace(); 38 } // 39 40 } 41 else 42 { 43 Log.d("mylog", "HAS BOND_BONDED"); 44 try 45 { 46 ClsUtils.createBond(device.getClass(), device); 47 ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对 48 ClsUtils.createBond(device.getClass(), device); 49 remoteDevice = device; // 如果绑定成功,就直接把这个设备对象传给全局的remoteDevice 50 result = true; 51 } 52 catch (Exception e) 53 { 54 // TODO Auto-generated catch block 55 Log.d("mylog", "setPiN failed!"); 56 e.printStackTrace(); 57 } 58 } 59 return result; 60 }

Ⅷ android 怎么连接蓝牙设备

先展示代码结构
步骤阅读
2
连接蓝牙类
类名:MainActivity(有点偷懒,没有起表意的类名。)
步骤阅读
3
要声明的控件和变量等
步骤阅读
4
在onCreate声明控件
步骤阅读
5
ToogleButton设置开关状态
声明一个组件愿意接收
IntentFilter intent = new IntentFilter();
步骤阅读
6
BroadcastReceiver广播接收器
步骤阅读
步骤阅读
7
listview点击事件
OnItemClickListener

OnClickListener
步骤阅读
步骤阅读
8
蓝牙连接
步骤阅读
9
退出消耗页面是的onDestroy()
步骤阅读
10
布局结构图
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.bluetooth_connection.MainActivity" >
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="horizontal" >
<Button android:id="@+id/btnSearch" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="搜索" android:layout_weight="1" />
<Button android:id="@+id/btnExit" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="退出" android:layout_weight="1" />
<Button android:id="@+id/btnDis" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="开启蓝牙" android:layout_weight="1" /> </LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" >
<ToggleButton android:id="@+id/tbtnSwitch" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" android:textOff="关闭蓝牙" android:textOn="开启蓝牙" /> </LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.3" android:orientation="vertical" >
<ListView android:id="@+id/lvDevices" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#f1f1f1" android:cacheColorHint="#ff333333" android:fadingEdge="none" android:scrollbars="none" > </ListView> </LinearLayout> </LinearLayout>
</RelativeLayout>

步骤阅读
11
界面效果

步骤阅读

12
例子如下

Ⅸ 如何实现android蓝牙自动配对连接

之前做一个Android版的蓝牙 与血压计通讯的项目,遇到最大的难题就是自动配对.
上网查资料说是用反射createBond()和setPin(),但测试时进行配对还是会出现提示,但配对是成功了
我就开始查找怎么关闭这个蓝牙配对提示框,后面还是伟大的android源码帮助了我。
在源码 BluetoothDevice 类中还有两个隐藏方法
cancelBondProcess()和cancelPairingUserInput()
这两个方法一个是取消配对进程一个是取消用户输入
下面是自动配对的代码
Mainfest,xml注册

1 <receiver android:name="." >
2 <intent-filter>
3 <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
4 </intent-filter>
5 </receiver>
自己在收到广播时处理并将预先输入的密码设置进去

01 public class extends BroadcastReceiver
02 {
03
04 String strPsw = "0";
05
06 @Override
07 public void onReceive(Context context, Intent intent)
08 {
09 // TODO Auto-generated method stub
10 if (intent.getAction().equals(
11 "android.bluetooth.device.action.PAIRING_REQUEST"))
12 {
13 BluetoothDevice btDevice = intent
14 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
15
16 // byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");
17 // device.setPin(pinBytes);
18 Log.i("tag11111", "ddd");
19 try
20 {
21 ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手机和蓝牙采集器配对
22 ClsUtils.createBond(btDevice.getClass(), btDevice);
23 ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice);
24 }
25 catch (Exception e)
26 {
27 // TODO Auto-generated catch block
28 e.printStackTrace();
29 }
30 }
31
32
33 }
34 }

001 <b>/************************************ 蓝牙配对函数 * **************/
002 import java.lang.reflect.Field;
003 import java.lang.reflect.Method;
004
005 import android.bluetooth.BluetoothDevice;
006 import android.util.Log;
007 public class ClsUtils
008 {
009
010 /**
011 * 与设备配对 参考源码:platform/packages/apps/Settings.git
012 * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
013 */
014 static public boolean createBond(Class btClass, BluetoothDevice btDevice)
015 throws Exception
016 {
017 Method createBondMethod = btClass.getMethod("createBond");
018 Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
019 return returnValue.booleanValue();
020 }
021
022 /**
023 * 与设备解除配对 参考源码:platform/packages/apps/Settings.git
024 * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
025 */
026 static public boolean removeBond(Class btClass, BluetoothDevice btDevice)
027 throws Exception
028 {
029 Method removeBondMethod = btClass.getMethod("removeBond");
030 Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);
031 return returnValue.booleanValue();
032 }
033
034 static public boolean setPin(Class btClass, BluetoothDevice btDevice,
035 String str) throws Exception
036 {
037 try
038 {
039 Method removeBondMethod = btClass.getDeclaredMethod("setPin",
040 new Class[]
041 {byte[].class});
042 Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice,
043 new Object[]
044 {str.getBytes()});
045 Log.e("returnValue", "" + returnValue);
046 }
047 catch (SecurityException e)
048 {
049 // throw new RuntimeException(e.getMessage());
050 e.printStackTrace();
051 }
052 catch (IllegalArgumentException e)
053 {
054 // throw new RuntimeException(e.getMessage());
055 e.printStackTrace();
056 }
057 catch (Exception e)
058 {
059 // TODO Auto-generated catch block
060 e.printStackTrace();
061 }
062 return true;
063
064 }
065
066 // 取消用户输入
067 static public boolean cancelPairingUserInput(Class btClass,
068 BluetoothDevice device)
069
070 throws Exception
071 {
072 Method createBondMethod = btClass.getMethod("cancelPairingUserInput");
073 // cancelBondProcess()
074 Boolean returnValue = (Boolean) createBondMethod.invoke(device);
075 return returnValue.booleanValue();
076 }
077
078 // 取消配对
079 static public boolean cancelBondProcess(Class btClass,
080 BluetoothDevice device)
081
082 throws Exception
083 {
084 Method createBondMethod = btClass.getMethod("cancelBondProcess");
085 Boolean returnValue = (Boolean) createBondMethod.invoke(device);
086 return returnValue.booleanValue();
087 }
088
089 /**
090 *
091 * @param clsShow
092 */
093 static public void printAllInform(Class clsShow)
094 {
095 try
096 {
097 // 取得所有方法
098 Method[] hideMethod = clsShow.getMethods();
099 int i = 0;
100 for (; i < hideMethod.length; i++)
101 {
102 Log.e("method name", hideMethod[i].getName() + ";and the i is:"
103 + i);
104 }
105 // 取得所有常量
106 Field[] allFields = clsShow.getFields();
107 for (i = 0; i < allFields.length; i++)
108 {
109 Log.e("Field name", allFields[i].getName());
110 }
111 }
112 catch (SecurityException e)
113 {
114 // throw new RuntimeException(e.getMessage());
115 e.printStackTrace();
116 }
117 catch (IllegalArgumentException e)
118 {
119 // throw new RuntimeException(e.getMessage());
120 e.printStackTrace();
121 }
122 catch (Exception e)
123 {
124 // TODO Auto-generated catch block
125 e.printStackTrace();
126 }
127 }
128 }</b>
执行时直接使用:

view sourceprint?
01 <b>public static boolean pair(String strAddr, String strPsw)
02 {
03 boolean result = false;
04 BluetoothAdapter bluetoothAdapter = BluetoothAdapter
05 .getDefaultAdapter();
06
07 bluetoothAdapter.cancelDiscovery();
08
09 if (!bluetoothAdapter.isEnabled())
10 {
11 bluetoothAdapter.enable();
12 }
13
14 if (!BluetoothAdapter.checkBluetoothAddress(strAddr))
15 { // 检查蓝牙地址是否有效
16
17 Log.d("mylog", "devAdd un effient!");
18 }
19
20 BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr);
21
22 if (device.getBondState() != BluetoothDevice.BOND_BONDED)
23 {
24 try
25 {
26 Log.d("mylog", "NOT BOND_BONDED");
27 ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对
28 ClsUtils.createBond(device.getClass(), device);
29 remoteDevice = device; // 配对完毕就把这个设备对象传给全局的remoteDevice
30 result = true;
31 }
32 catch (Exception e)
33 {
34 // TODO Auto-generated catch block
35
36 Log.d("mylog", "setPiN failed!");
37 e.printStackTrace();
38 } //
39
40 }
41 else
42 {
43 Log.d("mylog", "HAS BOND_BONDED");
44 try
45 {
46 ClsUtils.createBond(device.getClass(), device);
47 ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对
48 ClsUtils.createBond(device.getClass(), device);
49 remoteDevice = device; // 如果绑定成功,就直接把这个设备对象传给全局的remoteDevice
50 result = true;
51 }
52 catch (Exception e)
53 {
54 // TODO Auto-generated catch block
55 Log.d("mylog", "setPiN failed!");
56 e.printStackTrace();
57 }
58 }
59 return result;
60 }</b>

Ⅹ Android开发 蓝牙连接问题

Android 蓝牙编程的基本步骤:
1.获取蓝牙适配器BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter();
如果BluetoothAdapter 为null,说明android手机没有蓝牙模块。
判断蓝牙模块是否开启,blueadapter.isEnabled() true表示已经开启,false表示蓝牙并没启用。
2.启动配置蓝牙可见模式,即进入可配对模式Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);
startActivity(in); ,200就表示200秒。
3.获取蓝牙适配器中已经配对的设备Set<BluetoothDevice> device=blueadapter.getBondedDevices();
4.还需要在androidManifest.xml中声明蓝牙的权限
<uses-permission android:name="android.permission.BLUETOOTH" />

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
接下来就是根据自己的需求对BluetoothAdapter 的操作了。

热点内容
安卓手机用什么软件做特效 发布:2025-03-14 21:50:01 浏览:396
gta5怎么把配置调低 发布:2025-03-14 21:50:00 浏览:719
解压缩所有的 发布:2025-03-14 21:48:34 浏览:523
安卓怎么在屏保记录恋爱时间 发布:2025-03-14 21:47:41 浏览:765
java获取地址 发布:2025-03-14 21:44:30 浏览:730
app后台源码 发布:2025-03-14 21:33:34 浏览:735
cftp目录是否存在 发布:2025-03-14 21:32:45 浏览:98
我的世界斗罗服务器电脑网易版 发布:2025-03-14 21:28:33 浏览:525
java是甲骨文 发布:2025-03-14 21:21:38 浏览:127
柱顶要加密 发布:2025-03-14 21:16:11 浏览:854