安卓蓝牙开发Android 蓝牙开发怎么监听连对了

安卓蓝牙开发  时间:2021-09-19  阅读:()

android蓝牙通信要怎么设计

Android平台支持蓝牙网络协议栈,实现蓝牙设备之间数据的无线传输。

本文档描述了怎样利用android平台提供的蓝牙API去实现蓝压设备之间的通信。

蓝牙具有point-to-point 和 multipoint两种连接功能。

  使用蓝牙API,可以做到:   * 搜索蓝牙设备   * 从本地的Bluetooth adapter中查询已经配对的设备   * 建立RFCOMM通道   * 通过service discovery连接到其它设备   * 在设备之间传输数据   * 管理多个连接   基础知识   本文档介绍了如何使用Android的蓝牙API来完成的四个必要的主要任务,使用蓝牙进行设备通信,主要包含四个部分:蓝牙设置、搜索设备(配对的或可见的)、连接、传输数据。

  所有的蓝牙API在android.bluetooth包中。

实现这些功能主要需要下面这几个类和接口:   BluetoothAdapter   代表本地蓝牙适配器(蓝牙发射器),是所有蓝牙交互的入口。

通过它可以搜索其它蓝牙设备,查询已经配对的设备列表,通过已知的MAC地址创建BluetoothDevice,创建BluetoothServerSocket监听来自其它设备的通信。

  BluetoothDevice   代表了一个远端的蓝牙设备, 使用它请求远端蓝牙设备连接或者获取 远端蓝牙设备的名称、地址、种类和绑定状态。

(其信息是封装在 bluetoothsocket 中) 。

  BluetoothSocket   代表了一个蓝牙套接字的接口(类似于 tcp 中的套接字) ,他是应用程 序通过输入、输出流与其他蓝牙设备通信的连接点。

  BluetoothServerSocket   代表打开服务连接来监听可能到来的连接请求 (属于 server 端) , 为了连接两个蓝牙设备必须有一个设备作为服务器打开一个服务套接字。

当远端设备发起连 接连接请求的时候,并且已经连接到了的时候,Blueboothserversocket 类将会返回一个 bluetoothsocket。

  BluetoothClass   描述了一个设备的特性(profile)或该设备上的蓝牙大致可以提供哪些服务(service),但不可信。

比如,设备是一个电话、计算机或手持设备;设备可以提供audio/telephony服务等。

可以用它来进行一些UI上的提示。

  BluetoothProfile   BluetoothHeadset   提供手机使用蓝牙耳机的支持。

这既包括蓝牙耳机和免提(V1.5)模式。

  BluetoothA2dp   定义高品质的音频,可以从一个设备传输到另一个蓝牙连接。

“A2DP的”代表高级音频分配模式。

  BluetoothHealth   代表了医疗设备配置代理控制的蓝牙服务   BluetoothHealthCallback   一个抽象类,使用实现BluetoothHealth回调。

你必须扩展这个类并实现回调方法接收更新应用程序的注册状态和蓝牙通道状态的变化。

  BluetoothHealthAppConfiguration   代表一个应用程序的配置,蓝牙医疗第三方应用注册与远程蓝牙医疗设备交流。

  BluetoothProfile.ServiceListener   当他们已经连接到或从服务断开时通知BluetoothProfile IPX的客户时一个接口(即运行一个特定的配置文件,内部服务)。

  蓝牙权限   为了在你的应用中使用蓝牙功能,至少要在AndroidManifest.xml中声明两个权限:BLUETOOTH(任何蓝牙相关API都要使用这个权限) 和 BLUETOOTH_ADMIN(设备搜索、蓝牙设置等)。

  为了执行蓝牙通信,例如连接请求,接收连接和传送数据都必须有BLUETOOTH权限。

  必须要求BLUETOOTH_ADMIN的权限来启动设备发现或操纵蓝牙设置。

大多数应用程序都需要这个权限能力,发现当地的蓝牙设备。

此权限授予其他的能力不应该使用,除非应用程序是一个“电源管理”,将根据用户要求修改的蓝牙设置   注释:要请求BLUETOOTH_ADMIN的话,必须要先有BLUETOOTH。

  在你的应用manifest 文件中声明蓝牙权限。

