【问题标题】:Bluetooth not sending file to other device蓝牙不向其他设备发送文件
【发布时间】:2013-08-31 06:49:58
【问题描述】:

已经问过了,但我没有找到任何解决方案。对于蓝牙应用,我使用的是bluetoothShare.class

我将文件发送到目标设备的源代码

MainActivity.class:

Set<BluetoothDevice> devices = btAdapter
                            .getBondedDevices();
                    final String btDeviceName = selected_deviceName;
                    BluetoothDevice device = null;

                    for (BluetoothDevice itDevice : devices) {
                        if (btDeviceName.equals(itDevice.getName())) {
                            device = itDevice;
                        }
                    }

                    if (device != null) {

                        ContentValues values = new ContentValues();
                        values.put(BluetoothShare.URI, uri.toString());
                        values.put(BluetoothShare.MIMETYPE, "image/jpeg");
                        values.put(BluetoothShare.DESTINATION,
                                device.getAddress());
                        values.put(BluetoothShare.DIRECTION,
                                BluetoothShare.DIRECTION_OUTBOUND);
                        Long ts = System.currentTimeMillis();
                        values.put(BluetoothShare.TIMESTAMP, ts);
                        final Uri contentUri = getApplicationContext()
                                .getContentResolver().insert(
                                        BluetoothShare.CONTENT_URI, values);
                        Log.v(TAG, "Insert contentUri: " + contentUri
                                + "  to device: " + device.getName());
                        Toast.makeText(getApplicationContext(), "Success",
                                Toast.LENGTH_LONG).show();
                    } else {
                        textStatus
                                .setText("Bluetooth remote device not found");
                    }
                } else {
                    textStatus.setText("Bluetooth not activated");
                }
            }

            else {
                Toast.makeText(getApplicationContext(), "No devices found",
                        Toast.LENGTH_LONG).show();
            }

还有 blueToothShare.class:

package process.bluetooth.sendfile.opp;

import android.net.Uri;
import android.provider.BaseColumns;


public final class BluetoothShare implements BaseColumns {
private BluetoothShare() {
}

public static final String PERMISSION_ACCESS = "android.permission.ACCESS_BLUETOOTH_SHARE";

public static final Uri CONTENT_URI = Uri
        .parse("content://com.android.bluetooth.opp/btopp");

public static final String TRANSFER_COMPLETED_ACTION = "android.btopp.intent.action.TRANSFER_COMPLETE";

public static final String INCOMING_FILE_CONFIRMATION_REQUEST_ACTION = "android.btopp.intent.action.INCOMING_FILE_NOTIFICATION";

public static final String USER_CONFIRMATION_TIMEOUT_ACTION = "android.btopp.intent.action.USER_CONFIRMATION_TIMEOUT";

public static final String URI = "uri";

public static final String FILENAME_HINT = "hint";

public static final String _DATA = "_data";

public static final String MIMETYPE = "mimetype";

public static final String DIRECTION = "direction";

public static final String DESTINATION = "destination";

public static final String VISIBILITY = "visibility";

public static final String USER_CONFIRMATION = "confirm";

public static final String STATUS = "status";

public static final String TOTAL_BYTES = "total_bytes";

public static final String CURRENT_BYTES = "current_bytes";

public static final String TIMESTAMP = "timestamp";

public static final int DIRECTION_OUTBOUND = 0;

public static final int DIRECTION_INBOUND = 1;

public static final int USER_CONFIRMATION_PENDING = 0;

public static final int USER_CONFIRMATION_CONFIRMED = 1;

public static final int USER_CONFIRMATION_AUTO_CONFIRMED = 2;

public static final int USER_CONFIRMATION_DENIED = 3;

public static final int USER_CONFIRMATION_TIMEOUT = 4;

public static final int VISIBILITY_VISIBLE = 0;

public static final int VISIBILITY_HIDDEN = 1;

public static boolean isStatusInformational(int status) {
    return (status >= 100 && status < 200);
}

public static boolean isStatusSuspended(int status) {
    return (status == STATUS_PENDING);
}

public static boolean isStatusSuccess(int status) {
    return (status >= 200 && status < 300);
}

public static boolean isStatusError(int status) {
    return (status >= 400 && status < 600);
}

public static boolean isStatusClientError(int status) {
    return (status >= 400 && status < 500);
}

public static boolean isStatusServerError(int status) {
    return (status >= 500 && status < 600);
}

public static boolean isStatusCompleted(int status) {
    return (status >= 200 && status < 300)
            || (status >= 400 && status < 600);
}

public static final int STATUS_PENDING = 190;

public static final int STATUS_RUNNING = 192;

public static final int STATUS_SUCCESS = 200;

public static final int STATUS_BAD_REQUEST = 400;

public static final int STATUS_FORBIDDEN = 403;

public static final int STATUS_NOT_ACCEPTABLE = 406;

public static final int STATUS_LENGTH_REQUIRED = 411;

public static final int STATUS_PRECONDITION_FAILED = 412;

public static final int STATUS_CANCELED = 490;

public static final int STATUS_UNKNOWN_ERROR = 491;

public static final int STATUS_FILE_ERROR = 492;

public static final int STATUS_ERROR_NO_SDCARD = 493;

public static final int STATUS_ERROR_SDCARD_FULL = 494;

public static final int STATUS_UNHANDLED_OBEX_CODE = 495;

public static final int STATUS_OBEX_DATA_ERROR = 496;

public static final int STATUS_CONNECTION_ERROR = 497;

}

【问题讨论】:

    标签: android android-bluetooth


    【解决方案1】:

    也许您可以尝试使用 BufferedWriter 和 BufferedReader 的另一种解决方案。

    这是一段代码:

                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;
                    }
                }
    

    阅读:

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

    希望对你有帮助。

    【讨论】:

    • 忘记 app.setmSocket(mmSocket); ..这只是给我的。
    • 它显示这个错误你知道如何解决.. ##### 错误:wrp_wsock_create:wrp_sock_create:超出wsock块#####
    • 请提供您拥有的任何样品或我在过去两周为此苦苦挣扎的任何参考资料。
    • 我不知道这个错误是什么意思。你想寄什么?
    • 我想发送图片。我可以配对设备但不能发送数据。
    【解决方案2】:

    BluetoothShare 类不支持 android 4.1 及更高版本。您可以使用以下意图编码在 android 4.1 及更高版本中发送文件

    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);
    

    还有一些 Android v 2.2/2.3 中的设备不通过 bluetoothShare 类发送文件。

    【讨论】:

      【解决方案3】:

      也许这可以在某种程度上帮助你……

      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);       
          }
      }
      

      【讨论】:

        【解决方案4】:

        此代码适用于联发科的固件。在安卓 4.0.4 上测试。发送文件,不向用户询问任何内容

        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;
        }
        

        在搭载 Android 2.2.1 的 MediaTek 旧手机上,此代码启动 BluetoothDevicePicker 以完成操作。

        【讨论】:

          猜你喜欢
          • 2012-02-29
          • 1970-01-01
          • 1970-01-01
          • 2016-12-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-03
          相关资源
          最近更新 更多