16. 设备双连功能

16.1. 查询是否支持设备双连功能

  1. JL_BluetoothManager方式

//Step0: 获取JL_BluetoothManager对象
JL_BluetoothManager manager = JL_BluetoothManager.getInstance(context);
//Step1: 获取设备信息
final BluetoothDevice device = manager.getConnectedDevice();
DeviceInfo deviceInfo = manager.getDeviceInfo(device);
//Step2: 查询是否支持设备双连功能
boolean isSupportDoubleConnection = deviceInfo != null && deviceInfo.isSupportDoubleConnection();
  1. RCSPController方式

//获取RCSPController对象
final RCSPController controller = RCSPController.getInstance();
//查询是否支持设备双连功能
final BluetoothDevice device = controller.getUsingDevice();
boolean isSupportDoubleConnection = controller.isSupportDoubleConnection(device);

16.2. 获取设备双连状态

Note

设备必须支持【设备双连】功能才能使用

  1. JL_BluetoothManager方式

function : AttrAndFunCode.SYS_INFO_FUNCTION_PUBLIC – 公共属性
mask : – 设备双连状态 * 0x00000001 << AttrAndFunCode.SYS_INFO_ATTR_DOUBLE_CONNECT

命令构造

CommandBase getDoubleConnectionStateCmd = CommandBuilder.buildGetSysInfoCmd(AttrAndFunCode.SYS_INFO_FUNCTION_PUBLIC,
            0x00000001 << AttrAndFunCode.SYS_INFO_ATTR_DOUBLE_CONNECT);

代码示例参考 查询设备系统信息

  1. RCSPController方式

//获取RCSPController对象
final RCSPController controller = RCSPController.getInstance();
//注册蓝牙RCSP事件监听器
controller.addBTRcspEventCallback(new BTRcspEventCallback() {
    @Override
    public void onDoubleConnectionChange(BluetoothDevice device, DoubleConnectionState state) {
        //回调设备双连状态
    }
});
//执行查询设备双连状态功能并等待结果回调
final BluetoothDevice device = controller.getUsingDevice();
controller.queryDoubleConnectionState(device, new OnRcspActionCallback<Boolean>() {
    @Override
    public void onSuccess(BluetoothDevice device, Boolean message) {
        //成功回调
        //信息将在BTRcspEventCallback#onDoubleConnectionChange回调
    }

    @Override
    public void onError(BluetoothDevice device, BaseError error) {
        //失败回调
        //error - 错误信息
    }
});

Note

设备双连状态, DoubleConnectionState

16.2.1. DoubleConnectionState

设备双连状态

public class DoubleConnectionState implements Parcelable {
    private int version = 0;  //版本号
    private boolean isOn;     //是否开启
    private String btAddress; //连接设备的蓝牙地址
}

16.3. 设置设备双连开关

Note

设备必须支持【设备双连】功能才能使用

  1. JL_BluetoothManager方式

function : AttrAndFunCode.SYS_INFO_FUNCTION_PUBLIC – 公共属性
type : AttrAndFunCode.SYS_INFO_ATTR_DOUBLE_CONNECT – 标识设备双连功能
value : DoubleConnectionState (n Bytes) –

命令构造

//Step0: 获取JL_BluetoothManager对象
final JL_BluetoothManager manager = JL_BluetoothManager.getInstance(context);
final BluetoothDevice device = manager.getConnectedDevice();
//需要先调用【获取设备双连状态】功能获取,后通过缓存信息获取
final DoubleConnectionState state = manager.getDeviceInfo(device).getDoubleConnectionState();
state.setOn(true);//设置开关
//构建设置设备双连状态命令
List<AttrBean> list = new ArrayList<>();
AttrBean attrBean = new AttrBean();
attrBean.setType(AttrAndFunCode.SYS_INFO_ATTR_DOUBLE_CONNECT);
attrBean.setAttrData(state.toData());
list.add(attrBean);
CommandBase setDoubleConnectionStateCmd = CommandBuilder.buildSetSysInfoCmd(AttrAndFunCode.SYS_INFO_FUNCTION_PUBLIC, list);

代码示例参考 设置设备系统属性

  1. RCSPController方式

