Android 蓝牙开发之搜索、配对、连接、通信大全

 

        蓝牙( Bluetooth®):是一种无线技术标准,可实现固定设备、移动设备和楼宇个人域网之间的短距离数据

交换(使用2.4—2.485GHz的ISM波段的UHF无线电波)。蓝牙设备最多可以同时和7个其它蓝牙设备建立连接,进

行通信,当然并不是每一个蓝牙都可以达到最大值。下面,我们从蓝牙的基本概念开始,一步一步开始了解蓝牙。

 

(尊重劳动成果,转载请注明出处http://blog.csdn.net/qq_25827845/article/details/52997523

 源码下载地址:http://download.csdn.net/download/qq_25827845/9757403


基本概念: 

         安卓平台提供对蓝牙的通讯栈的支持,允许设别和其他的设备进行无线传输数据。应用程序层通过安卓API来调用蓝牙的相关功

能,这些API使程序无线连接到蓝牙设备,并拥有P2P或者多端无线连接的特性。

 

蓝牙的功能:

1、扫描其他蓝牙设备

2、为可配对的蓝牙设备查询蓝牙适配器

3、建立RFCOMM通道

4、通过服务搜索来链接其他的设备

5、与其他的设备进行数据传输

6、管理多个连接



蓝牙建立连接必须要求:

1、打开蓝牙

2、查找附近已配对或可用设备

3、连接设备

4、设备间数据交换

 

 

常用的蓝牙API如下:

 

BluetoothAdapter

代表本地蓝牙适配器(蓝牙无线电)。BluetoothAdapter是所有蓝牙交互的入口。使用这个你可以发现其他蓝牙设备,查询已配对的设备列表,使用一个已知的MAC地址来实例化一个BluetoothDevice,以及创建一个BluetoothServerSocket来为监听与其他设备的通信。

 

BlueDevice 代表一个远程蓝牙设备,使用这个来请求一个与远程设备的BluetoothSocket连接,或者查询关于设备名称、地址、类和连接状态等设备信息。
BluetoothSocket 代表一个蓝牙socket的接口(和TCP Socket类似)。这是一个连接点,它允许一个应用与其他蓝牙设备通过InputStream和OutputStream交换数据。
BluetoothServerSocket 代表一个开放的服务器socket,它监听接受的请求(与TCP ServerSocket类似)。为了连接两台Android设备,一个设备必须使用这个类开启一个服务器socket。当一个远程蓝牙设备开始一个和该设备的连接请求,BluetoothServerSocket将会返回一个已连接的BluetoothSocket,接受该连接。

 

BluetoothAdapter 中常用方法如下所示:

boolean cancelDiscovery()
Cancel the current device discovery process.
static boolean checkBluetoothAddress(String address)
Validate a String Bluetooth address, such as "00:43:A8:23:10:F0"

Alphabetic characters must be uppercase to be valid.

void closeProfileProxy(int profile, BluetoothProfile proxy)
Close the connection of the profile proxy to the Service.
boolean disable()
Turn off the local Bluetooth adapter—do not use without explicit user action to turn off Bluetooth.
boolean enable()
Turn on the local Bluetooth adapter—do not use without explicit user action to turn on Bluetooth.
String getAddress()
Returns the hardware address of the local Bluetooth adapter.
Set<BluetoothDevice> getBondedDevices()
Return the set of   BluetoothDevice  objects that are bonded (paired) to the local adapter.
synchronized static BluetoothAdapter getDefaultAdapter()
Get a handle to the default local Bluetooth adapter.
String getName()
Get the friendly Bluetooth name of the local Bluetooth adapter.
int getProfileConnectionState(int profile)
Get the current connection state of a profile.
boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener listener, int profile)
Get the profile proxy object associated with the profile.
BluetoothDevice getRemoteDevice(byte[] address)
Get a   BluetoothDevice  object for the given Bluetooth hardware address.
BluetoothDevice getRemoteDevice(String address)
Get a   BluetoothDevice  object for the given Bluetooth hardware address.
int getScanMode()
Get the current Bluetooth scan mode of the local Bluetooth adapter.
int getState()
Get the current state of the local Bluetooth adapter.
boolean isDiscovering()
Return true if the local Bluetooth adapter is currently in the device discovery process.
boolean isEnabled()
Return true if Bluetooth is currently enabled and ready for use.
BluetoothServerSocket listenUsingInsecureRfcommWithServiceRecord(String name, UUID uuid)
Create a listening, insecure RFCOMM Bluetooth socket with Service Record.
  • 14
    点赞
  • 116
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值