[#ArtifactTitle:”Find Phone/Device Function Documentation”] .. _search_device:

13. Find Phone / Find Device

Find Phone: Upon receiving a command from the device, the phone plays an audio alert to help the user locate it.
Find Device: The SDK sends a command to the device, which then plays a designated audio alert so the user can locate the device.

Important

  1. It is recommended to play an alarm sound rather than other audio types, as regular audio may be muted if the phone is in silent mode.

  2. Ensure the phone is not set to silent mode.

13.1. Find Phone

// WatchManager is a subclass of WatchOpImpl; ensure SDK is configured as per section 1.3
final WatchManager watchManager = WatchManager.getInstance();
// Ringtone handler
final RingHandler mRingHandler = RingHandler.getInstance();
// Register watch event callback
watchManager.registerOnWatchCallback(new OnWatchCallback() {
    // Listen for commands actively pushed by the device
    @Override
    public void onRcspCommand(BluetoothDevice device, CommandBase command) {
        if (command.getId() == Command.CMD_SEARCH_DEVICE) { // Handle "find device" command
            SearchDevCmd searchDevCmd = (SearchDevCmd) command;
            SearchDevParam param = searchDevCmd.getParam();
            if (param == null) return;
            if (param.getOp() == RcspConstant.RING_OP_OPEN) { // Turn on ringtone
                // Play alarm ringtone
                // type: playback method
                // timeout: playback duration (in milliseconds)
                mRingHandler.playAlarmRing(param.getType(), param.getTimeoutSec() * 1000);
                isRingPlay = true;
            } else { // Turn off ringtone
                mRingHandler.stopAlarmRing();
                isRingPlay = false;
            }
            // Respond to device with success status
            searchDevCmd.setStatus(StateCode.STATUS_SUCCESS);
            searchDevCmd.setParam(new SearchDevParam.SearchDevResultParam(0));
            watchManager.sendCommandResponse(device, searchDevCmd, null);
        }
    }
});

13.2. Find Device

// WatchManager is a subclass of WatchOpImpl; ensure SDK is configured as per section 1.3
final WatchManager watchManager = WatchManager.getInstance();
// Build "find device" command:
// op      --- operation type (find device)
// timeout --- timeout in seconds
// ringWay --- all devices ring
// player  --- device-side playback
CommandBase searchDevCmd = CommandBuilder.buildSearchDevCmd(
    RcspConstant.SEARCH_TYPE_DEVICE,
    60,
    RcspConstant.RING_WAY_ALL,
    RcspConstant.RING_PLAYER_DEVICE
);
watchManager.sendRcspCommand(watchManager.getConnectedDevice(), searchDevCmd, new RcspCommandCallback() {
    @Override
    public void onCommandResponse(BluetoothDevice device, SearchDevCmd cmd) {
        if (cmd.getStatus() != StateCode.STATUS_SUCCESS) {
            onErrCode(device, RcspErrorCode.buildJsonError(cmd.getId(), RcspErrorCode.ERR_RESPONSE_BAD_STATUS, cmd.getStatus(), null));
            return;
        }
        SearchDevResponse response = cmd.getResponse();
        if (null == response) {
            onErrCode(device, new BaseError(RcspErrorCode.ERR_PARSE_DATA, RcspErrorCode.getErrorDesc(RcspErrorCode.ERR_PARSE_DATA)));
            return;
        }
        if (response.getResult() == 0) {
            // Request succeeded – device is now ringing
        } else {
            onErrCode(device, RcspErrorCode.buildJsonError(cmd.getId(), RcspErrorCode.ERR_RESPONSE_BAD_RESULT, response.getResult(), null));
        }
    }

    @Override
    public void onErrCode(BluetoothDevice device, BaseError error) {
        // Handle error event
    }
});

13.3. Stop Searching

// WatchManager is a subclass of WatchOpImpl; ensure SDK is configured as per section 1.3
final WatchManager watchManager = WatchManager.getInstance();
// Build command to stop "find device"
watchManager.sendRcspCommand(
    CommandBuilder.buildSearchDevCmd(RcspConstant.RING_OP_CLOSE, 0),
    new RcspCommandCallback() {
        @Override
        public void onCommandResponse(BluetoothDevice device, SearchDevCmd cmd) {
            if (cmd.getStatus() != StateCode.STATUS_SUCCESS) {
                onErrCode(device, RcspErrorCode.buildJsonError(cmd.getId(), RcspErrorCode.ERR_RESPONSE_BAD_STATUS, cmd.getStatus(), null));
                return;
            }
            SearchDevResponse response = cmd.getResponse();
            if (null == response) {
                onErrCode(device, new BaseError(RcspErrorCode.ERR_PARSE_DATA, RcspErrorCode.getErrorDesc(RcspErrorCode.ERR_PARSE_DATA)));
                return;
            }
            if (response.getResult() == 0) {
                // Request succeeded – device has stopped ringing
            } else {
                onErrCode(device, RcspErrorCode.buildJsonError(cmd.getId(), RcspErrorCode.ERR_RESPONSE_BAD_RESULT, response.getResult(), null));
            }
        }

        @Override
        public void onErrCode(BluetoothDevice device, BaseError error) {
            // Handle error event
        }
    }
);