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< BluetoothDevice>

  //得到已经配对蓝牙   

getBondedDevices()                 
Return the set of BluetoothDevice objects that are bonded (paired) to the local adapter.

 synchronized  static  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.

 5.UUID和String之间的转化:

uuid->String:

String s = UUID.randomUUID().toString();

String->uuid:

UUID.fromString(name)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.os.Handler; import android.os.Message; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; public class bluetoothController { private BluetoothAdapter mBluetoothAdapter; private BluetoothSocket mBluetoothSocket; private InputStream mInputStream; private OutputStream mOutputStream; private Handler mHandler; public bluetoothController(Handler handler) { mHandler = handler; mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); } public boolean connect(String address) { BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); try { mBluetoothSocket = device.createRfcommSocketToServiceRecord(uuid); mBluetoothSocket.connect(); mInputStream = mBluetoothSocket.getInputStream(); mOutputStream = mBluetoothSocket.getOutputStream(); return true; } catch (IOException e) { e.printStackTrace(); return false; } } public void disconnect() { try { mBluetoothSocket.close(); } catch (IOException e) { e.printStackTrace(); } } public void write(byte[] bytes) { try { mOutputStream.write(bytes); } catch (IOException e) { e.printStackTrace(); } } public void read() { byte[] buffer = new byte[1024]; int bytes; while (true) { try { bytes = mInputStream.read(buffer); String message = new String(buffer, 0, bytes); Message msg = mHandler.obtainMessage(0, message); mHandler.sendMessage(msg); } catch (IOException e) { e.printStackTrace(); break; } } } }
03-23
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值