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

Posted on 2014-12-13 11:05  kp_liu  阅读(245)  评论(0编辑  收藏  举报

一、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

Copyright © 2024 kp_liu
Powered by .NET 8.0 on Kubernetes