API

BluetoothDevice

java.lang.Object

↳android.bluetooth.BluetoothDevice

代表 远程蓝牙设备 。通过BluetoothDevice,您可以与各自的设备建立连接或查询有关该设备的信息,例如名称,地址,类和绑定状态。

//硬件地址形如: "00:11:22:AA:BB:CC"
String address = BluetoothDevice.address  

//设备名称(如果无则返回空) 
String name = BluetoothDevice.name  

//设备类型:
int type = BluetoothDevice.type     
    //未知类型
    public static final int DEVICE_TYPE_UNKNOWN = 0; 
    //BR、EDR(经典)蓝牙类型
    public static final int DEVICE_TYPE_CLASSIC = 1;  
    //BLE(低功耗)蓝牙类型
    public static final int DEVICE_TYPE_LE = 2;   
    //双模式类型
    public static final int DEVICE_TYPE_DUAL = 3;      

//设备支持的特征通用唯一标识符(UUID)
int uuids = BluetoothDevice.uuids

//设备绑定状态
int bondState = BluetoothDevice.bondState
     //未与远程蓝牙设备绑定(配对),所以通信(如果允许)将是未经身份认证和加密的
     public static final int BOND_NONE = 10; 
     //与远程蓝牙设备正在绑定(配对)
     public static final int BOND_BONDING = 11;
     //与远程蓝牙设备已经绑定(配对),通讯是经过身份验证和加密的(设备绑定(配对)并不一定已连接。)
     public static final int BOND_BONDED = 12;  
BluetoothClass

java.lang.Object

↳android.bluetooth.BluetoothClass

表示 蓝牙类别 ,该类别描述设备的一般特征和功能。例如,蓝牙类将指定常规设备类型(例如电话,计算机或头戴式耳机),以及是否支持诸如音频或电话之类的服务。

  • getMajorDeviceClass

    返回的是 主要设备类型 (比如是麦克风、电话、电脑、网络、音视频、外围设备等等)。

    /**
     *返回的是主要设备,从此函数返回的值可以与BluetoothClass.Device.Major中的公共常量进行比较,
     *以确定当前设备主要类型。
     */
    int majorDeviceClass = BluetoothClass.getMajorDeviceClass;
    	public static class Device {
    			public static class Major {
                //麦克风
                public static final int MISC = 0x0000;  
                //电脑
                public static final int COMPUTER = 0x0100;
                //电话
                public static final int PHONE = 0x0200;
                //网络
                public static final int NETWORKING = 0x0300;
                //音视频
                public static final int AUDIO_VIDEO = 0x0400;
                //外围
                public static final int PERIPHERAL = 0x0500;
                //图像
                public static final int IMAGING = 0x0600;
                //可穿戴设备
                public static final int WEARABLE = 0x0700;
                //玩具
                public static final int TOY = 0x0800;
                //健康
                public static final int HEALTH = 0x0900;
                //未分类
                public static final int UNCATEGORIZED = 0x1F00;
            }
      }
    
  • getDeviceClass

    返回此BluetoothClass的(主要和次要)设备类组件其中包含了getMajorDeviceClass类别。

    /**
     *此BluetoothClass的(主要和次要)设备类组件,从此函数返回的值可以与BluetoothClass.Device.Major
     *中的公共常量进行比较,以确定当前设备主要和次要类型。
     */
    int deviceClass = BluetoothClass.getDeviceClass
    	public static class Device {
          //DeviceClass包含了上面的MajorDeviceClass,不再重复标示类型,参照上面getMajorDeviceClass
    			public static class Major {
                public static final int MISC = 0x0000;
                public static final int COMPUTER = 0x0100;
                public static final int PHONE = 0x0200;
                public static final int NETWORKING = 0x0300;
                public static final int AUDIO_VIDEO = 0x0400;
                public static final int PERIPHERAL = 0x0500;
                public static final int IMAGING = 0x0600;
                public static final int WEARABLE = 0x0700;
                public static final int TOY = 0x0800;
                public static final int HEALTH = 0x0900;
                public static final int UNCATEGORIZED = 0x1F00;
            }
            //未分类的可穿戴设备
            public static final int WEARABLE_UNCATEGORIZED = 0x0700;
            //可穿戴手表
            public static final int WEARABLE_WRIST_WATCH = 0x0704;
            //可穿戴寻呼机
            public static final int WEARABLE_PAGER = 0x0708;
            //可穿戴夹克
            public static final int WEARABLE_JACKET = 0x070C;
            //可穿戴头盔
            public static final int WEARABLE_HELMET = 0x0710;
            //可穿戴眼睛
            public static final int WEARABLE_GLASSES = 0x0714;
     
            //其他取值如下:
            // Devices in the COMPUTER major class(major为电脑的Devices有如下取值)
            public static final int COMPUTER_UNCATEGORIZED = 0x0100;
            public static final int COMPUTER_DESKTOP = 0x0104;
            //笔记本电脑
            public static final int COMPUTER_SERVER = 0x0108;
            public static final int COMPUTER_LAPTOP = 0x010C;
            public static final int COMPUTER_HANDHELD_PC_PDA = 0x0110;
            public static final int COMPUTER_PALM_SIZE_PC_PDA = 0x0114;
            public static final int COMPUTER_WEARABLE = 0x0118;
     
            // Devices in the PHONE major class(major为电话的Devices有如下取值)
            public static final int PHONE_UNCATEGORIZED = 0x0200;
            public static final int PHONE_CELLULAR = 0x0204;
            public static final int PHONE_CORDLESS = 0x0208;
            public static final int PHONE_SMART = 0x020C;
            public static final int PHONE_MODEM_OR_GATEWAY = 0x0210;
            public static final int PHONE_ISDN = 0x0214;
     
            // Minor classes for the AUDIO_VIDEO major class(major为音视频的Devices有如下取值)
            public static final int AUDIO_VIDEO_UNCATEGORIZED = 0x0400;
            public static final int AUDIO_VIDEO_WEARABLE_HEADSET = 0x0404;
            public static final int AUDIO_VIDEO_HANDSFREE = 0x0408;
            //public static final int AUDIO_VIDEO_RESERVED              = 0x040C;
            public static final int AUDIO_VIDEO_MICROPHONE = 0x0410;
            public static final int AUDIO_VIDEO_LOUDSPEAKER = 0x0414;
            public static final int AUDIO_VIDEO_HEADPHONES = 0x0418;
            public static final int AUDIO_VIDEO_PORTABLE_AUDIO = 0x041C;
            public static final int AUDIO_VIDEO_CAR_AUDIO = 0x0420;
            public static final int AUDIO_VIDEO_SET_TOP_BOX = 0x0424;
            public static final int AUDIO_VIDEO_HIFI_AUDIO = 0x0428;
            public static final int AUDIO_VIDEO_VCR = 0x042C;
            public static final int AUDIO_VIDEO_VIDEO_CAMERA = 0x0430;
            public static final int AUDIO_VIDEO_CAMCORDER = 0x0434;
            public static final int AUDIO_VIDEO_VIDEO_MONITOR = 0x0438;
            public static final int AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER = 0x043C;
            public static final int AUDIO_VIDEO_VIDEO_CONFERENCING = 0x0440;
            //public static final int AUDIO_VIDEO_RESERVED              = 0x0444;
            public static final int AUDIO_VIDEO_VIDEO_GAMING_TOY = 0x0448;
     
            // Devices in the TOY major class(major为玩具的Devices有如下取值)
            public static final int TOY_UNCATEGORIZED = 0x0800;
            public static final int TOY_ROBOT = 0x0804;
            public static final int TOY_VEHICLE = 0x0808;
            public static final int TOY_DOLL_ACTION_FIGURE = 0x080C;
            public static final int TOY_CONTROLLER = 0x0810;
            public static final int TOY_GAME = 0x0814;
     
            // Devices in the HEALTH major class(major为健康的Devices有如下取值)
            public static final int HEALTH_UNCATEGORIZED = 0x0900;
            public static final int HEALTH_BLOOD_PRESSURE = 0x0904;
            public static final int HEALTH_THERMOMETER = 0x0908;
            public static final int HEALTH_WEIGHING = 0x090C;
            public static final int HEALTH_GLUCOSE = 0x0910;
            public static final int HEALTH_PULSE_OXIMETER = 0x0914;
            public static final int HEALTH_PULSE_RATE = 0x0918;
            public static final int HEALTH_DATA_DISPLAY = 0x091C;
      }
    

