首页 > Android, Other > Android中的蓝牙传送文件

Android中的蓝牙传送文件

2012-12-13 13:33 星期四    浏览: 2,429    绿 发表评论 阅读评论

Android中的蓝牙传送文件功能运行在进程com.android.bluetooth中,apk安装包文件是system/app/Bluetooth.apk,代码在目录packages/apps/Bluetooth/src/com/android/bluetooth/opp/下,由Motorola贡献。在Android的4.0.4版本中,有一个小bug,当向外传输文件时,状态栏中的提示消息右侧为当前发送时间,但是显示为69年或70年的日期。这主要是因为代码(BluetoothOppNotification.java)中的时间变量timeStamp声明为int型而不是long型,造成溢出,从而导致状态通知消息中的时间显示错误的日期。查阅在JellyBean(4.2.1)版本,这个问题得到了纠正(另:在4.2.1中,BluetoothOppNotification添加了对BT Handover的支持,并将A2DP挪到了packages/apps/Bluetooth/)。

另:由于某些原因,Google发布的Android代码对蓝牙传输所接受的文件作了限制,只允许音视频、图片和某些文档文件进行传输,不允许其它类型(如apk安装包和lyrc歌词文件)的接收,它是通过检查文件的扩展名来判断。某些文件可以传,某些不可以传,限制了自由,很烦人。我们可以修改文件:

packages/apps/Bluetooth//src/com/android/bluetooth/opp/Constants.java

