Android蓝牙开发(二):相关的API简介

一、BluetoothAdapter--蓝牙适配器(本机)

 BluetoothAdapter里的方法很多,常用的有以下几个:

(1)cancelDiscovery() 根据字面意思,是取消发现,也就是说当我们正在搜索设备的时候调用这个方法将不再继续搜索

(2)disable()关闭蓝牙

(3)enable()打开蓝牙,这个方法打开蓝牙不会弹出提示,

更多的时候我们需要问下用户是否打开,以下这两行代码同样是打开蓝牙,不过会提示用户:

 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
 startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
//也可以写成startActivity(enableIntent);

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

(5)getDefaultAdapter()获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdapter

(6)getName()获取本地蓝牙名称

(7)getRemoteDevice(String address)根据蓝牙地址获取远程蓝牙设备

(8)getState()获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要)

(9)isDiscovering()判断当前是否正在查找设备,是返回true

(10)isEnabled()判断蓝牙是否打开,已打开返回true,否则,返回false

(11)listenUsingRfcommWithServiceRecord(String name,UUID uuid)根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步

(12)startDiscovery()开始搜索,这是搜索的第一步


二、BluetoothDevice--蓝牙设备(远程)

      createRfcommSocketToServiceRecord(UUIDuuid)根据UUID创建并返回一个BluetoothSocket

这个方法也是我们获取BluetoothDevice的目的——创建BluetoothSocket

这个类其他的方法,如getAddress(),getName(),同BluetoothAdapter


三、BluetoothSocket--蓝牙socket接口


四、BluetoothServerSocket--表示一个开放的服务器socket,监听进入的连接请求


五、bluetooth包下还有4个类,BluetoothClass 、BluetoothClass.Device、BluetoothClass.Device.Major、BluetoothClass.Service


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值