android中使用蓝牙设备发送数据的常用蓝牙功能
我正在android中开发蓝牙应用程序,首先我要扫描可用的蓝牙设备,然后连接它们。建立连接后,我必须向他们发送数据。我已经做了直到连接。但我找不到将数据发送到连接设备的通用函数。我必须在我的应用程序中多次使用此功能。下面是我的连接代码…请帮助我android中使用蓝牙设备发送数据的常用蓝牙功能,android,sockets,bluetooth,Android,Sockets,Bluetooth,我正在android中开发蓝牙应用程序,首先我要扫描可用的蓝牙设备,然后连接它们。建立连接后,我必须向他们发送数据。我已经做了直到连接。但我找不到将数据发送到连接设备的通用函数。我必须在我的应用程序中多次使用此功能。下面是我的连接代码…请帮助我 import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.text
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.Set;
import java.util.TimeZone;
import java.util.UUID;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint("NewApi")
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;
String MESSAGEPASS;
@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<BluetoothDevice> 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);
}
};
}
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.OutputStream;
导入java.text.simpleDataFormat;
导入java.util.Calendar;
导入java.util.Date;
导入java.util.Random;
导入java.util.Set;
导入java.util.TimeZone;
导入java.util.UUID;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.Dialog;
导入android.app.ProgressDialog;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothSocket;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.content.res.Configuration;
导入android.graphics.Bitmap;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.ColorMatrix;
导入android.graphics.ColorMatrixColorFilter;
导入android.graphics.PorterDuff.Mode;
导入android.graphics.drawable.BitmapDrawable;
导入android.graphics.drawable.drawable;
导入android.os.Bundle;
导入android.os.Environment;
导入android.os.Handler;
导入android.os.Message;
导入android.preference.PreferenceManager;
导入android.provider.Settings;
导入android.util.Log;
导入android.view.KeyEvent;
导入android.view.MotionEvent;
导入android.view.view;
导入android.view.view.OnTouchListener;
导入android.view.Window;
导入android.view.WindowManager;
导入android.view.inputmethod.InputMethodManager;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ImageView;
导入android.widget.TextView;
导入android.widget.Toast;
@SuppressLint(“新API”)
公共类主扩展活动实现可运行
{
受保护的静态最终字符串TAG=“TAG”;
专用静态最终int请求\u连接\u设备=1;
私有静态最终整数请求_ENABLE_BT=2;
按钮mScan;
蓝牙适配器mBluetoothAdapter;
专用UUID应用程序UID=UUID.fromString(“000011101-0000-1000-8000-00805F9B34FB”);
private ProgressDialog mBluetoothConnectProgressDialog;
私人蓝牙插座;
蓝牙设备;
字符串消息传递;
@凌驾
创建时的公共void(Bundle mSavedInstanceState)
{
super.onCreate(mSavedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(R.layout.main);
mScan=(按钮)findViewById(R.id.Scan);
mScan.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图mView)
{
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter==null)
{
Toast.makeText(Main.this,“Message1”,2000).show();
}
其他的
{
如果(!mBluetoothAdapter.isEnabled())
{
Intent enablebintent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent、请求\启用\ BT);
}
其他的
{
ListPairedDevices();
Intent connectIntent=新的Intent(Main.this、DeviceListActivity.class);
startActivityForResult(连接内容、请求连接设备);
}
}
}
});
}
activityResult上的公共无效(int-mreRequestCode、int-mResultCode、Intent-mDataIntent)
{
super.onActivityResult(mRequestCode、mResultCode、mDataIntent);
开关(mRequestCode)
{
案例请求\u连接\u设备:
如果(mResultCode==Activity.RESULT\u确定)
{
Bundle-mExtra=mDataIntent.getExtras();
字符串mDeviceAddress=mExtra.getString(“DeviceAddress”);
Log.v(标记“即将到来的传入地址”+mDeviceAddress);
mBluetoothDevice=mBluetoothAdapter.getRemoteDevice(mDeviceAddress);
mBluetoothConnectProgressDialog=ProgressDialog.show(此“连接…”,mBluetoothDevice.getName()+”:“+mBluetoothDevice.getAddress(),true,false);
线程mBlutoothConnectThread=新线程(此线程);
mBlutoothConnectThread.start();
//pairToDevice(mBluetoothDevice);此方法由带有线程的进度对话框替换
}
打破
案例请求\u启用\u BT:
如果(mResultCode==Activity.RESULT\u确定)
{
ListPairedDevices();
Intent connectIntent=新的Intent(Main.this、DeviceListActivity.class);
startActivityForResult(连接内容、请求连接设备);
}
其他的
{
Toast.makeText(Main.this,“Message”,2000).show();
}
打破
}
}
私有void ListPairedDevices()
{
设置mPairedDevices=mBluetoothAdapter.getBondedDevices();
如果(mPairedDevices.size()>0)
{
用于(蓝牙设备mDevice:mPairedDevices)