例如:         ...      通过查看资料来声明应用权限获取更多的信息。

  蓝牙设置   在你的应用通过蓝牙进行通信之前,你需要确认设备是否支持蓝牙,如果支持,确信它被打开。

  如果不支持,则不能使用蓝牙功能。

如果支持蓝牙,但不能够使用,你刚要在你的应用中请求使用蓝牙。

这个要两步完成,使用BluetoothAdapter。

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

下面是android蓝牙自动配对的代码 Mainfest,xml注册 自己在收到广播时处理并将预先输入的密码设置进去 public class BluetoothConnectActivityReceiver extends BroadcastReceiver { String strPsw = "0"; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (intent.getAction().equals( "android.bluetooth.device.action.PAIRING_REQUEST")) { BluetoothDevice btDevice = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234"); // device.setPin(pinBytes); Log.i("tag11111", "ddd"); try { ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手机和蓝牙采集器配对 ClsUtils.createBond(btDevice.getClass(), btDevice); ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /************************************ 蓝牙配对函数 * **************/ import java.lang.reflect.Field; import java.lang.reflect.Method; import android.bluetooth.BluetoothDevice; import android.util.Log; public class ClsUtils { /** * 与设备配对 参考源码:platform/packages/apps/Settings.git * /Settings//android/settings/bluetooth/CachedBluetoothDevice.java */ static public boolean createBond(Class btClass, BluetoothDevice btDevice) throws Exception { Method createBondMethod = btClass.getMethod("createBond"); Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice); return returnValue.booleanValue(); } /** * 与设备解除配对 参考源码:platform/packages/apps/Settings.git * /Settings//android/settings/bluetooth/CachedBluetoothDevice.java */ static public boolean removeBond(Class btClass, BluetoothDevice btDevice) throws Exception { Method removeBondMethod = btClass.getMethod("removeBond"); Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice); return returnValue.booleanValue(); } static public boolean setPin(Class btClass, BluetoothDevice btDevice, String str) throws Exception { try { Method removeBondMethod = btClass.getDeclaredMethod("setPin", new Class[] {byte[].class}); Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[] {str.getBytes()}); Log.e("returnValue", "" + returnValue); } catch (SecurityException e) { // throw new RuntimeException(e.getMessage()); e.printStackTrace(); } catch (IllegalArgumentException e) { // throw new RuntimeException(e.getMessage()); e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } // 取消用户输入 static public boolean cancelPairingUserInput(Class btClass, BluetoothDevice device) throws Exception { Method createBondMethod = btClass.getMethod("cancelPairingUserInput"); // cancelBondProcess() Boolean returnValue = (Boolean) createBondMethod.invoke(device); return returnValue.booleanValue(); } // 取消配对 static public boolean cancelBondProcess(Class btClass, BluetoothDevice device) throws Exception { Method createBondMethod = btClass.getMethod("cancelBondProcess"); Boolean returnValue = (Boolean) createBondMethod.invoke(device); return returnValue.booleanValue(); } /** * * @param clsShow */ static public void printAllInform(Class clsShow) { try { // 取得所有方法 Method[] hideMethod = clsShow.getMethods(); int i = 0; for (; i < hideMethod.length; i++) { Log.e("method name", hideMethod[i].getName() + ";and the i is:" + i); } // 取得所有常量 Field[] allFields = clsShow.getFields(); for (i = 0; i < allFields.length; i++) { Log.e("Field name", allFields[i].getName()); } } catch (SecurityException e) { // throw new RuntimeException(e.getMessage()); e.printStackTrace(); } catch (IllegalArgumentException e) { // throw new RuntimeException(e.getMessage()); e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 执行时直接使用: public static boolean pair(String strAddr, String strPsw) { boolean result = false; BluetoothAdapter bluetoothAdapter = BluetoothAdapter .getDefaultAdapter(); bluetoothAdapter.cancelDiscovery(); if (!bluetoothAdapter.isEnabled()) { bluetoothAdapter.enable(); } if (!BluetoothAdapter.checkBluetoothAddress(strAddr)) { // 检查蓝牙地址是否有效 Log.d("mylog", "devAdd un effient!"); } BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { try { Log.d("mylog", "NOT BOND_BONDED"); ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对 ClsUtils.createBond(device.getClass(), device); remoteDevice = device; // 配对完毕就把这个设备对象传给全局的remoteDevice result = true; } catch (Exception e) { // TODO Auto-generated catch block Log.d("mylog", "setPiN failed!"); e.printStackTrace(); } // } else { Log.d("mylog", "HAS BOND_BONDED"); try { ClsUtils.createBond(device.getClass(), device); ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对 ClsUtils.createBond(device.getClass(), device); remoteDevice = device; // 如果绑定成功,就直接把这个设备对象传给全局的remoteDevice result = true; } catch (Exception e) { // TODO Auto-generated catch block Log.d("mylog", "setPiN failed!"); e.printStackTrace(); } } return result; }

Android蓝牙开发相关:一个客户端、服务器端都可以当的app,应该在什么时候开启我的服务器端的线程?

服务端的需要一直开着,等待客户端的想要连接时自己来连接,看起来有些像钓鱼的样子~ 蓝牙的话是这样,服务端一直开着,并把服务端所在的机子设置成可以被其他机子搜索,然后就等待其他机子的配对请求,若果在可被搜索的时间内内有其他机子访问,只能再次启动配对监听,直到配对成功为止

Android 蓝牙开发怎么监听连对了

BlueboothServerSocket类:代表打开服务连接来监听可能到来的连接请求(属于server端), 为了连接两个蓝牙设备必须有一个设备作为服务器打开一个服务套接字。

当远端设备发起连接连接请求的时候,并且已经连接到了的时候,BlueboothServerSocket类将会返回一个BluetoothSocket。

青果网络618:洛杉矶CN2 GIA/东京CN2套餐年付199元起,国内高防独服套餐66折

青果网络怎么样?青果网络隶属于泉州市青果网络科技有限公司,青果网络商家成立于2015年4月1日,拥有工信部颁发的全网IDC/ISP/IP-VPN资质,是国内为数不多具有IDC/ISP双资质的综合型云计算服务商。青果网络是APNIC和CNNIC地址分配联盟成员,泉州市互联网协会会员单位,信誉非常有保障。目前,青果网络商家正式开启了618云特惠活动,针对国内外机房都有相应的优惠。点击进入:青果网络官方...

欧路云(22元) 新增美国Cera线路VPS主机且可全场8折

欧路云(oulucloud) 商家在前面的文章中也有陆续介绍过几次,这不今天有看到商家新增加美国Cera线路的VPS主机,而且有提供全场八折优惠。按照最低套餐最低配置的折扣,月付VPS主机低至22元,还是比较便宜的。不过我们需要注意的是,欧路云是一家2021年新成立的国人主机商,据说是由深圳和香港的几名大佬创建。如果我们有介意新商家的话,选择的时候谨慎且月付即可,注意数据备份。商家目前主营高防VP...

CloudCone中国新年特别套餐,洛杉矶1G内存VPS年付13.5美元起

CloudCone针对中国农历新年推出了几款特别套餐, 其中2019年前注册的用户可以以13.5美元/年的价格购买一款1G内存特价套餐,以及另外提供了两款不限制注册时间的用户可购买年付套餐。CloudCone是Quadcone旗下成立于2017年的子品牌,提供VPS及独立服务器租用,也是较早提供按小时计费VPS的商家之一,支持使用PayPal或者支付宝等付款方式。下面列出几款特别套餐配置信息。CP...

安卓蓝牙开发为你推荐
智能工单系统智能供电系统方案怎么做胶南建管网怎样在胶南信息港注册账号?亿图网亿图怎么加背景,不是插入图片那种,是要把任意图片变成背景、、、声源定位产生视觉、听觉产生的部位是(  ) A.视网膜;耳蜗 B.大脑皮层;大脑皮层 C.晶状体;听小骨深圳erp请问深圳值得信赖的ERP公司都要哪些?暴风播酷云暴风影音视频播放器 暴风影音万能播放器充电宝摄像机移动电源如何接入摄像机寻找手机查找手机是什么功能?怎么使用?oa地址连接超时,请检查oa地址和网络环境亚网谁知道什么是软式网球?
武汉域名注册 合租服务器 景安vps 香港ufo justhost 免费个人博客 国外空间 大容量存储 空间服务商 500m空间 777te 193邮箱 河南m值兑换 秒杀汇 国外代理服务器地址 河南移动网 河南移动m值兑换 卡巴斯基是免费的吗 国外视频网站有哪些 七夕快乐英语 更多