为ACCEPTABLE_SHARE_INBOUND_TYPES添加所有类型的支持:”*/*”。具体如下:

public static final String[] ACCEPTABLE_SHARE_INBOUND_TYPES = new String[] {
+ “*/*”,
“image/*”,
“video/*”,
“audio/*”,

另:打开log开关也是在该Constants.java中。

打开log开关,支持接受所有文件传输以及修正时间日期显示的问题的patch如下(基于Android4.0.4):

diff –git a/src/com/android/bluetooth/opp/BluetoothOppNotification.java b/src/com/android/bluetooth/opp/BluetoothOppNotification.java
index a0fc32f..edcb6a3 100644
— a/src/com/android/bluetooth/opp/BluetoothOppNotification.java
+++ b/src/com/android/bluetooth/opp/BluetoothOppNotification.java
@@ -110,7 +110,7 @@ class BluetoothOppNotification {

int totalTotal = 0; // total bytes for current transfer

- int timeStamp = 0; // Database time stamp. Used for sorting ongoing transfers.
+ long timeStamp = 0; // Database time stamp. Used for sorting ongoing transfers.

String description; // the text above progress bar
}
@@ -204,6 +204,7 @@ class BluetoothOppNotification {
Cursor cursor = mContext.getContentResolver().query(BluetoothShare.CONTENT_URI, null,
WHERE_RUNNING, null, BluetoothShare._ID);
if (cursor == null) {
+ Log.v(TAG, “cursor is null, return”);
return;
}

@@ -227,7 +228,8 @@ class BluetoothOppNotification {

mNotifications.clear();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
- int timeStamp = cursor.getInt(timestampIndex);
+ long timeStamp = cursor.getLong(timestampIndex);
+ Log.v(TAG,” cursor.getLong(timestampIndex)=”+ cursor.getLong(timestampIndex)+”,timestampIndex=”+timestampIndex);
int dir = cursor.getInt(directionIndex);
int id = cursor.getInt(idIndex);
int total = cursor.getInt(totalBytesIndex);
@@ -277,10 +279,12 @@ class BluetoothOppNotification {
// TODO: split description into two rows with filename in second row
Notification.Builder b = new Notification.Builder(mContext);
b.setContentTitle(item.description);
+ Log.v(TAG,”item.description=”+item.description);
b.setContentInfo(
BluetoothOppUtility.formatProgressText(item.totalTotal, item.totalCurrent));
b.setProgress(item.totalTotal, item.totalCurrent, item.totalTotal == -1);
b.setWhen(item.timeStamp);
+ Log.v(TAG,”item.timeStamp=”+item.timeStamp);
if (item.direction == BluetoothShare.DIRECTION_OUTBOUND) {
b.setSmallIcon(android.R.drawable.stat_sys_upload);
} else if (item.direction == BluetoothShare.DIRECTION_INBOUND) {
diff –git a/src/com/android/bluetooth/opp/Constants.java b/src/com/android/bluetooth/opp/Constants.java
index 892df70..36b5e39 100644
— a/src/com/android/bluetooth/opp/Constants.java
+++ b/src/com/android/bluetooth/opp/Constants.java
@@ -129,6 +129,7 @@ public class Constants {
* Today, restricted to images, audio, video and certain text types.
*/
public static final String[] ACCEPTABLE_SHARE_INBOUND_TYPES = new String[] {
+ “*/*”,
“image/*”,
“video/*”,
“audio/*”,
@@ -156,12 +157,12 @@ public class Constants {
/**
* Debug level logging
*/
- public static final boolean DEBUG = false;
+ public static final boolean DEBUG = true;

/**
* Verbose level logging
*/
- public static final boolean VERBOSE = false;
+ public static final boolean VERBOSE = true;

/** use TCP socket instead of Rfcomm Socket to develop */
public static final boolean USE_TCP_DEBUG = false;

测试传给PC一首许巍的新歌,输出的log如下:

12-13 04:30:33.700: V/BluetoothOppNotification(2049): update too frequent, put in queue
12-13 04:30:33.830: V/BluetoothOppNotification(2049): update too frequent, put in queue
12-13 04:30:33.951: V/BluetoothOppNotification(2049): new notify threadi!
12-13 04:30:33.951: V/BluetoothOppNotification(2049): send delay message
12-13 04:30:33.961: V/BluetoothOppNotification(2049): mUpdateCompleteNotification = false
12-13 04:30:33.971: V/BluetoothOppNotification(2049): cursor.getLong(timestampIndex)=1355373031751,timestampIndex=12
12-13 04:30:33.981: V/BluetoothOppNotification(2049): ID=6; batchID=1355373031751; totoalCurrent0; totalTotal=7973818
12-13 04:30:33.981: V/BluetoothOppNotification(2049): item.description=Bluetooth share: Sending 救赎之旅.mp3
12-13 04:30:33.991: V/BluetoothOppNotification(2049): item.timeStamp=1355373031751
12-13 04:30:34.021: V/BluetoothOppNotification(2049): No need to update complete notification
12-13 04:30:36.073: V/BluetoothOppNotification(2049): send message
12-13 04:30:36.083: V/BluetoothOppNotification(2049): new notify threadi!
12-13 04:30:36.083: V/BluetoothOppNotification(2049): send delay message
12-13 04:30:36.093: V/BluetoothOppNotification(2049): mUpdateCompleteNotification = false
12-13 04:30:36.093: V/BluetoothOppNotification(2049): cursor.getLong(timestampIndex)=1355373031751,timestampIndex=12
12-13 04:30:36.093: V/BluetoothOppNotification(2049): ID=6; batchID=1355373031751; totoalCurrent32721; totalTotal=7973818
12-13 04:30:36.093: V/BluetoothOppNotification(2049): item.description=Bluetooth share: Sending 救赎之旅.mp3
12-13 04:30:36.093: V/BluetoothOppNotification(2049): item.timeStamp=1355373031751
12-13 04:30:36.103: V/BluetoothOppNotification(2049): No need to update complete notification
12-13 04:30:36.563: V/BluetoothOppNotification(2049): update too frequent, put in queue

在没有改为long型的log输出如下(cursor.getInt(timestampIndex)的返回值因溢出为负,故不对):

12-13 03:30:41.302: V/BluetoothOppNotification(2006): new notify threadi!
12-13 03:30:41.302: V/BluetoothOppNotification(2006): send delay message
12-13 03:30:41.312: V/BluetoothOppNotification(2006): mUpdateCompleteNotification = false
12-13 03:30:41.312: V/BluetoothOppNotification(2006): cursor.getInt(timestampIndex)=-1840228159,timestampIndex=12
12-13 03:30:41.312: V/BluetoothOppNotification(2006): ID=5; batchID=-1840228159; totoalCurrent0; totalTotal=6905237
12-13 03:30:41.312: V/BluetoothOppNotification(2006): item.description=Bluetooth share: Sending 世外桃源.mp3
12-13 03:30:41.312: V/BluetoothOppNotification(2006): item.timeStamp=-1840228159
12-13 03:30:41.322: V/BluetoothOppNotification(2006): No need to update complete notification
12-13 03:30:47.077: V/BluetoothOppNotification(2006): send message
12-13 03:30:47.077: V/BluetoothOppNotification(2006): new notify threadi!
12-13 03:30:47.077: V/BluetoothOppNotification(2006): send delay message
12-13 03:30:47.087: V/BluetoothOppNotification(2006): mUpdateCompleteNotification = false
12-13 03:30:47.087: V/BluetoothOppNotification(2006): cursor.getInt(timestampIndex)=-1840228159,timestampIndex=12
12-13 03:30:47.087: V/BluetoothOppNotification(2006): ID=5; batchID=-1840228159; totoalCurrent32721; totalTotal=6905237
12-13 03:30:47.097: V/BluetoothOppNotification(2006): item.description=Bluetooth share: Sending 世外桃源.mp3
12-13 03:30:47.097: V/BluetoothOppNotification(2006): item.timeStamp=-1840228159
12-13 03:30:47.107: V/BluetoothOppNotification(2006): No need to update complete notification

本文链接地址: http://blog.redwolf-soft.com/?p=1893

原创文章,版权©红狼博客所有, 转载随意,但请注明出处。

    分享到:

相关文章:

  • 无相关文章
分类: Android, Other 标签: , ,
  1. 本文目前尚无任何评论.
订阅评论
  欢迎参与讨论,请在这里发表您的看法、交流您的观点。