Android Bluetooth to connect another bluetooth device

前端 未结 2 1794
庸人自扰
庸人自扰 2020-12-18 08:03

Im doing bluetooth based application, I want to connect other devices like nokia devices, and printer.

I refer the android bluetooth documentation http://developer.a

相关标签:
2条回答
  • 2020-12-18 08:56

    I think this is not possible.

    in fact, when you create a bluetooth socket, you have to use createRfcommSocketToServiceRecord(UUID)

    This function requires an UUID that is a string shared between the applications on the two devices so the connection can be established.

    Without a bluetooth socket listening on the other device, with the exact same UUID, you won't be able to share data.

    0 讨论(0)
  • 2020-12-18 08:59

    You can do connection between two BT devices easily. You just need to call

    createRfcommSocketToServiceRecord(UUID)
    

    with UUID that understand receiver device. For file transfer action UUID must be equal (for example) to 00001106-0000-1000-8000-00805F9B34FB (File transfer service)

    So you connection code might looks like code below

    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("00:0A:94:16:77:A0"); BluetoothSocket clientSocket;

    try {
        log(TAG, "Remote device " + device);
        ParcelUuid[] uuids = device.getUuids();
        boolean isFileTransferSupported = false;
        UUID ftpUID = UUID.fromString("00001106-0000-1000-8000-00805F9B34FB");
        // Check if remote device supports file transfer
        for (ParcelUuid parcelUuid: uuids) {
            if (parcelUuid.getUuid().equals(ftpUID)) {
                isFileTransferSupported = true;
                break;
            }
        }
        if (!isFileTransferSupported) {
            log(TAG, "Remote bluetooth device does not supports file transfer ");
            return;
        }
        clientSocket = device.createRfcommSocketToServiceRecord(ftpUID);
        clientSocket.connect();
    } catch (IOException e) {
        return;
    }
    
    0 讨论(0)
提交回复
热议问题