How to accept bluetooth received file in Android application?

后端 未结 3 647
醉梦人生
醉梦人生 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:13

    ON ROOTED DEVICES, You can just install only two apps on your phone to achieve your goal.

    1. XPosed Installer
    2. Auto-Accept

    This way you hook System service.

    import android.util.*;
    import de.robv.android.xposed.*;
    import de.robv.android.xposed.callbacks.XC_LoadPackage.*;
    
    import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
    
    public class Tutorial implements IXposedHookLoadPackage
    {
    
        private String TAG="TUTORIAL";
        public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
            if (!lpparam.packageName.equals("com.android.bluetooth"))
            {
                Log.i(TAG,"Not: "+lpparam.packageName);
                return;
            }
            Log.i(TAG,"Yes "+lpparam.packageName);  
    
            findAndHookMethod("com.android.bluetooth.opp.BluetoothOppManager", lpparam.classLoader, "isWhitelisted", String.class,new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        Log.v(TAG,"HOOK DONE");
                        param.setResult(true); /* you can compare the sender address(String) with your computer and determine if you return true or just allow the original method to be called after this returns.*/
    
                    }
                });
    
        }
    }
    

    For more information, please visit my answer in SO.

    I'll post some direct links here.

    Links

    Dropbox link of the auto accepting app

    Dropbox link of the project files (zip)

    Xposed apk site

    Towelroot site to root your phone

    Auto-Accept github repository

    0 讨论(0)
  • 2021-02-12 14:20

    You can try using the Bluetooth socket connection to set a client server TCP like connection.

    0 讨论(0)
  • 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;
                }
            }
    
    0 讨论(0)
提交回复
热议问题