android+蓝牙传输文件,在Android中使用蓝牙的消息和文件传输

我正在开发一个应用程序,首先我们必须搜索和连接可用的配对蓝牙设备.我做到了连接.但之后我放了一个屏幕要求在文本和文件传输之间进行选择.当我选择文本时,将打开另一个屏幕,其中有edittext和按钮.无论用户在edittext中输入什么,点击按钮,都应该转移到BT聊天应用程序等连接的BT设备.我已经通过BT聊天应用程序,但理解起来很复杂.我想要一个单独的功能,它可以完成任务.以下是我的连接代码.

Main.java

public class Main extends Activity implements Runnable

{

protected static final String TAG = "TAG";

private static final int REQUEST_CONNECT_DEVICE = 1;

private static final int REQUEST_ENABLE_BT = 2;

Button mScan;

BluetoothAdapter mBluetoothAdapter;

private UUID applicationUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

private ProgressDialog mBluetoothConnectProgressDialog;

private BluetoothSocket mBluetoothSocket;

BluetoothDevice mBluetoothDevice;

@Override

public void onCreate(Bundle mSavedInstanceState)

{

super.onCreate(mSavedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.main);

mScan = (Button) findViewById(R.id.Scan);

mScan.setOnClickListener(new View.OnClickListener()

{

public void onClick(View mView)

{

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (mBluetoothAdapter == null)

{

Toast.makeText(Main.this, "Message1", 2000).show();

}

else

{

if (!mBluetoothAdapter.isEnabled())

{

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

}

else

{

ListPairedDevices();

Intent connectIntent = new Intent(Main.this, DeviceListActivity.class);

startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE);

}

}

}

});

}

public void onActivityResult(int mRequestCode, int mResultCode, Intent mDataIntent)

{

super.onActivityResult(mRequestCode, mResultCode, mDataIntent);

switch (mRequestCode)

{

case REQUEST_CONNECT_DEVICE:

if (mResultCode == Activity.RESULT_OK)

{

Bundle mExtra = mDataIntent.getExtras();

String mDeviceAddress = mExtra.getString("DeviceAddress");

Log.v(TAG, "Coming incoming address " + mDeviceAddress);

mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mDeviceAddress);

mBluetoothConnectProgressDialog = ProgressDialog.show(this, "Connecting...", mBluetoothDevice.getName() + " : " + mBluetoothDevice.getAddress(), true, false);

Thread mBlutoothConnectThread = new Thread(this);

mBlutoothConnectThread.start();

//pairToDevice(mBluetoothDevice); This method is replaced by progress dialog with thread

}

break;

case REQUEST_ENABLE_BT:

if (mResultCode == Activity.RESULT_OK)

{

ListPairedDevices();

Intent connectIntent = new Intent(Main.this, DeviceListActivity.class);

startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE);

}

else

{

Toast.makeText(Main.this, "Message", 2000).show();

}

break;

}

}

private void ListPairedDevices()

{

Set mPairedDevices = mBluetoothAdapter.getBondedDevices();

if (mPairedDevices.size() > 0)

{

for (BluetoothDevice mDevice : mPairedDevices)

{

Log.v(TAG, "PairedDevices: " + mDevice.getName() + " " + mDevice.getAddress());

}

}

}

public void run()

{

try

{

mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(applicationUUID);

mBluetoothAdapter.cancelDiscovery();

mBluetoothSocket.connect();

mHandler.sendEmptyMessage(0);

}

catch (IOException eConnectException)

{

Log.d(TAG, "CouldNotConnectToSocket", eConnectException);

closeSocket(mBluetoothSocket);

return;

}

}

private void closeSocket(BluetoothSocket nOpenSocket)

{

try

{

nOpenSocket.close();

Log.d(TAG, "SocketClosed");

}

catch (IOException ex)

{

Log.d(TAG, "CouldNotCloseSocket");

}

}

private Handler mHandler = new Handler()

{

@Override

public void handleMessage(Message msg)

{

mBluetoothConnectProgressDialog.dismiss();

Toast.makeText(Main.this, "Device Connected", 5000).show();

Intent in = new Intent(getBaseContext(), Option.class);

startActivity(in);

}

};

}

DeviceListActivity.java

public class DeviceListActivity extends Activity

{

protected static final String TAG = "TAG";

private BluetoothAdapter mBluetoothAdapter;

private ArrayAdapter mPairedDevicesArrayAdapter;

@Override

protected void onCreate(Bundle mSavedInstanceState)

{

super.onCreate(mSavedInstanceState);

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

setContentView(R.layout.device_list);

setResult(Activity.RESULT_CANCELED);

mPairedDevicesArrayAdapter = new ArrayAdapter(this, R.layout.device_name);

ListView mPairedListView = (ListView) findViewById(R.id.paired_devices);

mPairedListView.setAdapter(mPairedDevicesArrayAdapter);

mPairedListView.setOnItemClickListener(mDeviceClickListener);

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

Set mPairedDevices = mBluetoothAdapter.getBondedDevices();

if (mPairedDevices.size() > 0)

{

findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);

for (BluetoothDevice mDevice : mPairedDevices)

{

mPairedDevicesArrayAdapter.add(mDevice.getName() + "\n" + mDevice.getAddress());

}

}

else

{

String mNoDevices = getResources().getText(R.string.none_paired).toString();

mPairedDevicesArrayAdapter.add(mNoDevices);

}

}

@Override

protected void onDestroy()

{

super.onDestroy();

if (mBluetoothAdapter != null)

{

mBluetoothAdapter.cancelDiscovery();

}

}

private OnItemClickListener mDeviceClickListener = new OnItemClickListener()

{

public void onItemClick(AdapterView> mAdapterView, View mView, int mPosition, long mLong)

{

mBluetoothAdapter.cancelDiscovery();

String mDeviceInfo = ((TextView) mView).getText().toString();

String mDeviceAddress = mDeviceInfo.substring(mDeviceInfo.length() - 17);

Log.v(TAG, "Device_Address " + mDeviceAddress);

Bundle mBundle = new Bundle();

mBundle.putString("DeviceAddress", mDeviceAddress);

Intent mBackIntent = new Intent();

mBackIntent.putExtras(mBundle);

setResult(Activity.RESULT_OK, mBackIntent);

finish();

}

};

}

option.xml文件

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/file" />

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/txt" />

message.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/editText1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ems="10"

android:hint="@string/hint">

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/send" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值