BluetoothAdapter ;BluetoothDevice;BluetoothSocket;BluetoothServerSocket;UUID和String转换;
1.BluetoothAdapter 顾名思义,蓝牙适配器,直到我们建立bluetoothSocket连接之前,都要不断操作它
BluetoothAdapter里的方法很多,常用的有以下几个:
boolean//取消可见 | cancelDiscovery()Cancel the current device discovery process. |
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 //得到已经配对蓝牙 |
getBondedDevices()Return the set of
BluetoothDevice objects that are bonded (paired) to the local adapter.
|
synchronizedstatic BluetoothAdapter //获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdapter |
getDefaultAdapter()Get a handle to the default local Bluetooth adapter. |
String//得到本地蓝牙name | 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//通过address得device | getRemoteDevice(String address)Get a
BluetoothDevice object for the given Bluetooth hardware address.
|
BluetoothDevice | getRemoteDevice(byte[] 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 //通过bluetoothAdapter创建bluetoothsocket (不安全) |
listenUsingInsecureRfcommWithServiceRecord(String name, UUID uuid)Create a listening, insecure RFCOMM Bluetooth socket with Service Record. |
BluetoothServerSocket //通过bluetoothAdapter创建bluetoothsocket (不安全) |
listenUsingRfcommWithServiceRecord(String name, UUID uuid)Create a listening, secure RFCOMM Bluetooth socket with Service Record. |
boolean//设置本地蓝牙的name | setName(String name)Set the friendly Bluetooth name of the local Bluetooth adapter. |
boolean | startDiscovery()Start the remote device discovery process. |
listenUsingRfcommWithServiceRecord(String name,UUID uuid)根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步
startDiscovery()开始搜索,这是搜索的第一步
2.BluetoothDevice看名字就知道,这个类描述了一个蓝牙设备
BluetoothSocket //通过根据UUID创建并返回一个BluetoothSocket |
createInsecureRfcommSocketToServiceRecord(UUID uuid)Create an RFCOMM
BluetoothSocket socket ready to start an insecure outgoing connection to this remote device using SDP lookup of uuid.
|
BluetoothSocket | createRfcommSocketToServiceRecord(UUID uuid)Create an RFCOMM
BluetoothSocket ready to start a secure outgoing connection to this remote device using SDP lookup of uuid.
|
int | describeContents()Describe the kinds of special objects contained in this Parcelable's marshalled representation. |
boolean | equals(Object o)Compares this instance with the specified object and indicates if they are equal. |
boolean | fetchUuidsWithSdp()Perform a service discovery on the remote device to get the UUIDs supported. |
String//得到远程设备的地址 | getAddress()Returns the hardware address of this BluetoothDevice. |
BluetoothClass | getBluetoothClass()Get the Bluetooth class of the remote device. |
int//得到远程设备的状态 | getBondState()Get the bond state of the remote device. |
String得到远程设备的name | getName()Get the friendly Bluetooth name of the remote device. |
ParcelUuid[]得到远程设备的uuids | getUuids()Returns the supported features (UUIDs) of the remote device. |
int | hashCode()Returns an integer hash code for this object. |
String | toString()Returns a string representation of this BluetoothDevice. |
void | writeToParcel(Parcel out, int flags)Flatten this object in to a Parcel. |
这个方法也是我们获取BluetoothDevice的目的——创建BluetoothSocket
这个类其他的方法,如getAddress(),getName(),同BluetoothAdapter
3.BluetoothServerSocket如果去除了Bluetooth相信大家一定再熟悉不过了,既然是Socket,方法就应该都差不多
BluetoothSocket | accept(int timeout)Block until a connection is established, with timeout. |
BluetoothSocket | accept()Block until a connection is established. |
void | close()Immediately close this socket, and release all associated resources. |
两个重载的accept(),accept(inttimeout)两者的区别在于后面的方法指定了过时时间,需要注意的是,执行这两个方法的时候,直到接收到了客户端的请求(或是过期之后),都会阻塞线程,应该放在新线程里运行!
4.BluetoothSocket
void | close()Closes the object and release any system resources it holds. |
void | connect()Attempt to connect to a remote device. |
InputStream | getInputStream()Get the input stream associated with this socket. |
OutputStream | getOutputStream()Get the output stream associated with this socket. |
BluetoothDevice | getRemoteDevice()Get the remote device this socket is connecting, or connected, to. |
boolean | isConnected()Get the connection status of this socket, ie, whether there is an active connection with remote device. |
uuid->String:
String s = UUID.randomUUID().toString();
String->uuid:
【BluetoothAdapter ;BluetoothDevice;BluetoothSocket; BluetoothServerSocket; UUID和String转换;】UUID.fromString(name)
推荐阅读
- android|android BluetoothAdapter蓝牙BLE扫描总结
- Android|Android中蓝牙的基本应用--BluetoothAdapter类
- Android中蓝牙的基本使用----BluetoothAdapter类简介