【Android 应用开发】BluetoothClass详解

举报
韩曙亮 发表于 2022/01/11 02:39:46 2022/01/11
【摘要】 一. BluetoothClass简介 1. 继承关系 public final class BluetoothClass extends Object implements Parcelable 该类是final类, 不能被继承, 没有子类; 该类继承了Object类, 实现了Parcelable接口; Parcelabl...

一. BluetoothClass简介


1. 继承关系


public final class BluetoothClass extends Object implements Parcelable
 

该类是final类, 不能被继承, 没有子类;

该类继承了Object类, 实现了Parcelable接口;


Parcelable接口

Java中的序列化方法 : 在Java中序列化有两种方法, 一种是实现Serializable接口, 一种是实现Parcelable接口, Serializable接口是J2SE固有支持的, Parcelable是Android支持的, 是Android中特有的, 效率比Serializable高;


序列化实现

Serializable接口实现 : 只需要声明一下即可;

Parcelable接口实现

a. 声明实现的接口Parcelable;

b. 实现writeToParcel接口, 将对象序列化成一个Parcel对象;

c. 实例化内部对象Parcelable.Creator, 注意对象名称必须是"CREATOR", 在这个内部对象中实现createFromParcel()方法, 可以将序列化对象Parcel对象反序列化为源对象; 

public   static   final  Parcelable.Creator<T> CREATOR 
 

普通对象->Parcel对象 : 使用writeToParcel()方法;

Parcel对象->普通对象 : 使用createFromParcel()方法;


使用writeToParcel()方法 和 createFromParcel()方法 可以将普通对象与Parcel对象相互进行转化, 因此写入对象的顺序要和读取对象的顺序要保持一致;


Parcel对象读写方法

读写变量 : 


  
  1. writeByte( byte ), readByte()//读写byte类型数据
  2. writeDouble(double ), readDouble()//读写double类型数据

  
  1. writeFloat(float ), readFloat()//读写Float类型数据
  2. writeInt(int ), readInt()//读写int类型数据
  3. writeLong(long ), readLong()//读写long类型数据
  4. writeString(String), readString()//读写String类型数据

读写数组 :



  
  1. writeBooleanArray( boolean []), readBooleanArray( boolean []), createBooleanArray()
  2. writeByteArray(byte []), writeByteArray( byte [], int , int ), readByteArray( byte []), createByteArray()
  3. writeCharArray(char []), readCharArray( char []), createCharArray()
  4. writeDoubleArray(double []), readDoubleArray( double []), createDoubleArray()
  5. writeFloatArray(float []), readFloatArray( float []), createFloatArray()
  6. writeIntArray(int []), readIntArray( int []), createIntArray()
  7. writeLongArray(long []), readLongArray( long []), createLongArray()
  8. writeStringArray(String[]), readStringArray(String[]), createStringArray()
  9. writeSparseBooleanArray(SparseBooleanArray), readSparseBooleanArray()

2. BluetoothClass介绍


判断类别功能 : BluetoothClass用来描述一个蓝牙设备的通用特性功能, 可以根据这个类判断蓝牙的类别, 可以提供音频或者电话服务;

蓝牙类组成 : 0个或者多个服务类, 一个设备类;

低精确度 : BluetoothClass只能粗略描述一个蓝牙设备, 当两个设备连接的时候, 这个类提供的数据不是很准确; 

高精确度 : SDP请求搜寻的服务精确度很高, 调用createRfcommSocketToServiceRecord()和listenUsingRfcommWithServiceRecord()方法创建RFCOMM端口的时候会自动调用SDP服务;


二 API解析


1. 内部类


(1)设备

class	BluetoothClass.Device  
 
设备类的常量


(2)服务

class	BluetoothClass.Service
 
服务类的常量

2. 公共方法


(1)描述序列化内容

public int describeContents ()
 

返回值 : Parcelable排列的特殊对象类型集合的位掩码;


(2)比较方法

public boolean equals (Object o)
 


(3)获取设备内部类

public int getDeviceClass ()
 

作用 : 返回设备类, 这个设备类可以与BluetoothClass.Device中的常量进行对比, 得到这个设备是什么类型的设备;

返回值 : BluetoothClass的设备类; 


(4)获取设备主类

 public int getMajorDeviceClass ()
 

返回值 : 设备的主类;


(5)是否支持服务类

public boolean hasService (int service)
 

参数 : 服务类常量, 在BluetoothClass.Service中定义;

返回值 : 如果该服务类可以被支持, 返回true;


(6)获取哈希码

public int hashCode ()
 


(7)字符串方法

public int hashCode ()
 


(8)数据写入Parcel

public void writeToParcel (Parcel out, int flags)
 
作用 : 将类写入序列化对象中;

参数 : out, 被写入的parcel对象; flags, 对象被写入有关的标识;


文章来源: hanshuliang.blog.csdn.net,作者:韩曙亮,版权归原作者所有,如需转载,请联系作者。

原文链接:hanshuliang.blog.csdn.net/article/details/14224001

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。