Bluetooth not sending file to other device

前端 未结 4 1341
深忆病人
深忆病人 2021-02-14 12:41

It is already asked but i didn\'t find any solution. For bluetooth application i am using the bluetoothShare.class .

My source code for sending the file to

4条回答
  •  清歌不尽
    2021-02-14 13:02

    This code works with firmware from MediaTek. Tested on Android 4.0.4. Sends the file, without asking anything from the user

    public boolean SendFileViaBluetoothOPP(String file_path, String destinationMAC){
        BluetoothAdapter btadapter = BluetoothAdapter.getDefaultAdapter();
        if(btadapter == null)
            return false;
    
        BluetoothDevice btdev = btadapter.getRemoteDevice(destinationMAC);
        if(btdev == null)
            return false;
    
        Uri uri = Uri.fromFile(new File(file_path));
    
        Intent shareIntent = new Intent(Intent.ACTION_SEND)
                            .putExtra(Intent.EXTRA_STREAM, uri)
                            .setType("application/zip");
    
        List resolvedActivities = getPackageManager().queryIntentActivities(shareIntent,  0);
    
        boolean found = false;
        for(ResolveInfo actInfo: resolvedActivities){
            if(actInfo.activityInfo.packageName.equals("com.mediatek.bluetooth"))
            {                   
                shareIntent.setComponent( new ComponentName(actInfo.activityInfo.packageName, actInfo.activityInfo.name ) );
                shareIntent.putExtra("com.mediatek.bluetooth.sharegateway.extra.DEVICE_ADDRESS", btdev);
                shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                found = true;
                break;
            }
        }
    
        if(found){
            startActivity(shareIntent);
            return true;
        }
    
        return false;
    }
    

    On older phone from MediaTek with Android 2.2.1 this code launches BluetoothDevicePicker to complete the operation.

提交回复
热议问题