How to accept bluetooth received file in Android application?

后端 未结 3 648
醉梦人生
醉梦人生 2021-02-12 13:41

I would like to implement an application to receive a file from a Bluetooth device.

Before receiving, a notification will be raised to accept an incoming file r

3条回答
  •  忘了有多久
    2021-02-12 14:22

    I developed an app that include this kind of task, and you can use BluetoothChat example. You must set the secure flag to false: ` boolean secure = false;

            try {
                if (secure) {
                    tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,
                        MY_UUID_SECURE);
                } else {
                    tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(
                            NAME_INSECURE, MY_UUID_INSECURE);
                }
            } catch (IOException e) {
                Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
    
            mmServerSocket = tmp;
        }`
    

    And then read the buffer from the InputStream that you can find in ConnectedThread:

    while (true) {
                try {
    
                    bytes = mmInStream.read(buffer);
                     /*write bytes in a file*/
    
    
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
    
                    BluetoothChatService.this.start();
                    break;
                }
            }
    

提交回复
热议问题