连接建立方式

安全连接
  • listenUsingRfcommWithServiceRecord & createRfcommSocketToServiceRecord

    安全的连接需要PIN配对通信加密

    • listenUsingRfcommWithServiceRecord & createRfcommSocketToServiceRecord 服务端与客户端将使用UUID创建一个安全的RFCOMM蓝牙Socket来监听和连接 使用此种安全方式,连接到此Socket的远程设备将通过身份验证(即使用PIN码配对 以防中间人攻击),并且此Socket上的通信将被加密。
不安全连接
  • listenUsingInsecureRfcommWithServiceRecord & createInsecureRfcommSocketToServiceRecord

    非安全连接无需身份认证(PIN配对)蓝牙2.1设备已经强制加密

    • listenUsingInsecureRfcommWithServiceRecord & createInsecureRfcommSocketToServiceRecord:服务端与客户端将使用UUID创建一个非安全的RFCOMM蓝牙Socket来监听和连接,通信通道将没有经过身份验证的链接密钥(不会进行PIN配对),即它将受到中间人攻击。(对于蓝牙2.1设备,链接密钥将被加密,因为加密是强制性的。对于旧版设备(蓝牙2.1之前的设备),链接密钥不会被加密。如果需要加密并经过身份验证的通信通道,请使用listenUsingRfcommWithServiceRecord
使用注意
  • listenUsingRfcommWithServiceRecord 仅在能够进行认证的设备上使用,如果设备不具有输入和输出功能或仅具有显示功能(无法进行PIN确认,如耳机等),这种情况下请使用: listenUsingInsecureRfcommWithServiceRecord

  • 安全与非安全的创建方式服务端与客户端应 对等使用 (即服务端使用安全的方式创建连接监听,客户端也应使用安全的方式来创建连接)。否则一方使用安全方式,另一方使用非安全方式,在配对后无法连接出现如下类似异常

    getBluetoothService() called with no BluetoothManagerCallback
    java.io.IOException: read failed, socket might closed or timeout, read ret: -1
    
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