//获取RCSPController对象
final RCSPController controller = RCSPController.getInstance();
//注册蓝牙RCSP事件监听器
controller.addBTRcspEventCallback(new BTRcspEventCallback() {
    @Override
    public void onDoubleConnectionChange(BluetoothDevice device, DoubleConnectionState state) {
        //回调设备双连状态
    }
});
//执行设置设备双连状态功能并等待结果回调
final BluetoothDevice device = controller.getUsingDevice();
final DoubleConnectionState state = controller.getDeviceInfo(device).getDoubleConnectionState();
state.setOn(true);//设置开关
controller.setDoubleConnectionState(device, state, new OnRcspActionCallback<Boolean>() {
    @Override
    public void onSuccess(BluetoothDevice device, Boolean message) {
        //成功回调
        //可以等设备推送信息,也可以主动查询
    }

    @Override
    public void onError(BluetoothDevice device, BaseError error) {
        //失败回调
        //error - 错误信息
    }
});

16.4. 获取已连接设备的设备信息

Note

设备必须支持【设备双连】功能才能使用

  1. JL_BluetoothManager方式

//Step0: 获取JL_BluetoothManager对象
final JL_BluetoothManager manager = JL_BluetoothManager.getInstance(context);
//Step1: 获取当前正在使用的设备
final BluetoothDevice device = manager.getConnectedDevice();
if (null == device) return;
//Step2: 执行查询已连接设备的手机信息功能的值功能并等待结果回调
DeviceBtInfo deviceBtInfo = DoubleConnectionSp.getInstance().getDeviceBtInfo(device.getAddress());
if (null == deviceBtInfo) {
    String btName = AppUtil.getBtName(MainApplication.getApplication());
    deviceBtInfo = new DeviceBtInfo().setBtName(btName);
}
CommandBase queryConnectedPhoneBtInfoCmd = CommandBuilder.buildQueryConnectedPhoneBtInfoCmd(deviceBtInfo);
manager.sendRcspCommand(device, queryConnectedPhoneBtInfoCmd, new RcspCommandCallback() {
    @Override
    public void onCommandResponse(BluetoothDevice device, CommandBase cmd) {
        //Step3: 检查设备状态
        if (cmd.getStatus() != StateCode.STATUS_SUCCESS) { //设备状态异常,进行异常处理
            onErrCode(device, new BaseError(ErrorCode.SUB_ERR_RESPONSE_BAD_STATUS, "Device reply an bad status: " + cmd.getStatus()));
            return;
        }
        QueryPhoneBtInfoCmd queryPhoneBtInfoCmd = (QueryPhoneBtInfoCmd) cmd;
        QueryPhoneBtInfoCmd.Response resp = queryPhoneBtInfoCmd.getResponse();
        if(null == resp){
            onErrCode(device, new BaseError(ErrorCode.SUB_ERR_DATA_FORMAT, "Device reply is error."));
            return;
        }
        //解析数据
    }

    @Override
    public void onErrCode(BluetoothDevice device, BaseError error) {
        //失败回调
        //error - 错误信息
    }
});
  1. RCSPController方式

//获取RCSPController对象
final RCSPController controller = RCSPController.getInstance();
//注册蓝牙RCSP事件监听器
controller.addBTRcspEventCallback(new BTRcspEventCallback() {
    @Override
    public void onConnectedBtInfo(BluetoothDevice device, ConnectedBtInfo info) {
        //回调已连接设备的手机信息
    }
});
//执行查询已连接设备的手机信息功能并等待结果回调
final BluetoothDevice device = controller.getUsingDevice();
//查询数据库保存的本地信息
DeviceBtInfo deviceBtInfo = DoubleConnectionSp.getInstance().getDeviceBtInfo(device.getAddress());
if (null == deviceBtInfo) {
    String btName = AppUtil.getBtName(MainApplication.getApplication());
    deviceBtInfo = new DeviceBtInfo().setBtName(btName);
}
controller.queryConnectedPhoneBtInfo(device, deviceBtInfo, new OnRcspActionCallback<ConnectedBtInfo>() {
    @Override
    public void onSuccess(BluetoothDevice device, ConnectedBtInfo message) {
        //成功回调
        //信息将在BTRcspEventCallback#onConnectedBtInfo回调
    }

    @Override
    public void onError(BluetoothDevice device, BaseError error) {
        //失败回调
        //error - 错误信息
    }
});

Note

设备已连接的手机蓝牙信息, ConnectedBtInfo

16.4.1. ConnectedBtInfo

设备已连接的手机蓝牙信息

public class ConnectedBtInfo implements Parcelable {
    private int connectionNum;  //已连接设备的数量
    private List<DeviceBtInfo> mDeviceBtInfoList; //已连接的设备的蓝牙信息
}

16.4.2. DeviceBtInfo

设备蓝牙信息

public class DeviceBtInfo implements Parcelable {
    private boolean isBind; //是否本机绑定
    private String address; //设备蓝牙地址
    private String btName;  //设备蓝牙名称
}