美文网首页
蓝牙---Bluetooth

蓝牙---Bluetooth

作者: Otldan | 来源:发表于2017-11-22 14:18 被阅读0次

官方文档

Provides classes that manage Bluetooth functionality, such as scanning for devices, connecting with devices, and managing data transfer between devices. The Bluetooth API supports both "Classic Bluetooth" and Bluetooth Low Energy

提供管理蓝牙功能的类,例如对设备的扫描,连接设备,和管理设备之间的数据传输。蓝牙API支持经典蓝牙和低功耗蓝牙。

For more information about Classic Bluetooth, see the Bluetooth guide. For more information about Bluetooth Low Energy, see the Bluetooth Low Energy (BLE) guide.

对于更多关于经典蓝牙信息,请看蓝牙指南 对于更多关于蓝牙低耗,请查看蓝牙低耗指南


首先,要操作蓝牙,先要在AndroidManifest.xml里加入权限

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN">

<uses-permission android:name="android.permission.BLUETOOTH">

<uses-feature android:name="android.hardware.location.gps" />

6.0后需要动态获取权限

//判断是否有权限

if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

//请求权限

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);

//判断是否需要 向用户解释,为什么要申请该权限if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_CONTACTS)) {

Toast.makeText(this, "shouldShowRequestPermissionRationale", Toast.LENGTH_SHORT).show();}}



蓝牙基本操作

BluetoothAdapter ---蓝牙适配器,操作蓝牙,包括:启动蓝牙、查询、绑定设备等

获取本地蓝牙适配器

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

startDiscovery() ---开启扫描

cancelDiscovery () ---- 取消当前的设备发现过程。意思是关闭正在搜索

isEnable()---如果蓝牙当前已启用且可以使用,则返回true

disable()---关闭蓝牙

enable()---打开蓝牙,不会提示用户

if(!mBluetoothAdapter.isEnabled()){

//弹出对话框提示用户是后打开

Intent enabler =newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enabler, REQUEST_ENABLE);

//不做提示,直接打开,不建议用下面的方法,有的手机会有问题。

// mBluetoothAdapter.enable();

getAddress()---获取本地蓝牙地址

getBondedDevices()---返回绑定(配对)到本地适配器的一组BluetoothDevice 对象。

getDefaultAdapter()---获取蓝牙适配器

getName()---获取蓝牙名称

getRemoteDevice()---获取给定蓝牙硬件地址的BluetoothDevice对象。

getScanMode()---获取本地蓝牙适配器的当前蓝牙扫描模式。

getState()---获取本地蓝牙适配器的当前状态。

isDiscovering()---如果本地蓝牙适配器当前正在设备发现过程中,则返回true。

BluetoothDevice---远程蓝牙设备,获取 名称,地址,类别和绑定状态

createBond()----开始与远程设备的绑定(配对)过程。

createInsecureRfcommSocketToServiceRecord(UUIDuuid)---创建一个RFCOMMBluetoothSocket套接字,准备使用uuid的SDP查找来启动到此远程设备的不安全传出连接。

createRfcommSocketToServiceRecord(UUIDuuid)---使用SDU查找uuid,创建一个RFCOMMBluetoothSocket准备启动到此远程设备的安全传出连接。

getBluetoothClass()---获取远程设备的蓝牙类

getName()---获取远程设备的友好蓝牙名称

getType()---获取远程设备的蓝牙设备类型

getUuids()--返回远程设备的支持功能(UUID)

BluetoothSocket---连接蓝牙,线程安全

close()---关闭此流并释放与其关联的所有系统资源

connect()---尝试连接到远程设备

getInputStream()---获取输入流

getOutputStream()---获取输出流

getRemoteDeveice()---获取此套接字连接或连接到的远程设备

isConnected()---获取连接状态


相关文章

  • Android蓝牙权限

    1. 蓝牙权限 蓝牙连接和通讯需要获取相关的蓝牙权限BLUETOOTH和BLUETOOTH_ADMIN。 蓝牙权限...

  • 蓝牙开发入门

    蓝牙开发 相关资料: 蓝牙通过网站下载蓝牙开发套件 Bluetooth 和BLE 蓝牙(Bluetooth) 一种...

  • android蓝牙专题

    深入了解Android蓝牙Bluetooth ——《总结篇》 安卓蓝牙应用程序编写步骤 蓝牙 bluetooth-...

  • iOS蓝牙实现

    iOS 中使用 Core Bluetooth 框架实现蓝牙通信。Core Bluetooth 是基于蓝牙 4.0 ...

  • iOS 蓝牙开发 - 学习

    iOS 中使用 Core Bluetooth 框架实现蓝牙通信。Core Bluetooth 是基于蓝牙 4.0 ...

  • Android-蓝牙(Bluetooth)

    蓝牙(Bluetooth) 蓝牙(Bluetooth)是一种短距离的无线通信技术标准。 蓝牙协议 蓝牙协议分为4层...

  • 关于BLE需要知道的

    Bluetooth Low Energy 1、蓝牙协议 蓝牙( Bluetooth® )是一种无线技术标准,可实现...

  • iOS学习-蓝牙

    一、Core Bluetooth iOS中使用Core Bluetooth这个框架来进行蓝牙的开发支持蓝牙4.0,...

  • BLE蓝牙开发基础知识(一)

    1. Core Bluetooth 概述 BLE -> Bluetooth Low Energy (特指蓝牙4....

  • 蓝牙开发

    iOS蓝牙开发 Bluetooth蓝牙CoreBluetooth 蓝牙中心设备的实现 蓝牙外设的实现 有Demo ...

网友评论

      本文标题:蓝牙---Bluetooth

      本文链接:https://www.haomeiwen.com/subject/eieewttx.html