Bluetooth not sending file to other device

前端 未结 4 1338
深忆病人
深忆病人 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<ResolveInfo> 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.

    0 讨论(0)
  • 2021-02-14 13:05

    BluetoothShare class not supported android 4.1 and above. you can use the following intent coding to send file in android version 4.1 and above

    Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setComponent(new ComponentName(
            "com.android.bluetooth",
            "com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
        intent.setType("image/jpeg");
        file = new File(filepath);
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        startActivity(intent);
    

    and also some devices in Android v 2.2/2.3 not send the file via bluetoothShare class.

    0 讨论(0)
  • 2021-02-14 13:10

    Maybe you can try another solution with BufferedWriter and BufferedReader.

    Here is a snipped code:

                BluetoothDevice mmDevice;
                Set<BluetoothDevice> mBluetoothAdapter;
    
                BluetoothAdapter bAdapter = BluetoothAdapter
                        .getDefaultAdapter();
                mBluetoothAdapter = bAdapter.getBondedDevices();
    
                for (BluetoothDevice bc : mBluetoothAdapter) {
                    if (bc.getName().indexOf("name_of_bluetoothdevide") != -1) {
                        UUID uuid = UUID
                                .fromString("00001101-0000-1000-8000-00805F9B34FB"); // Standard
                                                                                        // SerialPortService
                                                                                        // ID
                        mmDevice = bc;
                        BluetoothSocket mmSocket = mmDevice
                                .createInsecureRfcommSocketToServiceRecord(uuid);
                        bAdapter.cancelDiscovery();
                        mmSocket.connect();
                        BufferedWriter Writer = new BufferedWriter(
                                new OutputStreamWriter(
                                        mmSocket.getOutputStream()));
                        Writer.write("Bluetooth connected!");
                        Writer.flush();
    
                        app.setmSocket(mmSocket);
    
                        break;
                    }
                }
    

    And for reading:

      BufferedReader Reader = new BufferedReader(
                        new InputStreamReader(mmSocket.getInputStream()));
                receivedMsg = Reader.readLine();
    

    Hope it can help you.

    0 讨论(0)
  • Maybe this could help you in some way…

    private void sendData(String message) {
        byte[] msgBuffer = message.getBytes();
        Log.d(TAG, "...Sending data: " + message + "...");
        try {
            outStream.write(msgBuffer);
        } catch (IOException e) {
            String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
            if (address.equals("00:00:00:00:00:00")) 
                msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on java code";
            msg = msg +  ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";
            errorExit("Fatal Error", msg);       
        }
    }
    
    0 讨论(0)
提交回复
热议问题